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 174131355 : is_instantiation_of_constexpr (tree fun)
62 : {
63 242558961 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 106205422 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 261824373 : || (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 174367059 : literal_type_p (tree t)
73 : {
74 171277044 : if (SCALAR_TYPE_P (t)
75 82013601 : || VECTOR_TYPE_P (t)
76 81992978 : || TYPE_REF_P (t)
77 242957767 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : return true;
79 64277341 : if (CLASS_TYPE_P (t))
80 : {
81 62803047 : t = complete_type (t);
82 62803047 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 62803047 : return CLASSTYPE_LITERAL_P (t);
84 : }
85 1474294 : if (TREE_CODE (t) == ARRAY_TYPE)
86 1474107 : 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 320651491 : ensure_literal_type_for_constexpr_object (tree decl)
96 : {
97 320651491 : tree type = TREE_TYPE (decl);
98 320651491 : if (VAR_P (decl)
99 120546975 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 84447652 : || var_in_constexpr_fn (decl))
101 374017100 : && !processing_template_decl)
102 : {
103 34855812 : tree stype = strip_array_types (type);
104 34855812 : 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 34855806 : else if (!literal_type_p (type))
108 : {
109 24006 : 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 23953 : else if (cxx_dialect < cxx23)
119 : {
120 18219 : 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 18219 : cp_function_chain->invalid_constexpr = true;
131 : }
132 : }
133 34831800 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 34831800 : && 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 320651491 : 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 967 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : const char *gmsgid, ...)
156 : {
157 967 : diagnostics::diagnostic_info diagnostic;
158 967 : va_list ap;
159 967 : rich_location richloc (line_table, location);
160 967 : va_start (ap, gmsgid);
161 967 : bool ret;
162 967 : 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 260 : 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 967 : va_end (ap);
181 1934 : return ret;
182 967 : }
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 662906192 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : const constexpr_fundef *rhs)
202 : {
203 642483376 : 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 650151813 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : {
212 650151813 : return DECL_UID (fundef->decl);
213 : }
214 :
215 : /* Return a previously saved definition of function FUN. */
216 :
217 : constexpr_fundef *
218 64213697 : retrieve_constexpr_fundef (tree fun)
219 : {
220 64213697 : if (constexpr_fundef_table == NULL)
221 : return NULL;
222 :
223 64209817 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 64209817 : 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 29367963 : is_valid_constexpr_fn (tree fun, bool complain)
232 : {
233 29367963 : bool ret = true;
234 :
235 58735926 : if (DECL_INHERITED_CTOR (fun)
236 3424457 : && 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 29367963 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 59283983 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 29916020 : if (!literal_type_p (TREE_TYPE (parm)))
248 : {
249 13333 : ret = false;
250 13333 : 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 48499891 : 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 58735920 : 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 29367960 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : {
279 25349568 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 25349568 : if (!literal_type_p (rettype))
281 : {
282 50779 : ret = false;
283 50779 : if (complain)
284 : {
285 13 : auto_diagnostic_group d;
286 13 : 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 13 : }
291 : }
292 :
293 : /* C++14 DR 1684 removed this restriction. */
294 25349568 : if (cxx_dialect < cxx14
295 31476 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 25350657 : && !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 4018392 : 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 29367963 : 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 898 : 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 898 : auto_vec<tree, 2> fields;
344 1892 : do
345 : {
346 1892 : fields.safe_push (TREE_OPERAND (member, 1));
347 1892 : member = TREE_OPERAND (member, 0);
348 : }
349 1892 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 3790 : && 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 1892 : while (field = fields.pop(),
357 1892 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : {
359 994 : tree ctor;
360 : /* If there is already an outer constructor entry for the anonymous
361 : aggregate FIELD, use it; otherwise, insert one. */
362 994 : if (vec_safe_is_empty (*vec)
363 198 : || (*vec)->last().index != field)
364 : {
365 892 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 892 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : }
368 : else
369 102 : ctor = (*vec)->last().value;
370 994 : 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 898 : gcc_assert (fields.is_empty());
376 898 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 :
378 898 : return true;
379 898 : }
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 5588212 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : {
390 6221827 : tree member, init;
391 6221827 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 2813423 : t = TREE_OPERAND (t, 0);
393 6221827 : if (TREE_CODE (t) == EXPR_STMT)
394 2812528 : t = TREE_OPERAND (t, 0);
395 6221827 : if (t == error_mark_node)
396 : return false;
397 6221818 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : {
399 7026718 : for (tree stmt : tsi_range (t))
400 808699 : if (! build_data_member_initialization (stmt, vec))
401 9 : return false;
402 : return true;
403 : }
404 5592002 : 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 603604 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : }
413 4988398 : if (TREE_CODE (t) == CONVERT_EXPR)
414 2807213 : t = TREE_OPERAND (t, 0);
415 4988398 : if (TREE_CODE (t) == INIT_EXPR)
416 : {
417 2699233 : member = TREE_OPERAND (t, 0);
418 2699233 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : }
420 2289165 : else if (TREE_CODE (t) == CALL_EXPR)
421 : {
422 1884291 : tree fn = get_callee_fndecl (t);
423 3768582 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : /* We're only interested in calls to subobject constructors. */
425 : return true;
426 1497336 : 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 1497336 : init = break_out_target_exprs (t);
431 : }
432 404874 : else if (TREE_CODE (t) == BIND_EXPR)
433 30011 : 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 4196569 : if (INDIRECT_REF_P (member))
438 58974 : member = TREE_OPERAND (member, 0);
439 4196569 : if (TREE_CODE (member) == NOP_EXPR)
440 : {
441 530915 : tree op = member;
442 530915 : STRIP_NOPS (op);
443 530915 : if (TREE_CODE (op) == ADDR_EXPR)
444 : {
445 609 : 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 530306 : else if (op == current_class_ptr
453 1060481 : && (same_type_ignoring_top_level_qualifiers_p
454 530175 : (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 458390 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : }
464 : }
465 4196569 : if (TREE_CODE (member) == ADDR_EXPR)
466 1026004 : member = TREE_OPERAND (member, 0);
467 4196569 : if (TREE_CODE (member) == COMPONENT_REF)
468 : {
469 3642373 : tree aggr = TREE_OPERAND (member, 0);
470 3642373 : if (TREE_CODE (aggr) == VAR_DECL)
471 : /* Initializing a local variable, don't add anything. */
472 : return true;
473 3642371 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : /* Normal member initialization. */
475 3641470 : member = TREE_OPERAND (member, 1);
476 901 : else if (VAR_P (get_base_address (aggr)))
477 : /* Initializing a local variable, don't add anything. */
478 : return true;
479 898 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
480 : /* Initializing a member of an anonymous union. */
481 898 : 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 5220400 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
491 56 : (*vec)->last().value = init;
492 : else
493 4195610 : 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 2394 : check_constexpr_bind_expr_vars (tree t)
503 : {
504 2394 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
505 :
506 3216 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
507 834 : if (TREE_CODE (var) == TYPE_DECL
508 815 : && DECL_IMPLICIT_TYPEDEF_P (var)
509 856 : && !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 4133 : check_constexpr_ctor_body_1 (tree last, tree list)
518 : {
519 4133 : 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 2057 : case BIND_EXPR:
532 2057 : if (!check_constexpr_bind_expr_vars (list)
533 2057 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
534 : /*complain=*/false))
535 42 : 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 5526336 : 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 5526336 : if (cxx_dialect >= cxx14)
556 : return true;
557 :
558 13623 : bool ok = true;
559 13623 : if (TREE_CODE (list) == STATEMENT_LIST)
560 : {
561 13597 : tree_stmt_iterator i = tsi_last (list);
562 17635 : for (; !tsi_end_p (i); tsi_prev (&i))
563 : {
564 15512 : tree t = tsi_stmt (i);
565 15512 : if (t == last)
566 : break;
567 4107 : if (!check_constexpr_ctor_body_1 (last, t))
568 : {
569 : ok = false;
570 : break;
571 : }
572 : }
573 : }
574 26 : else if (list != last
575 26 : && !check_constexpr_ctor_body_1 (last, list))
576 : ok = false;
577 13623 : if (!ok && complain)
578 : {
579 48 : pedwarn (input_location, OPT_Wc__14_extensions,
580 : "%<constexpr%> constructor does not have empty body");
581 48 : 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 3351696 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
594 : {
595 3351696 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
596 3351696 : tree field_type;
597 3351696 : unsigned i;
598 3351696 : constructor_elt *ce;
599 :
600 3351696 : if (pri)
601 60013 : field_type = BINFO_TYPE (pri);
602 3291683 : else if (TYPE_CONTAINS_VPTR_P (type))
603 34631 : 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 133970 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
610 99219 : if (TREE_TYPE (ce->index) == field_type)
611 : break;
612 :
613 115332 : 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 3423593 : build_constexpr_constructor_member_initializers (tree type, tree body)
630 : {
631 3423593 : vec<constructor_elt, va_gc> *vec = NULL;
632 3423593 : bool ok = true;
633 5369535 : while (true)
634 5369535 : switch (TREE_CODE (body))
635 : {
636 1945877 : case MUST_NOT_THROW_EXPR:
637 1945877 : case EH_SPEC_BLOCK:
638 1945877 : case TRY_FINALLY_EXPR: // For C++26 postconditions.
639 1945877 : body = TREE_OPERAND (body, 0);
640 1945877 : break;
641 :
642 65 : case STATEMENT_LIST:
643 5369619 : for (tree stmt : tsi_range (body))
644 : {
645 133 : body = stmt;
646 133 : if (TREE_CODE (body) == BIND_EXPR)
647 : break;
648 : }
649 : break;
650 :
651 3423593 : case BIND_EXPR:
652 3423593 : body = BIND_EXPR_BODY (body);
653 3423593 : goto found;
654 :
655 0 : default:
656 0 : gcc_unreachable ();
657 : }
658 3423593 : found:
659 3423593 : 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 3423593 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
666 : {
667 1879048 : body = TREE_OPERAND (body, 0);
668 1879048 : if (TREE_CODE (body) == EXPR_STMT)
669 1879038 : body = TREE_OPERAND (body, 0);
670 1879048 : if (TREE_CODE (body) == INIT_EXPR
671 1879048 : && (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 1879048 : ok = build_data_member_initialization (body, &vec);
679 : }
680 1544545 : else if (TREE_CODE (body) == STATEMENT_LIST)
681 : {
682 4438317 : for (tree stmt : tsi_range (body))
683 : {
684 2897119 : ok = build_data_member_initialization (stmt, &vec);
685 2897119 : if (!ok)
686 : break;
687 : }
688 : }
689 3346 : else if (EXPR_P (body))
690 3346 : ok = build_data_member_initialization (body, &vec);
691 : else
692 0 : gcc_assert (errorcount > 0);
693 3423593 : if (ok)
694 : {
695 3423584 : if (vec_safe_length (vec) > 0)
696 : {
697 : /* In a delegating constructor, return the target. */
698 3171704 : constructor_elt *ce = &(*vec)[0];
699 3171704 : if (ce->index == current_class_ptr)
700 : {
701 71888 : body = ce->value;
702 71888 : vec_free (vec);
703 71888 : return body;
704 : }
705 : }
706 3351696 : vec = sort_constexpr_mem_initializers (type, vec);
707 3351696 : 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 466701961 : get_function_named_in_call (tree t)
719 : {
720 466701961 : tree callee = cp_get_callee (t);
721 466701961 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
722 466701961 : 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 68583 : constexpr_fn_retval (tree body)
732 : {
733 75109 : 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 31538 : case RETURN_EXPR:
755 31538 : 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 6195 : case CLEANUP_POINT_EXPR:
768 6195 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
769 :
770 337 : case BIND_EXPR:
771 337 : if (!check_constexpr_bind_expr_vars (body))
772 6 : return error_mark_node;
773 331 : 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 28767172 : massage_constexpr_body (tree fun, tree body)
799 : {
800 57534344 : if (DECL_CONSTRUCTOR_P (fun))
801 3423593 : body = build_constexpr_constructor_member_initializers
802 3423593 : (DECL_CONTEXT (fun), body);
803 25343579 : else if (cxx_dialect < cxx14)
804 : {
805 31353 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
806 1 : body = EH_SPEC_STMTS (body);
807 31353 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
808 20264 : body = TREE_OPERAND (body, 0);
809 31353 : body = constexpr_fn_retval (body);
810 : }
811 28767172 : 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 3114807 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
819 : {
820 : /* We allow uninitialized bases/fields in C++20. */
821 3114807 : if (cxx_dialect >= cxx20)
822 : return false;
823 :
824 13339 : unsigned nelts = 0;
825 :
826 13339 : if (body)
827 : {
828 13335 : if (TREE_CODE (body) != CONSTRUCTOR)
829 : return false;
830 13284 : nelts = CONSTRUCTOR_NELTS (body);
831 : }
832 13288 : tree field = TYPE_FIELDS (ctype);
833 :
834 13288 : if (TREE_CODE (ctype) == UNION_TYPE)
835 : {
836 109 : 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 107 : 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 28039 : for (unsigned i = 0; i <= nelts; ++i)
850 : {
851 28039 : tree index = NULL_TREE;
852 28039 : if (i < nelts)
853 : {
854 14860 : index = CONSTRUCTOR_ELT (body, i)->index;
855 : /* Skip vptr adjustment represented with a COMPONENT_REF. */
856 14860 : if (TREE_CODE (index) != FIELD_DECL)
857 602 : continue;
858 : }
859 :
860 480818 : for (; field != index; field = DECL_CHAIN (field))
861 : {
862 453387 : tree ftype;
863 453387 : if (TREE_CODE (field) != FIELD_DECL)
864 904974 : continue;
865 1772 : if (DECL_UNNAMED_BIT_FIELD (field))
866 10 : continue;
867 : /* Artificial fields can be ignored unless they're bases. */
868 1762 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
869 6 : continue;
870 1756 : 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 1752 : ftype = TREE_TYPE (field);
880 1752 : 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 1751 : if (is_empty_field (field))
885 : /* An empty field doesn't need an initializer. */
886 1716 : 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 27431 : if (field == NULL_TREE)
906 : break;
907 :
908 14258 : 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 14258 : 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 159098097 : maybe_save_constexpr_fundef (tree fun)
928 : {
929 159098097 : if (processing_template_decl
930 65651492 : || cp_function_chain->invalid_constexpr
931 224731445 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
932 130331190 : 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 50124108 : bool implicit = false;
937 50124108 : 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 50124108 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
952 : return;
953 :
954 28822489 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
955 :
956 28822489 : if (!is_valid_constexpr_fn (fun, complain))
957 : return;
958 :
959 28767107 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
960 28767107 : 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 28767071 : bool potential = potential_rvalue_constant_expression (massaged);
969 28767071 : if (!potential && complain)
970 224 : require_potential_rvalue_constant_expression_fncheck (massaged);
971 :
972 32190643 : if (DECL_CONSTRUCTOR_P (fun) && potential
973 32188506 : && !DECL_DEFAULTED_FN (fun))
974 : {
975 3114753 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
976 : massaged, complain))
977 : potential = false;
978 3114727 : 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 3110013 : massaged = DECL_SAVED_TREE (fun);
983 3110013 : potential = potential_rvalue_constant_expression (massaged);
984 3110013 : if (!potential && complain)
985 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
986 : }
987 : }
988 :
989 28767071 : 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 28766917 : 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 28766907 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1013 28766907 : bool clear_ctx = false;
1014 28766907 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1015 : {
1016 28766907 : clear_ctx = true;
1017 28766907 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1018 : }
1019 28766907 : tree saved_fn = current_function_decl;
1020 28766907 : current_function_decl = fun;
1021 28766907 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1022 28766907 : current_function_decl = saved_fn;
1023 28766907 : if (clear_ctx)
1024 28766907 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1025 28766907 : 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 59669 : entry.result = error_mark_node;
1030 :
1031 28766907 : 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 28802004 : register_constexpr_fundef (const constexpr_fundef &value)
1039 : {
1040 : /* Create the constexpr function table if necessary. */
1041 28802004 : if (constexpr_fundef_table == NULL)
1042 26307 : constexpr_fundef_table
1043 26307 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1044 :
1045 28802004 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1046 28802004 : (const_cast<constexpr_fundef *> (&value), INSERT);
1047 :
1048 28802004 : gcc_assert (*slot == NULL);
1049 28802004 : *slot = ggc_alloc<constexpr_fundef> ();
1050 28802004 : **slot = value;
1051 28802004 : }
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 49378013 : 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 525450464 : constexpr_global_ctx ()
1229 1050900928 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1230 525450464 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1231 525450464 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1232 525450464 : contract_condition_non_const (false), metafns_called (false) {}
1233 :
1234 319804066 : bool is_outside_lifetime (tree t)
1235 : {
1236 319804066 : if (tree *p = values.get (t))
1237 95821306 : if (*p == void_node)
1238 189 : return true;
1239 : return false;
1240 : }
1241 394460436 : tree get_value (tree t)
1242 : {
1243 394460436 : if (tree *p = values.get (t))
1244 151591815 : if (*p != void_node)
1245 148342105 : return *p;
1246 : return NULL_TREE;
1247 : }
1248 59715114 : tree *get_value_ptr (tree t, bool initializing)
1249 : {
1250 59715114 : if (modifiable && !modifiable->contains (t))
1251 : return nullptr;
1252 59715103 : if (tree *p = values.get (t))
1253 : {
1254 59709808 : 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 185666590 : void put_value (tree t, tree v)
1265 : {
1266 185666590 : bool already_in_map = values.put (t, v);
1267 185666590 : if (!already_in_map && modifiable)
1268 30 : modifiable->add (t);
1269 185666590 : }
1270 125614611 : void destroy_value (tree t)
1271 : {
1272 125614611 : if (TREE_CODE (t) == VAR_DECL
1273 125614611 : || TREE_CODE (t) == PARM_DECL
1274 : || TREE_CODE (t) == RESULT_DECL)
1275 125608806 : values.put (t, void_node);
1276 : else
1277 5805 : values.remove (t);
1278 125614611 : }
1279 8823188 : void clear_value (tree t)
1280 : {
1281 17646376 : 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 173441 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1300 : {
1301 173441 : global->modifiable = &set;
1302 173441 : }
1303 173441 : ~modifiable_tracker ()
1304 : {
1305 173471 : for (tree t: set)
1306 30 : global->clear_value (t);
1307 173441 : global->modifiable = nullptr;
1308 173441 : }
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 1040 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1367 : {
1368 1040 : 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 67229549 : returns (tree *jump_target)
1386 : {
1387 11516748 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1388 : }
1389 :
1390 : static bool
1391 63382751 : breaks (tree *jump_target)
1392 : {
1393 63382751 : return (*jump_target
1394 63382751 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1395 91 : && LABEL_DECL_BREAK (*jump_target))
1396 979066 : || TREE_CODE (*jump_target) == BREAK_STMT
1397 938159 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1398 : }
1399 :
1400 : static bool
1401 90066193 : continues (tree *jump_target)
1402 : {
1403 90066193 : return (*jump_target
1404 90066193 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1405 163 : && LABEL_DECL_CONTINUE (*jump_target))
1406 956533 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1407 : }
1408 :
1409 : static bool
1410 9150482 : switches (tree *jump_target)
1411 : {
1412 48761 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1413 : }
1414 :
1415 : static bool
1416 1567112223 : 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 107635187 : 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 226508562 : p2280_active_p (const constexpr_ctx *ctx)
1429 : {
1430 30440068 : 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 125453741 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1445 : {
1446 125453741 : 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 125453724 : if (!*non_constant_p
1451 96330486 : && DECL_P (t)
1452 : /* Non-trivial destructors have their lifetimes ended explicitly
1453 : with a clobber, so don't worry about it here. */
1454 96324861 : && (!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 421333 : || TREE_CODE (t) == PARM_DECL)
1461 221357930 : && 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 125453724 : 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 8033918 : uid_sensitive_constexpr_evaluation_p ()
1490 : {
1491 7993026 : if (uid_sensitive_constexpr_evaluation_value)
1492 : {
1493 253004 : ++uid_sensitive_constexpr_evaluation_true_counter;
1494 252998 : 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 76885441 : uid_sensitive_constexpr_evaluation_sentinel
1506 76885441 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1507 76885441 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1508 : {
1509 76885441 : }
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 3616744802 : uid_sensitive_constexpr_evaluation_checker
1516 3616744802 : ::uid_sensitive_constexpr_evaluation_checker ()
1517 3616744802 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1518 : {
1519 3616744802 : }
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 2397746666 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1527 : {
1528 2397746666 : return (uid_sensitive_constexpr_evaluation_value
1529 2397746666 : && 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 163778132 : constexpr_call_hasher::hash (constexpr_call *info)
1542 : {
1543 163778132 : 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 169950079 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1552 : {
1553 169950079 : if (lhs == rhs)
1554 : return true;
1555 169950079 : if (lhs->hash != rhs->hash)
1556 : return false;
1557 20422816 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1558 : return false;
1559 20422816 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1560 : }
1561 :
1562 : /* Initialize the constexpr call table, if needed. */
1563 :
1564 : static void
1565 25714940 : maybe_initialize_constexpr_call_table (void)
1566 : {
1567 25714940 : if (constexpr_call_table == NULL)
1568 17387 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1569 25714940 : }
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 47757076 : get_fundef_copy (constexpr_fundef *fundef)
1591 : {
1592 47757076 : tree copy;
1593 47757076 : bool existed;
1594 47757076 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1595 47757076 : (fundef_copies_table, fundef->decl, &existed, 127));
1596 :
1597 47757076 : 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 6802311 : copy = build_tree_list (fundef->body, fundef->parms);
1603 6802311 : TREE_TYPE (copy) = fundef->result;
1604 : }
1605 40954765 : else if (*slot == NULL_TREE)
1606 : {
1607 4353 : 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 4347 : copy = build_tree_list (NULL, NULL);
1612 4347 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1613 4347 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1614 4347 : tree saved_result = DECL_RESULT (fundef->decl);
1615 4347 : tree saved_fn = current_function_decl;
1616 4347 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1617 4347 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1618 4347 : DECL_RESULT (fundef->decl) = fundef->result;
1619 4347 : current_function_decl = fundef->decl;
1620 4347 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1621 4347 : TREE_TYPE (copy));
1622 4347 : current_function_decl = saved_fn;
1623 4347 : DECL_RESULT (fundef->decl) = saved_result;
1624 4347 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1625 4347 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1626 : }
1627 : else
1628 : {
1629 : /* We have a cached function available. */
1630 40950412 : copy = *slot;
1631 40950412 : *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 47757068 : save_fundef_copy (tree fun, tree copy)
1643 : {
1644 47757068 : tree *slot = fundef_copies_table->get (fun);
1645 47757068 : TREE_CHAIN (copy) = *slot;
1646 47757068 : *slot = copy;
1647 47757068 : }
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 225 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1661 : {
1662 225 : tree type = strip_array_types (TREE_TYPE (exc));
1663 225 : if (!CLASS_TYPE_P (type))
1664 : return NULL;
1665 184 : tree std_exception = lookup_qualified_name (std_node, "exception",
1666 : LOOK_want::NORMAL, false);
1667 184 : if (TREE_CODE (std_exception) != TYPE_DECL)
1668 : return NULL;
1669 181 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1670 : return NULL;
1671 181 : base_kind b_kind;
1672 181 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1673 : tf_none);
1674 181 : if (binfo == NULL_TREE || binfo == error_mark_node)
1675 : return NULL;
1676 177 : if (type != TREE_TYPE (exc))
1677 139 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1678 177 : tree call
1679 177 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1680 : tf_none);
1681 177 : if (call == error_mark_node)
1682 : return NULL;
1683 177 : releasing_vec what_args;
1684 177 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1685 177 : if (call == error_mark_node)
1686 : return NULL;
1687 177 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1688 177 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1689 177 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1690 177 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1691 177 : TYPE_SIZE_UNIT (char_type_node))
1692 354 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1693 0 : return NULL;
1694 177 : if (!potential_constant_expression (call))
1695 : return NULL;
1696 177 : bool non_constant_p = false, overflow_p = false;
1697 177 : tree jmp_target = NULL;
1698 177 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1699 : &non_constant_p, &overflow_p,
1700 : &jmp_target);
1701 177 : if (throws (&jmp_target) || non_constant_p)
1702 : return NULL;
1703 177 : if (reduced_constant_expression_p (ptr))
1704 40 : if (const char *msg = c_getstr (ptr))
1705 40 : return xstrdup (msg);
1706 137 : auto_vec <char, 32> v;
1707 5717 : for (unsigned i = 0; i < INT_MAX; ++i)
1708 : {
1709 5717 : tree t = call;
1710 5717 : if (i)
1711 5580 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1712 5717 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1713 5717 : non_constant_p = false;
1714 5717 : overflow_p = false;
1715 5717 : jmp_target = NULL;
1716 5717 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1717 : &non_constant_p, &overflow_p,
1718 : &jmp_target);
1719 5717 : if (throws (&jmp_target)
1720 5717 : || non_constant_p
1721 5717 : || !tree_fits_shwi_p (t2))
1722 0 : return NULL;
1723 5717 : char c = tree_to_shwi (t2);
1724 5717 : v.safe_push (c);
1725 5717 : if (c == '\0')
1726 : break;
1727 : }
1728 274 : return xstrdup (v.address ());
1729 314 : }
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 214 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1768 : {
1769 214 : tree type = strip_array_types (TREE_TYPE (exc));
1770 214 : if (char *str = exception_what_str (ctx, exc))
1771 : {
1772 175 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1773 175 : 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 214 : }
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 30525189 : cxx_cxa_builtin_fn_p (tree fndecl)
1818 : {
1819 30525189 : if (cxx_dialect < cxx26)
1820 : return CXA_NONE;
1821 2705932 : if (DECL_LANGUAGE (fndecl) != lang_c)
1822 : {
1823 2370505 : if (!decl_in_std_namespace_p (fndecl))
1824 : return CXA_NONE;
1825 961189 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1826 : return STD_RETHROW_EXCEPTION;
1827 : return CXA_NONE;
1828 : }
1829 335427 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1830 : return CXA_NONE;
1831 59879 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1832 : return CXA_ALLOCATE_EXCEPTION;
1833 12606 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1834 : return CXA_FREE_EXCEPTION;
1835 12605 : 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 2577 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1840 : return CXA_END_CATCH;
1841 866 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1842 : return CXA_RETHROW;
1843 812 : 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 845 : cxa_check_throw_arg (tree arg, bool free_exc)
1862 : {
1863 845 : STRIP_NOPS (arg);
1864 845 : if (TREE_CODE (arg) != ADDR_EXPR)
1865 : return NULL_TREE;
1866 845 : arg = TREE_OPERAND (arg, 0);
1867 845 : if (!VAR_P (arg)
1868 845 : || !DECL_ARTIFICIAL (arg)
1869 845 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1870 845 : && DECL_NAME (arg) != heap_identifier)
1871 1690 : || !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 17433 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1882 : tree refcount)
1883 : {
1884 17433 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1885 17433 : DECL_ARTIFICIAL (var) = 1;
1886 17433 : retrofit_lang_decl (var);
1887 17433 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1888 17433 : ctx->global->heap_vars.safe_push (var);
1889 17433 : 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 23152 : 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 23152 : int nargs = call_expr_nargs (call);
1902 23152 : location_t loc = cp_expr_loc_or_input_loc (call);
1903 23152 : tree args[4], arg;
1904 23152 : 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 23152 : 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 26459 : && 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 38027 : for (int i = 0; i < nargs; ++i)
1993 : {
1994 18182 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1995 : vc_prvalue, non_constant_p,
1996 : overflow_p, jump_target);
1997 18182 : if (*non_constant_p)
1998 : return call;
1999 18182 : if (*jump_target)
2000 : return NULL_TREE;
2001 : }
2002 19845 : switch (kind)
2003 : {
2004 15738 : case CXA_ALLOCATE_EXCEPTION:
2005 15738 : if (nargs != 1)
2006 0 : goto invalid_nargs;
2007 15738 : 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 31476 : tree type = build_array_type_nelts (char_type_node,
2017 15738 : tree_to_uhwi (args[0]));
2018 15738 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2019 15738 : ctx->global->put_value (var, NULL_TREE);
2020 15738 : 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 774 : case CXA_THROW:
2040 774 : if (nargs != 3)
2041 0 : goto invalid_nargs;
2042 774 : arg = cxa_check_throw_arg (args[0], false);
2043 774 : if (arg == NULL_TREE)
2044 0 : goto invalid_ptr;
2045 774 : DECL_EXCEPTION_REFCOUNT (arg)
2046 774 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2047 : size_one_node);
2048 774 : ++ctx->global->uncaught_exceptions;
2049 774 : *jump_target = arg;
2050 774 : 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 1330 : case BUILTIN_CURRENT_EXCEPTION:
2202 1330 : if (nargs != 0)
2203 0 : goto invalid_nargs;
2204 : else
2205 : {
2206 1330 : tree name = get_identifier ("exception_ptr");
2207 1330 : tree decl = lookup_qualified_name (std_node, name);
2208 1330 : tree fld;
2209 1330 : if (TREE_CODE (decl) != TYPE_DECL
2210 1330 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2211 1330 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2212 1330 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2213 1330 : || DECL_ARTIFICIAL (fld)
2214 1330 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2215 1330 : || next_aggregate_field (DECL_CHAIN (fld))
2216 2660 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2217 1330 : 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 2660 : 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 1330 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2237 : {
2238 1310 : *non_constant_p = true;
2239 1310 : 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 14321834 : 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 14321834 : const int nargs = call_expr_nargs (t);
2470 14321834 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2471 14321834 : tree new_call;
2472 14321834 : int i;
2473 :
2474 : /* Don't fold __builtin_constant_p within a constexpr function. */
2475 14321834 : 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 1819791 : if (bi_const_p
2480 1819791 : && ctx->manifestly_const_eval != mce_true
2481 1708185 : && current_function_decl
2482 1707027 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2483 : {
2484 1413411 : *non_constant_p = true;
2485 1413411 : return t;
2486 : }
2487 :
2488 12908423 : if (fndecl_built_in_p (fun, BUILT_IN_FRONTEND))
2489 4040227 : switch (DECL_FE_FUNCTION_CODE (fun))
2490 : {
2491 4031410 : 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 4031410 : if (ctx->manifestly_const_eval == mce_unknown)
2498 : {
2499 3996924 : *non_constant_p = true;
2500 3996924 : return t;
2501 : }
2502 34486 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2503 34486 : boolean_type_node);
2504 :
2505 4568 : case CP_BUILT_IN_SOURCE_LOCATION:
2506 4568 : {
2507 4568 : temp_override<tree> ovr (current_function_decl);
2508 4568 : if (ctx->call && ctx->call->fundef)
2509 1442 : current_function_decl = ctx->call->fundef->decl;
2510 4568 : return fold_builtin_source_location (t);
2511 4568 : }
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 1330 : case CP_BUILT_IN_CURRENT_EXCEPTION:
2519 1330 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_CURRENT_EXCEPTION,
2520 : fun, non_constant_p, overflow_p,
2521 1330 : 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 8870931 : int strops = 0;
2537 8870931 : int strret = 0;
2538 8870931 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2539 8162295 : switch (DECL_FUNCTION_CODE (fun))
2540 : {
2541 : case BUILT_IN_STRLEN:
2542 : case BUILT_IN_STRNLEN:
2543 8870835 : strops = 1;
2544 : break;
2545 112144 : case BUILT_IN_MEMCHR:
2546 112144 : case BUILT_IN_STRCHR:
2547 112144 : case BUILT_IN_STRRCHR:
2548 112144 : strops = 1;
2549 112144 : strret = 1;
2550 112144 : break;
2551 72903 : case BUILT_IN_MEMCMP:
2552 72903 : case BUILT_IN_STRCMP:
2553 72903 : strops = 2;
2554 72903 : break;
2555 28863 : case BUILT_IN_STRSTR:
2556 28863 : strops = 2;
2557 28863 : strret = 1;
2558 28863 : break;
2559 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2560 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2561 42 : case BUILT_IN_OBSERVABLE_CHKPT:
2562 : /* These builtins shall be ignored during constant expression
2563 : evaluation. */
2564 42 : return void_node;
2565 54 : case BUILT_IN_UNREACHABLE:
2566 54 : case BUILT_IN_TRAP:
2567 54 : if (!*non_constant_p && !ctx->quiet)
2568 : {
2569 : /* Do not allow__builtin_unreachable in constexpr function.
2570 : The __builtin_unreachable call with BUILTINS_LOCATION
2571 : comes from cp_maybe_instrument_return. */
2572 13 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2573 0 : error ("%<constexpr%> call flows off the end of the function");
2574 : else
2575 13 : error ("%q+E is not a constant expression", t);
2576 : }
2577 54 : *non_constant_p = true;
2578 54 : return t;
2579 : default:
2580 : break;
2581 : }
2582 :
2583 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2584 : return constant false for a non-constant argument. */
2585 8870835 : constexpr_ctx new_ctx = *ctx;
2586 8870835 : new_ctx.quiet = true;
2587 24893770 : for (i = 0; i < nargs; ++i)
2588 : {
2589 16022936 : tree arg = CALL_EXPR_ARG (t, i);
2590 16022936 : tree oarg = arg;
2591 :
2592 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2593 : expand_builtin doesn't know how to look in the values table. */
2594 16022936 : bool strop = i < strops;
2595 16022936 : if (strop)
2596 : {
2597 448090 : STRIP_NOPS (arg);
2598 448090 : if (TREE_CODE (arg) == ADDR_EXPR)
2599 7943 : arg = TREE_OPERAND (arg, 0);
2600 : else
2601 : strop = false;
2602 : }
2603 :
2604 : /* If builtin_valid_in_constant_expr_p is true,
2605 : potential_constant_expression_1 has not recursed into the arguments
2606 : of the builtin, verify it here. */
2607 16022936 : if (!builtin_valid_in_constant_expr_p (fun)
2608 16022936 : || potential_constant_expression (arg))
2609 : {
2610 16022805 : bool dummy1 = false, dummy2 = false;
2611 16022805 : tree jmp_target = NULL_TREE;
2612 16022805 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2613 : &dummy1, &dummy2, &jmp_target);
2614 16022804 : if (jmp_target)
2615 : {
2616 0 : *jump_target = jmp_target;
2617 0 : return NULL_TREE;
2618 : }
2619 : }
2620 :
2621 16022935 : if (bi_const_p)
2622 : /* For __builtin_constant_p, fold all expressions with constant values
2623 : even if they aren't C++ constant-expressions. */
2624 406379 : arg = cp_fold_rvalue (arg);
2625 15616556 : else if (strop)
2626 : {
2627 7943 : if (TREE_CODE (arg) == CONSTRUCTOR)
2628 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2629 7943 : if (TREE_CODE (arg) == STRING_CST)
2630 2984 : arg = build_address (arg);
2631 : else
2632 : arg = oarg;
2633 : }
2634 :
2635 16022935 : args[i] = arg;
2636 : }
2637 :
2638 8870834 : bool save_ffbcp = force_folding_builtin_constant_p;
2639 8870834 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2640 8870834 : tree save_cur_fn = current_function_decl;
2641 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2642 8870834 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2643 38 : && ctx->call
2644 8870838 : && ctx->call->fundef)
2645 4 : current_function_decl = ctx->call->fundef->decl;
2646 8870834 : if (fndecl_built_in_p (fun,
2647 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2648 : BUILT_IN_FRONTEND))
2649 : {
2650 336 : location_t loc = EXPR_LOCATION (t);
2651 336 : if (nargs >= 1)
2652 333 : VERIFY_CONSTANT (args[0]);
2653 136 : new_call
2654 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2655 : args);
2656 : }
2657 8870498 : else if (fndecl_built_in_p (fun,
2658 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2659 : BUILT_IN_FRONTEND))
2660 : {
2661 471 : location_t loc = EXPR_LOCATION (t);
2662 471 : if (nargs >= 2)
2663 : {
2664 465 : VERIFY_CONSTANT (args[0]);
2665 237 : VERIFY_CONSTANT (args[1]);
2666 : }
2667 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2668 : }
2669 8870027 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
2670 : BUILT_IN_FRONTEND))
2671 : {
2672 1928 : location_t loc = EXPR_LOCATION (t);
2673 1928 : if (nargs >= 1)
2674 : {
2675 1928 : tree arg = CALL_EXPR_ARG (t, 0);
2676 1928 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2677 : non_constant_p, overflow_p,
2678 : jump_target);
2679 1928 : if (*jump_target)
2680 : return NULL_TREE;
2681 1928 : args[0] = arg;
2682 : }
2683 1928 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
2684 : }
2685 : else
2686 17736198 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2687 8868099 : CALL_EXPR_FN (t), nargs, args);
2688 8870406 : current_function_decl = save_cur_fn;
2689 8870406 : force_folding_builtin_constant_p = save_ffbcp;
2690 8870406 : if (new_call == NULL)
2691 : {
2692 6367055 : if (!*non_constant_p && !ctx->quiet)
2693 : {
2694 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2695 0 : CALL_EXPR_FN (t), nargs, args);
2696 0 : error ("%q+E is not a constant expression", new_call);
2697 : }
2698 6367055 : *non_constant_p = true;
2699 6367055 : return t;
2700 : }
2701 :
2702 2503351 : if (!potential_constant_expression (new_call))
2703 : {
2704 724 : if (!*non_constant_p && !ctx->quiet)
2705 4 : error ("%q+E is not a constant expression", new_call);
2706 724 : *non_constant_p = true;
2707 724 : return t;
2708 : }
2709 :
2710 2502627 : if (strret)
2711 : {
2712 : /* memchr returns a pointer into the first argument, but we replaced the
2713 : argument above with a STRING_CST; put it back it now. */
2714 119 : tree op = CALL_EXPR_ARG (t, strret-1);
2715 119 : STRIP_NOPS (new_call);
2716 119 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2717 60 : TREE_OPERAND (new_call, 0) = op;
2718 59 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2719 2502627 : new_call = op;
2720 : }
2721 :
2722 2502627 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2723 : non_constant_p, overflow_p,
2724 2502627 : jump_target);
2725 : }
2726 :
2727 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2728 : the type of the value to match. */
2729 :
2730 : static tree
2731 93236740 : adjust_temp_type (tree type, tree temp)
2732 : {
2733 93236740 : if (same_type_p (TREE_TYPE (temp), type))
2734 : return temp;
2735 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2736 42880395 : if (TREE_CODE (temp) == CONSTRUCTOR)
2737 : {
2738 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2739 3206993 : tree t = copy_node (temp);
2740 3206993 : TREE_TYPE (t) = type;
2741 3206993 : return t;
2742 : }
2743 39673402 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2744 0 : return build0 (EMPTY_CLASS_EXPR, type);
2745 39673402 : gcc_assert (scalarish_type_p (type));
2746 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2747 : type is cv-unqualified. */
2748 39673402 : return cp_fold_convert (cv_unqualified (type), temp);
2749 : }
2750 :
2751 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2752 : sub-CONSTRUCTORs. Otherwise return T.
2753 :
2754 : We use this whenever we initialize an object as a whole, whether it's a
2755 : parameter, a local variable, or a subobject, so that subsequent
2756 : modifications don't affect other places where it was used. */
2757 :
2758 : tree
2759 65958119 : unshare_constructor (tree t MEM_STAT_DECL)
2760 : {
2761 65958119 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2762 : return t;
2763 12301597 : auto_vec <tree*, 4> ptrs;
2764 12301597 : ptrs.safe_push (&t);
2765 12301597 : while (!ptrs.is_empty ())
2766 : {
2767 15213657 : tree *p = ptrs.pop ();
2768 15213657 : tree n = copy_node (*p PASS_MEM_STAT);
2769 23218981 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2770 15213657 : *p = n;
2771 15213657 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2772 15213657 : constructor_elt *ce;
2773 57384635 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2774 14655724 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2775 2912060 : ptrs.safe_push (&ce->value);
2776 : }
2777 12301597 : return t;
2778 12301597 : }
2779 :
2780 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2781 :
2782 : static void
2783 813085 : free_constructor (tree t)
2784 : {
2785 813085 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2786 0 : return;
2787 813085 : releasing_vec ctors;
2788 813085 : vec_safe_push (ctors, t);
2789 1664012 : while (!ctors->is_empty ())
2790 : {
2791 850927 : tree c = ctors->pop ();
2792 850927 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2793 : {
2794 : constructor_elt *ce;
2795 316761 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2796 204268 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2797 37842 : vec_safe_push (ctors, ce->value);
2798 112493 : ggc_free (elts);
2799 : }
2800 850927 : ggc_free (c);
2801 : }
2802 813085 : }
2803 :
2804 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2805 : if *TP is address of a static variable (or part of it) currently being
2806 : constructed or of a heap artificial variable. */
2807 :
2808 : static tree
2809 24543621 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2810 : {
2811 24543621 : if (TREE_CODE (*tp) == ADDR_EXPR)
2812 3866359 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2813 3866359 : if (VAR_P (var) && TREE_STATIC (var))
2814 : {
2815 1759247 : if (DECL_NAME (var) == heap_uninit_identifier
2816 1759247 : || DECL_NAME (var) == heap_identifier
2817 1759247 : || DECL_NAME (var) == heap_vec_uninit_identifier
2818 3518494 : || DECL_NAME (var) == heap_vec_identifier)
2819 : return var;
2820 :
2821 1759247 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2822 1759247 : if (global->get_value (var))
2823 : return var;
2824 : }
2825 24188778 : if (TYPE_P (*tp))
2826 1817 : *walk_subtrees = false;
2827 : return NULL_TREE;
2828 : }
2829 :
2830 : /* Subroutine of cxx_eval_call_expression.
2831 : We are processing a call expression (either CALL_EXPR or
2832 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2833 : all arguments and bind their values to correspondings
2834 : parameters, making up the NEW_CALL context. */
2835 :
2836 : static tree
2837 131556204 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2838 : tree orig_fun, bool *non_constant_p,
2839 : bool *overflow_p, bool *non_constant_args,
2840 : tree *jump_target)
2841 : {
2842 131556204 : int nargs = call_expr_nargs (t);
2843 131556204 : tree parms = DECL_ARGUMENTS (fun);
2844 131556204 : int i, j = 0;
2845 131556204 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2846 1239 : ++nargs;
2847 131556204 : if (DECL_HAS_VTT_PARM_P (fun)
2848 1241 : && fun != orig_fun
2849 131557443 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2850 902 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2851 348 : ++nargs;
2852 : /* We don't record ellipsis args below. */
2853 131556204 : int nparms = list_length (parms);
2854 131556204 : int nbinds = nargs < nparms ? nargs : nparms;
2855 131556204 : tree binds = make_tree_vec (nbinds);
2856 :
2857 : /* The call is not a constant expression if it involves the cdtor for a type
2858 : with virtual bases before C++26. */
2859 131556204 : if (cxx_dialect < cxx26
2860 131556204 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2861 : {
2862 17 : if (!ctx->quiet)
2863 : {
2864 3 : error_at (cp_expr_loc_or_input_loc (t),
2865 : "call to non-%<constexpr%> function %qD", fun);
2866 3 : explain_invalid_constexpr_fn (fun);
2867 : }
2868 17 : *non_constant_p = true;
2869 17 : return binds;
2870 : }
2871 :
2872 214359912 : for (i = 0; i < nargs; ++i)
2873 : {
2874 150584420 : tree x, arg;
2875 150584420 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2876 150584420 : if (parms && DECL_BY_REFERENCE (parms))
2877 8432 : type = TREE_TYPE (type);
2878 150584420 : if (i == 1
2879 150584420 : && j == 0
2880 30629913 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2881 150585548 : && orig_fun != fun)
2882 : {
2883 1128 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2884 1128 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2885 328 : x = boolean_true_node;
2886 : else
2887 800 : x = boolean_false_node;
2888 : j = -1;
2889 : }
2890 150583292 : else if (i == 2
2891 150583292 : && j == -1
2892 1128 : && DECL_HAS_VTT_PARM_P (fun)
2893 1128 : && orig_fun != fun
2894 150584420 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2895 811 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2896 : {
2897 328 : x = build_zero_cst (type);
2898 328 : j = -2;
2899 : }
2900 : else
2901 150582964 : x = get_nth_callarg (t, i + j);
2902 : /* For member function, the first argument is a pointer to the implied
2903 : object. For a constructor, it might still be a dummy object, in
2904 : which case we get the real argument from ctx. */
2905 229663662 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2906 170430867 : && is_dummy_object (x))
2907 : {
2908 10597529 : x = ctx->object;
2909 10597529 : x = build_address (x);
2910 : }
2911 150584420 : if (TREE_ADDRESSABLE (type))
2912 : {
2913 : /* Undo convert_for_arg_passing work here. */
2914 14606 : x = convert_from_reference (x);
2915 14606 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2916 : non_constant_p, overflow_p,
2917 : jump_target);
2918 : }
2919 : else
2920 : /* Normally we would strip a TARGET_EXPR in an initialization context
2921 : such as this, but here we do the elision differently: we keep the
2922 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2923 150569814 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2924 : non_constant_p, overflow_p,
2925 : jump_target);
2926 150584420 : if (*jump_target)
2927 : break;
2928 : /* Check we aren't dereferencing a null pointer when calling a non-static
2929 : member function, which is undefined behaviour. */
2930 114831802 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2931 70964703 : && integer_zerop (arg)
2932 : /* But ignore calls from within compiler-generated code, to handle
2933 : cases like lambda function pointer conversion operator thunks
2934 : which pass NULL as the 'this' pointer. */
2935 150700593 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2936 : {
2937 491 : if (!ctx->quiet)
2938 6 : error_at (cp_expr_loc_or_input_loc (x),
2939 : "dereferencing a null pointer");
2940 491 : *non_constant_p = true;
2941 : }
2942 : /* Don't VERIFY_CONSTANT here. */
2943 150584379 : if (*non_constant_p && ctx->quiet)
2944 : break;
2945 : /* Just discard ellipsis args after checking their constantitude. */
2946 82803725 : if (!parms)
2947 283 : continue;
2948 :
2949 82803442 : if (!*non_constant_p)
2950 : {
2951 : /* Make sure the binding has the same type as the parm. But
2952 : only for constant args. */
2953 82802818 : if (TREE_ADDRESSABLE (type))
2954 : {
2955 9013 : if (!same_type_p (type, TREE_TYPE (arg)))
2956 : {
2957 9 : arg = build_fold_addr_expr (arg);
2958 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2959 9 : arg = convert_from_reference (arg);
2960 : }
2961 : }
2962 82793805 : else if (!TYPE_REF_P (type))
2963 66802267 : arg = adjust_temp_type (type, arg);
2964 82802818 : if (!TREE_CONSTANT (arg))
2965 55903631 : *non_constant_args = true;
2966 26899187 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2967 : /* The destructor needs to see any modifications the callee makes
2968 : to the argument. */
2969 0 : *non_constant_args = true;
2970 : /* If arg is or contains address of a heap artificial variable or
2971 : of a static variable being constructed, avoid caching the
2972 : function call, as those variables might be modified by the
2973 : function, or might be modified by the callers in between
2974 : the cached function and just read by the function. */
2975 26899187 : else if (!*non_constant_args
2976 26899187 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2977 : NULL))
2978 354843 : *non_constant_args = true;
2979 :
2980 : /* For virtual calls, adjust the this argument, so that it is
2981 : the object on which the method is called, rather than
2982 : one of its bases. */
2983 82802818 : if (i == 0 && DECL_VIRTUAL_P (fun))
2984 : {
2985 20801 : tree addr = arg;
2986 20801 : STRIP_NOPS (addr);
2987 20801 : if (TREE_CODE (addr) == ADDR_EXPR)
2988 : {
2989 20781 : tree obj = TREE_OPERAND (addr, 0);
2990 20781 : while (TREE_CODE (obj) == COMPONENT_REF
2991 9822 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
2992 30605 : && !same_type_ignoring_top_level_qualifiers_p
2993 9770 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
2994 54 : obj = TREE_OPERAND (obj, 0);
2995 20781 : if (obj != TREE_OPERAND (addr, 0))
2996 51 : arg = build_fold_addr_expr_with_type (obj,
2997 : TREE_TYPE (arg));
2998 : }
2999 : }
3000 82802818 : TREE_VEC_ELT (binds, i) = arg;
3001 : }
3002 82803442 : parms = TREE_CHAIN (parms);
3003 : }
3004 :
3005 : return binds;
3006 : }
3007 :
3008 : static int
3009 63246944 : push_cx_call_context (tree call)
3010 : {
3011 63246944 : ++call_stack_tick;
3012 63246944 : if (!EXPR_HAS_LOCATION (call))
3013 55631 : SET_EXPR_LOCATION (call, input_location);
3014 63246944 : call_stack.safe_push (call);
3015 63246944 : int len = call_stack.length ();
3016 63246944 : if (len > max_constexpr_depth)
3017 30 : return false;
3018 : return len;
3019 : }
3020 :
3021 : static void
3022 63246942 : pop_cx_call_context (void)
3023 : {
3024 63246942 : ++call_stack_tick;
3025 63246942 : call_stack.pop ();
3026 63246942 : }
3027 :
3028 : vec<tree>
3029 225876 : cx_error_context (void)
3030 : {
3031 225876 : vec<tree> r = vNULL;
3032 225876 : if (call_stack_tick != last_cx_error_tick
3033 225876 : && !call_stack.is_empty ())
3034 : r = call_stack;
3035 225876 : last_cx_error_tick = call_stack_tick;
3036 225876 : return r;
3037 : }
3038 :
3039 : /* E is an operand of a failed assertion, fold it either with or without
3040 : constexpr context. */
3041 :
3042 : static tree
3043 573 : fold_operand (tree e, const constexpr_ctx *ctx)
3044 : {
3045 573 : if (ctx)
3046 : {
3047 32 : bool new_non_constant_p = false, new_overflow_p = false;
3048 32 : tree jmp_target = NULL_TREE;
3049 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3050 : &new_non_constant_p,
3051 : &new_overflow_p, &jmp_target);
3052 : }
3053 : else
3054 541 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3055 573 : return e;
3056 : }
3057 :
3058 : /* If we have a condition in conjunctive normal form (CNF), find the first
3059 : failing clause. In other words, given an expression like
3060 :
3061 : true && true && false && true && false
3062 :
3063 : return the first 'false'. EXPR is the expression. */
3064 :
3065 : static tree
3066 320 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3067 : {
3068 421 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3069 : {
3070 : /* First check the left side... */
3071 186 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3072 186 : if (e == NULL_TREE)
3073 : /* ...if we didn't find a false clause, check the right side. */
3074 101 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3075 : return e;
3076 : }
3077 235 : tree e = contextual_conv_bool (expr, tf_none);
3078 235 : e = fold_operand (e, ctx);
3079 235 : if (integer_zerop (e))
3080 : /* This is the failing clause. */
3081 134 : return expr;
3082 : return NULL_TREE;
3083 : }
3084 :
3085 : /* Wrapper for find_failing_clause_r. */
3086 :
3087 : tree
3088 1597 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3089 : {
3090 1597 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3091 134 : if (tree e = find_failing_clause_r (ctx, expr))
3092 1597 : expr = e;
3093 1597 : return expr;
3094 : }
3095 :
3096 : /* Emit additional diagnostics for failing condition BAD.
3097 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3098 : If SHOW_EXPR_P is true, print the condition (because it was
3099 : instantiation-dependent). */
3100 :
3101 : void
3102 1597 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3103 : const constexpr_ctx *ctx /* = nullptr */)
3104 : {
3105 : /* Nobody wants to see the artificial (bool) cast. */
3106 1597 : bad = tree_strip_nop_conversions (bad);
3107 1597 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3108 3 : bad = TREE_OPERAND (bad, 0);
3109 :
3110 1597 : auto_diagnostic_nesting_level sentinel;
3111 :
3112 : /* Actually explain the failure if this is a concept check or a
3113 : requires-expression. */
3114 1597 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3115 287 : diagnose_constraints (cloc, bad, NULL_TREE);
3116 : /* Similarly if this is a standard trait. */
3117 1310 : else if (maybe_diagnose_standard_trait (cloc, bad))
3118 : ;
3119 982 : else if (COMPARISON_CLASS_P (bad)
3120 982 : && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))
3121 21 : || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))))
3122 : {
3123 169 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3124 169 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3125 169 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3126 169 : inform (cloc, "the comparison reduces to %qE", cond);
3127 : }
3128 813 : else if (show_expr_p)
3129 571 : inform (cloc, "%qE evaluates to false", bad);
3130 1597 : }
3131 :
3132 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3133 : do it without changing the current evaluation state. If it evaluates to
3134 : false, complain and return false; otherwise, return true. */
3135 :
3136 : static bool
3137 173449 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3138 : location_t loc, bool evaluated,
3139 : bool *non_constant_p, bool *overflow_p)
3140 : {
3141 173449 : if (*non_constant_p)
3142 : return true;
3143 :
3144 173449 : tree eval, jmp_target = NULL_TREE;
3145 173449 : if (!evaluated)
3146 : {
3147 173449 : if (!potential_rvalue_constant_expression (arg))
3148 16 : return true;
3149 :
3150 173433 : constexpr_ctx new_ctx = *ctx;
3151 173433 : new_ctx.quiet = true;
3152 173433 : bool new_non_constant_p = false, new_overflow_p = false;
3153 : /* Avoid modification of existing values. */
3154 173433 : modifiable_tracker ms (new_ctx.global);
3155 173433 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3156 : &new_non_constant_p,
3157 : &new_overflow_p, &jmp_target);
3158 173433 : }
3159 : else
3160 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3161 : non_constant_p,
3162 : overflow_p, &jmp_target);
3163 173433 : if (jmp_target)
3164 : return true;
3165 :
3166 173433 : if (!*non_constant_p && integer_zerop (eval))
3167 : {
3168 42 : if (!ctx->quiet)
3169 : {
3170 : /* See if we can find which clause was failing
3171 : (for logical AND). */
3172 13 : tree bad = find_failing_clause (ctx, arg);
3173 : /* If not, or its location is unusable, fall back to the
3174 : previous location. */
3175 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3176 :
3177 : /* Report the error. */
3178 13 : auto_diagnostic_group d;
3179 13 : error_at (cloc, msg);
3180 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3181 13 : return bad;
3182 13 : }
3183 29 : *non_constant_p = true;
3184 29 : return false;
3185 : }
3186 :
3187 : return true;
3188 : }
3189 :
3190 : /* Evaluate a call T to a GCC internal function when possible and return
3191 : the evaluated result or, under the control of CTX, give an error, set
3192 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3193 :
3194 : static tree
3195 370341 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3196 : value_cat lval,
3197 : bool *non_constant_p, bool *overflow_p,
3198 : tree *jump_target)
3199 : {
3200 370341 : enum tree_code opcode = ERROR_MARK;
3201 :
3202 370341 : switch (CALL_EXPR_IFN (t))
3203 : {
3204 179 : case IFN_UBSAN_NULL:
3205 179 : case IFN_UBSAN_BOUNDS:
3206 179 : case IFN_UBSAN_VPTR:
3207 179 : case IFN_FALLTHROUGH:
3208 179 : return void_node;
3209 :
3210 173449 : case IFN_ASSUME:
3211 173449 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3212 : G_("failed %<assume%> attribute assumption"),
3213 173449 : EXPR_LOCATION (t), /*eval*/false,
3214 : non_constant_p, overflow_p))
3215 : return t;
3216 173420 : return void_node;
3217 :
3218 : case IFN_ADD_OVERFLOW:
3219 : opcode = PLUS_EXPR;
3220 : break;
3221 364 : case IFN_SUB_OVERFLOW:
3222 364 : opcode = MINUS_EXPR;
3223 364 : break;
3224 193796 : case IFN_MUL_OVERFLOW:
3225 193796 : opcode = MULT_EXPR;
3226 193796 : break;
3227 :
3228 74 : case IFN_LAUNDER:
3229 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3230 : vc_prvalue, non_constant_p,
3231 74 : overflow_p, jump_target);
3232 :
3233 0 : case IFN_DEFERRED_INIT:
3234 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3235 :
3236 2011 : case IFN_VEC_CONVERT:
3237 2011 : {
3238 2011 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3239 : vc_prvalue, non_constant_p,
3240 : overflow_p, jump_target);
3241 2011 : if (*jump_target)
3242 : return NULL_TREE;
3243 2011 : if (TREE_CODE (arg) == VECTOR_CST)
3244 1912 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3245 : return r;
3246 : }
3247 : /* FALLTHRU */
3248 :
3249 104 : default:
3250 104 : if (!ctx->quiet)
3251 0 : error_at (cp_expr_loc_or_input_loc (t),
3252 : "call to internal function %qE", t);
3253 104 : *non_constant_p = true;
3254 104 : return t;
3255 : }
3256 :
3257 : /* Evaluate constant arguments using OPCODE and return a complex
3258 : number containing the result and the overflow bit. */
3259 194628 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3260 : non_constant_p, overflow_p,
3261 : jump_target);
3262 194628 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3263 : non_constant_p, overflow_p,
3264 : jump_target);
3265 194628 : if (*jump_target)
3266 : return NULL_TREE;
3267 194628 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3268 : {
3269 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3270 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3271 2 : tree result = fold_binary_loc (loc, opcode, type,
3272 : fold_convert_loc (loc, type, arg0),
3273 : fold_convert_loc (loc, type, arg1));
3274 2 : tree ovf
3275 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3276 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3277 2 : if (TREE_OVERFLOW (result))
3278 0 : TREE_OVERFLOW (result) = 0;
3279 :
3280 2 : return build_complex (TREE_TYPE (t), result, ovf);
3281 : }
3282 :
3283 194626 : *non_constant_p = true;
3284 194626 : return t;
3285 : }
3286 :
3287 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3288 :
3289 : static void
3290 4115027 : clear_no_implicit_zero (tree ctor)
3291 : {
3292 4115027 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3293 : {
3294 972675 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3295 4155979 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3296 1308016 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3297 400004 : clear_no_implicit_zero (e.value);
3298 : }
3299 4115027 : }
3300 :
3301 : /* Complain about a const object OBJ being modified in a constant expression.
3302 : EXPR is the MODIFY_EXPR expression performing the modification. */
3303 :
3304 : static void
3305 63 : modifying_const_object_error (tree expr, tree obj)
3306 : {
3307 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3308 63 : auto_diagnostic_group d;
3309 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3310 63 : "a constant expression", TREE_OPERAND (expr, 0));
3311 :
3312 : /* Find the underlying object that was declared as const. */
3313 63 : location_t decl_loc = UNKNOWN_LOCATION;
3314 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3315 75 : switch (TREE_CODE (probe))
3316 : {
3317 39 : case BIT_FIELD_REF:
3318 39 : case COMPONENT_REF:
3319 39 : {
3320 39 : tree elt = TREE_OPERAND (probe, 1);
3321 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3322 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3323 39 : probe = TREE_OPERAND (probe, 0);
3324 : }
3325 39 : break;
3326 :
3327 0 : case ARRAY_REF:
3328 0 : case REALPART_EXPR:
3329 0 : case IMAGPART_EXPR:
3330 0 : probe = TREE_OPERAND (probe, 0);
3331 0 : break;
3332 :
3333 36 : default:
3334 36 : decl_loc = location_of (probe);
3335 36 : break;
3336 : }
3337 63 : inform (decl_loc, "originally declared %<const%> here");
3338 63 : }
3339 :
3340 : /* Return true if FNDECL is a replaceable global allocation function that
3341 : should be useable during constant expression evaluation. */
3342 :
3343 : static inline bool
3344 32138687 : cxx_replaceable_global_alloc_fn (tree fndecl)
3345 : {
3346 32138687 : return (cxx_dialect >= cxx20
3347 30577423 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3348 2211202 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3349 34349499 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3350 1212767 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3351 : }
3352 :
3353 : /* Return true if FNDECL is a placement new function that should be
3354 : useable during constant expression evaluation of std::construct_at. */
3355 :
3356 : static inline bool
3357 30954655 : cxx_placement_new_fn (tree fndecl)
3358 : {
3359 30954655 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3360 : }
3361 :
3362 : /* Return true if FNDECL is std::construct_at. */
3363 :
3364 : static inline bool
3365 1416976 : is_std_construct_at (tree fndecl)
3366 : {
3367 1416976 : if (!decl_in_std_namespace_p (fndecl))
3368 : return false;
3369 :
3370 1390838 : tree name = DECL_NAME (fndecl);
3371 1390838 : return name && id_equal (name, "construct_at");
3372 : }
3373 :
3374 : /* Overload for the above taking constexpr_call*. */
3375 :
3376 : static inline bool
3377 1046145 : is_std_construct_at (const constexpr_call *call)
3378 : {
3379 1046145 : return (call
3380 773050 : && call->fundef
3381 1819195 : && is_std_construct_at (call->fundef->decl));
3382 : }
3383 :
3384 : /* True if CTX is an instance of std::NAME class. */
3385 :
3386 : bool
3387 13077028 : is_std_class (tree ctx, const char *name)
3388 : {
3389 13077028 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3390 : return false;
3391 :
3392 13076103 : tree decl = TYPE_MAIN_DECL (ctx);
3393 13076103 : tree dname = DECL_NAME (decl);
3394 13076103 : if (dname == NULL_TREE || !id_equal (dname, name))
3395 : return false;
3396 :
3397 553741 : return decl_in_std_namespace_p (decl);
3398 : }
3399 :
3400 : /* True if CTX is an instance of std::allocator. */
3401 :
3402 : bool
3403 404349 : is_std_allocator (tree ctx)
3404 : {
3405 404349 : return is_std_class (ctx, "allocator");
3406 : }
3407 :
3408 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3409 :
3410 : static bool
3411 418991 : is_std_allocator_allocate (tree fndecl)
3412 : {
3413 418991 : tree name = DECL_NAME (fndecl);
3414 418991 : if (name == NULL_TREE
3415 418991 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3416 : return false;
3417 :
3418 399709 : return is_std_allocator (DECL_CONTEXT (fndecl));
3419 : }
3420 :
3421 : /* Overload for the above taking constexpr_call*. */
3422 :
3423 : static inline bool
3424 245569 : is_std_allocator_allocate (const constexpr_call *call)
3425 : {
3426 245569 : return (call
3427 154054 : && call->fundef
3428 399623 : && is_std_allocator_allocate (call->fundef->decl));
3429 : }
3430 :
3431 : /* Return true if FNDECL is std::source_location::current. */
3432 :
3433 : static inline bool
3434 33614 : is_std_source_location_current (tree fndecl)
3435 : {
3436 33614 : if (!decl_in_std_namespace_p (fndecl))
3437 : return false;
3438 :
3439 24801 : tree name = DECL_NAME (fndecl);
3440 24801 : if (name == NULL_TREE || !id_equal (name, "current"))
3441 : return false;
3442 :
3443 159 : tree ctx = DECL_CONTEXT (fndecl);
3444 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3445 : return false;
3446 :
3447 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3448 159 : return name && id_equal (name, "source_location");
3449 : }
3450 :
3451 : /* Overload for the above taking constexpr_call*. */
3452 :
3453 : static inline bool
3454 42109 : is_std_source_location_current (const constexpr_call *call)
3455 : {
3456 42109 : return (call
3457 33614 : && call->fundef
3458 75723 : && is_std_source_location_current (call->fundef->decl));
3459 : }
3460 :
3461 : /* Return true if FNDECL is __dynamic_cast. */
3462 :
3463 : static inline bool
3464 30530546 : cxx_dynamic_cast_fn_p (tree fndecl)
3465 : {
3466 30530546 : return (cxx_dialect >= cxx20
3467 28969277 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3468 5361 : && CP_DECL_CONTEXT (fndecl) == abi_node
3469 : /* Only consider implementation-detail __dynamic_cast calls that
3470 : correspond to a dynamic_cast, and ignore direct calls to
3471 : abi::__dynamic_cast. */
3472 30535907 : && DECL_ARTIFICIAL (fndecl));
3473 : }
3474 :
3475 : /* Often, we have an expression in the form of address + offset, e.g.
3476 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3477 :
3478 : static tree
3479 4608 : extract_obj_from_addr_offset (tree expr)
3480 : {
3481 4608 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3482 2173 : expr = TREE_OPERAND (expr, 0);
3483 4608 : STRIP_NOPS (expr);
3484 4608 : if (TREE_CODE (expr) == ADDR_EXPR)
3485 4597 : expr = TREE_OPERAND (expr, 0);
3486 4608 : return expr;
3487 : }
3488 :
3489 : /* Given a PATH like
3490 :
3491 : g.D.2181.D.2154.D.2102.D.2093
3492 :
3493 : find a component with type TYPE. Return NULL_TREE if not found, and
3494 : error_mark_node if the component is not accessible. If STOP is non-null,
3495 : this function will return NULL_TREE if STOP is found before TYPE. */
3496 :
3497 : static tree
3498 2218 : get_component_with_type (tree path, tree type, tree stop)
3499 : {
3500 6416 : while (true)
3501 : {
3502 4317 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3503 : /* Found it. */
3504 : return path;
3505 3148 : else if (stop
3506 3148 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3507 : stop)))
3508 : return NULL_TREE;
3509 3103 : else if (TREE_CODE (path) == COMPONENT_REF
3510 3103 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3511 : {
3512 : /* We need to check that the component we're accessing is in fact
3513 : accessible. */
3514 3103 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3515 3103 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3516 1004 : return error_mark_node;
3517 2099 : path = TREE_OPERAND (path, 0);
3518 : }
3519 : else
3520 : return NULL_TREE;
3521 : }
3522 : }
3523 :
3524 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3525 :
3526 : The declaration of __dynamic_cast is:
3527 :
3528 : void* __dynamic_cast (const void* __src_ptr,
3529 : const __class_type_info* __src_type,
3530 : const __class_type_info* __dst_type,
3531 : ptrdiff_t __src2dst);
3532 :
3533 : where src2dst has the following possible values
3534 :
3535 : >-1: src_type is a unique public non-virtual base of dst_type
3536 : dst_ptr + src2dst == src_ptr
3537 : -1: unspecified relationship
3538 : -2: src_type is not a public base of dst_type
3539 : -3: src_type is a multiple public non-virtual base of dst_type */
3540 :
3541 : static tree
3542 2554 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3543 : bool *non_constant_p, bool *overflow_p,
3544 : tree *jump_target)
3545 : {
3546 : /* T will be something like
3547 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3548 : dismantle it. */
3549 2554 : gcc_assert (call_expr_nargs (call) == 4);
3550 2554 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3551 2554 : tree obj = CALL_EXPR_ARG (call, 0);
3552 2554 : tree type = CALL_EXPR_ARG (call, 2);
3553 2554 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3554 2554 : location_t loc = cp_expr_loc_or_input_loc (call);
3555 :
3556 : /* Get the target type of the dynamic_cast. */
3557 2554 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3558 2554 : type = TREE_OPERAND (type, 0);
3559 2554 : type = TREE_TYPE (DECL_NAME (type));
3560 :
3561 : /* TYPE can only be either T* or T&. We can't know which of these it
3562 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3563 : and something like "(T*)(T&)(T*) x" in the second case.
3564 : This is true for the reference cases in C++ < 26 or when exceptions
3565 : aren't enabled, in that case we should diagnose errors. For C++26
3566 : with exceptions we should silently evaluate to null pointer and
3567 : let the callers call __cxa_bad_cast () later to throw an exception. */
3568 2554 : bool fail_for_non_constant_p = false;
3569 11597 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3570 : {
3571 9043 : if (cxx_dialect < cxx26 || !flag_exceptions)
3572 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3573 9043 : obj = TREE_OPERAND (obj, 0);
3574 : }
3575 :
3576 : /* Evaluate the object so that we know its dynamic type. */
3577 2554 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3578 : overflow_p, jump_target);
3579 2554 : if (*non_constant_p)
3580 : return call;
3581 2435 : if (*jump_target)
3582 : return NULL_TREE;
3583 :
3584 : /* For dynamic_cast from classes with virtual bases we can get something
3585 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3586 : d.D.1234 using cxx_fold_indirect_ref. */
3587 2435 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3588 : {
3589 607 : tree objo = obj;
3590 607 : STRIP_NOPS (objo);
3591 607 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3592 : {
3593 576 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3594 : obj, NULL, jump_target);
3595 576 : if (objo)
3596 576 : obj = build_fold_addr_expr (objo);
3597 : }
3598 : }
3599 :
3600 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3601 : but when HINT is > 0, it can also be something like
3602 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3603 2435 : obj = extract_obj_from_addr_offset (obj);
3604 2435 : const tree objtype = TREE_TYPE (obj);
3605 : /* If OBJ doesn't refer to a base field, we're done. */
3606 4797 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3607 2435 : ? TREE_OPERAND (obj, 1) : obj))
3608 2435 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3609 : {
3610 73 : if (fail_for_non_constant_p)
3611 : {
3612 55 : if (!ctx->quiet)
3613 : {
3614 2 : auto_diagnostic_group d;
3615 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3616 2 : inform (loc, "dynamic type %qT of its operand does "
3617 : "not have a base class of type %qT",
3618 : objtype, type);
3619 2 : }
3620 55 : *non_constant_p = true;
3621 : }
3622 73 : return integer_zero_node;
3623 : }
3624 :
3625 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3626 : or in a destructor ... if the operand of the dynamic_cast refers
3627 : to the object under construction or destruction, this object is
3628 : considered to be a most derived object that has the type of the
3629 : constructor or destructor's class. */
3630 2362 : tree vtable = build_vfield_ref (obj, objtype);
3631 2362 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3632 : non_constant_p, overflow_p,
3633 : jump_target);
3634 2362 : if (*non_constant_p)
3635 : return call;
3636 2185 : if (*jump_target)
3637 : return NULL_TREE;
3638 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3639 : so it's possible that we got a null pointer now. */
3640 2185 : if (integer_zerop (vtable))
3641 : {
3642 12 : if (!ctx->quiet)
3643 3 : error_at (loc, "virtual table pointer is used uninitialized");
3644 12 : *non_constant_p = true;
3645 12 : return integer_zero_node;
3646 : }
3647 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3648 2173 : vtable = extract_obj_from_addr_offset (vtable);
3649 2173 : const tree mdtype = DECL_CONTEXT (vtable);
3650 :
3651 : /* Given dynamic_cast<T>(v),
3652 :
3653 : [expr.dynamic.cast] If C is the class type to which T points or refers,
3654 : the runtime check logically executes as follows:
3655 :
3656 : If, in the most derived object pointed (referred) to by v, v points
3657 : (refers) to a public base class subobject of a C object, and if only
3658 : one object of type C is derived from the subobject pointed (referred)
3659 : to by v the result points (refers) to that C object.
3660 :
3661 : In this case, HINT >= 0 or -3. */
3662 2173 : if (hint >= 0 || hint == -3)
3663 : {
3664 : /* Look for a component with type TYPE. */
3665 664 : tree t = get_component_with_type (obj, type, mdtype);
3666 : /* If not accessible, give an error. */
3667 664 : if (t == error_mark_node)
3668 : {
3669 198 : if (fail_for_non_constant_p)
3670 : {
3671 108 : if (!ctx->quiet)
3672 : {
3673 12 : auto_diagnostic_group d;
3674 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3675 12 : inform (loc, "static type %qT of its operand is a "
3676 : "non-public base class of dynamic type %qT",
3677 : objtype, type);
3678 :
3679 12 : }
3680 108 : *non_constant_p = true;
3681 : }
3682 198 : return integer_zero_node;
3683 : }
3684 466 : else if (t)
3685 : /* The result points to the TYPE object. */
3686 421 : return cp_build_addr_expr (t, complain);
3687 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3688 : Fall through to the normal processing. */
3689 : }
3690 :
3691 : /* Otherwise, if v points (refers) to a public base class subobject of the
3692 : most derived object, and the type of the most derived object has a base
3693 : class, of type C, that is unambiguous and public, the result points
3694 : (refers) to the C subobject of the most derived object.
3695 :
3696 : But it can also be an invalid case. */
3697 :
3698 : /* Get the most derived object. */
3699 1554 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3700 1554 : if (obj == error_mark_node)
3701 : {
3702 806 : if (fail_for_non_constant_p)
3703 : {
3704 350 : if (!ctx->quiet)
3705 : {
3706 38 : auto_diagnostic_group d;
3707 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3708 38 : inform (loc, "static type %qT of its operand is a non-public"
3709 : " base class of dynamic type %qT", objtype, mdtype);
3710 38 : }
3711 350 : *non_constant_p = true;
3712 : }
3713 806 : return integer_zero_node;
3714 : }
3715 : else
3716 748 : gcc_assert (obj);
3717 :
3718 : /* Check that the type of the most derived object has a base class
3719 : of type TYPE that is unambiguous and public. */
3720 748 : base_kind b_kind;
3721 748 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3722 748 : if (!binfo || binfo == error_mark_node)
3723 : {
3724 339 : if (fail_for_non_constant_p)
3725 : {
3726 134 : if (!ctx->quiet)
3727 : {
3728 16 : auto_diagnostic_group d;
3729 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3730 16 : if (b_kind == bk_ambig)
3731 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3732 : "type %qT of its operand", type, mdtype);
3733 : else
3734 10 : inform (loc, "dynamic type %qT of its operand does not "
3735 : "have an unambiguous public base class %qT",
3736 : mdtype, type);
3737 16 : }
3738 134 : *non_constant_p = true;
3739 : }
3740 339 : return integer_zero_node;
3741 : }
3742 : /* If so, return the TYPE subobject of the most derived object. */
3743 409 : obj = convert_to_base_statically (obj, binfo);
3744 409 : return cp_build_addr_expr (obj, complain);
3745 : }
3746 :
3747 : /* Data structure used by replace_decl and replace_decl_r. */
3748 :
3749 : struct replace_decl_data
3750 : {
3751 : /* The _DECL we want to replace. */
3752 : tree decl;
3753 : /* The replacement for DECL. */
3754 : tree replacement;
3755 : /* Trees we've visited. */
3756 : hash_set<tree> *pset;
3757 : /* Whether we've performed any replacements. */
3758 : bool changed;
3759 : };
3760 :
3761 : /* Helper function for replace_decl, called through cp_walk_tree. */
3762 :
3763 : static tree
3764 9567391 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3765 : {
3766 9567391 : replace_decl_data *d = (replace_decl_data *) data;
3767 :
3768 : /* We could be replacing
3769 : &<retval>.bar -> &foo.bar
3770 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3771 : on the ADDR_EXPR around it. */
3772 9567391 : if (TREE_CODE (*tp) == ADDR_EXPR)
3773 : {
3774 684765 : d->pset->add (*tp);
3775 684765 : auto save_changed = d->changed;
3776 684765 : d->changed = false;
3777 684765 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3778 684765 : if (d->changed)
3779 : {
3780 290 : cxx_mark_addressable (*tp);
3781 290 : recompute_tree_invariant_for_addr_expr (*tp);
3782 : }
3783 : else
3784 684475 : d->changed = save_changed;
3785 684765 : *walk_subtrees = 0;
3786 : }
3787 8882626 : else if (*tp == d->decl)
3788 : {
3789 513 : *tp = unshare_expr (d->replacement);
3790 513 : d->changed = true;
3791 513 : *walk_subtrees = 0;
3792 : }
3793 8882113 : else if (TYPE_P (*tp)
3794 8882113 : || d->pset->add (*tp))
3795 2367484 : *walk_subtrees = 0;
3796 :
3797 9567391 : return NULL_TREE;
3798 : }
3799 :
3800 : /* Replace every occurrence of DECL with (an unshared copy of)
3801 : REPLACEMENT within the expression *TP. Returns true iff a
3802 : replacement was performed. */
3803 :
3804 : bool
3805 1153670 : replace_decl (tree *tp, tree decl, tree replacement)
3806 : {
3807 1153670 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3808 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3809 1153670 : hash_set<tree> pset;
3810 1153670 : replace_decl_data data = { decl, replacement, &pset, false };
3811 1153670 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3812 1153670 : return data.changed;
3813 1153670 : }
3814 :
3815 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3816 :
3817 : static tree
3818 30 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3819 : value_cat lval,
3820 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3821 : {
3822 30 : tree function = THUNK_TARGET (thunk_fndecl);
3823 :
3824 30 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3825 : {
3826 13 : if (!ctx->quiet)
3827 : {
3828 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3829 : {
3830 0 : error ("call to non-%<constexpr%> function %qD", function);
3831 0 : explain_invalid_constexpr_fn (function);
3832 : }
3833 : else
3834 : /* virtual_offset is only set for virtual bases, which make the
3835 : class non-literal, so we don't need to handle it here. */
3836 3 : error ("calling constexpr member function %qD through virtual "
3837 : "base subobject", function);
3838 : }
3839 13 : *non_constant_p = true;
3840 13 : return t;
3841 : }
3842 :
3843 17 : tree new_call = copy_node (t);
3844 17 : CALL_EXPR_FN (new_call) = function;
3845 17 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3846 :
3847 17 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3848 :
3849 17 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3850 : {
3851 : /* 'this'-adjusting thunk. */
3852 11 : tree this_arg = CALL_EXPR_ARG (t, 0);
3853 11 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3854 : this_arg, offset);
3855 11 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3856 : }
3857 : else
3858 : /* Return-adjusting thunk. */
3859 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3860 : new_call, offset);
3861 :
3862 17 : return cxx_eval_constant_expression (ctx, new_call, lval,
3863 : non_constant_p, overflow_p,
3864 17 : jump_target);
3865 : }
3866 :
3867 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3868 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3869 : 'tors to detect modifying const objects in a constexpr context. */
3870 :
3871 : static void
3872 8329867 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3873 : bool readonly_p, bool *non_constant_p,
3874 : bool *overflow_p, tree *jump_target)
3875 : {
3876 16659734 : if (CLASS_TYPE_P (TREE_TYPE (object))
3877 16659734 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3878 : {
3879 : /* Subobjects might not be stored in ctx->global->values but we
3880 : can get its CONSTRUCTOR by evaluating *this. */
3881 1039267 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3882 : non_constant_p, overflow_p,
3883 : jump_target);
3884 1039267 : if (!*non_constant_p
3885 9289763 : && !throws (jump_target)
3886 1999163 : && TREE_CODE (e) == CONSTRUCTOR)
3887 959896 : TREE_READONLY (e) = readonly_p;
3888 : }
3889 8329867 : }
3890 :
3891 : /* Allocate an exception for OBJECT and throw it. */
3892 :
3893 : tree
3894 1567 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
3895 : tree object)
3896 : {
3897 1567 : tree type = TREE_TYPE (object);
3898 : /* This simulates a call to __cxa_allocate_exception. We need
3899 : (struct exception *) &heap -- memory on the heap so that
3900 : it can survive the stack being unwound. */
3901 1567 : tree arr = build_array_of_n_type (type, 1);
3902 1567 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
3903 1567 : DECL_NAME (var) = heap_identifier;
3904 1567 : ctx->global->put_value (var, NULL_TREE);
3905 :
3906 : /* *(struct exception *) &heap = exc{ ... } */
3907 1567 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
3908 1567 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
3909 1567 : bool non_constant_p = false, overflow_p = false;
3910 1567 : tree jump_target = NULL_TREE;
3911 1567 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
3912 : &overflow_p, &jump_target);
3913 1567 : if (non_constant_p)
3914 : {
3915 0 : if (!ctx->quiet)
3916 0 : error_at (loc, "couldn%'t throw %qT", type);
3917 0 : return NULL_TREE;
3918 : }
3919 :
3920 : /* Now we can __cxa_throw. */
3921 1567 : DECL_EXCEPTION_REFCOUNT (var)
3922 1567 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
3923 1567 : ++ctx->global->uncaught_exceptions;
3924 :
3925 1567 : return var;
3926 : }
3927 :
3928 : /* Subroutine of cxx_eval_constant_expression.
3929 : Evaluate the call expression tree T in the context of OLD_CALL expression
3930 : evaluation. */
3931 :
3932 : static tree
3933 147287123 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3934 : value_cat lval,
3935 : bool *non_constant_p, bool *overflow_p,
3936 : tree *jump_target)
3937 : {
3938 147287123 : location_t loc = cp_expr_loc_or_input_loc (t);
3939 147287123 : tree fun = get_function_named_in_call (t);
3940 :
3941 147287123 : if (fun == NULL_TREE)
3942 370341 : return cxx_eval_internal_function (ctx, t, lval,
3943 : non_constant_p, overflow_p,
3944 370341 : jump_target);
3945 :
3946 146916782 : if (TREE_CODE (fun) != FUNCTION_DECL)
3947 : {
3948 : /* Might be a constexpr function pointer. */
3949 231852 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3950 : non_constant_p, overflow_p,
3951 : jump_target);
3952 231852 : if (*jump_target)
3953 : return NULL_TREE;
3954 231852 : STRIP_NOPS (fun);
3955 231852 : if (TREE_CODE (fun) == ADDR_EXPR)
3956 24217 : fun = TREE_OPERAND (fun, 0);
3957 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3958 : indirection, the called expression is a pointer into the
3959 : virtual table which should contain FDESC_EXPR. Extract the
3960 : FUNCTION_DECL from there. */
3961 : else if (TARGET_VTABLE_USES_DESCRIPTORS
3962 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
3963 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
3964 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
3965 : {
3966 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
3967 : if (VAR_P (d)
3968 : && DECL_VTABLE_OR_VTT_P (d)
3969 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
3970 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
3971 : && DECL_INITIAL (d)
3972 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
3973 : {
3974 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
3975 : TYPE_SIZE_UNIT (vtable_entry_type));
3976 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
3977 : if (idx >= 0)
3978 : {
3979 : tree fdesc
3980 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
3981 : if (TREE_CODE (fdesc) == FDESC_EXPR
3982 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
3983 : fun = TREE_OPERAND (fdesc, 0);
3984 : }
3985 : }
3986 : }
3987 : }
3988 146916782 : if (TREE_CODE (fun) != FUNCTION_DECL)
3989 : {
3990 207635 : if (!ctx->quiet && !*non_constant_p)
3991 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
3992 : "function", fun);
3993 207635 : *non_constant_p = true;
3994 207635 : return t;
3995 : }
3996 146709147 : tree orig_fun = fun;
3997 146709147 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
3998 20704587 : fun = DECL_CLONED_FUNCTION (fun);
3999 :
4000 146709147 : if (is_ubsan_builtin_p (fun))
4001 58 : return void_node;
4002 :
4003 146709089 : if (fndecl_built_in_p (fun))
4004 14321834 : return cxx_eval_builtin_function_call (ctx, t, fun,
4005 : lval, non_constant_p, overflow_p,
4006 14321833 : jump_target);
4007 132387255 : if (DECL_THUNK_P (fun))
4008 30 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
4009 30 : jump_target);
4010 132387225 : if (metafunction_p (fun))
4011 : {
4012 : /* To be able to evaluate a metafunction, we may have to instantiate
4013 : constexpr functions. If we're not allowed to instantiate, leave
4014 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4015 : otherwise we might fold those prematurely. See
4016 : g++.dg/reflect/p2996-17.C. */
4017 36539 : if (uid_sensitive_constexpr_evaluation_p ()
4018 36261 : || ctx->manifestly_const_eval == mce_unknown)
4019 : {
4020 9859 : *non_constant_p = true;
4021 9859 : return t;
4022 : }
4023 26680 : ctx->global->metafns_called = true;
4024 26680 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4025 : jump_target);
4026 26680 : if (*jump_target)
4027 : return NULL_TREE;
4028 25107 : if (*non_constant_p)
4029 : return t;
4030 24959 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4031 : non_constant_p, overflow_p,
4032 : jump_target);
4033 24959 : if (*jump_target)
4034 : return NULL_TREE;
4035 24959 : if (*non_constant_p)
4036 : return t;
4037 : return e;
4038 : }
4039 132350686 : bool non_constexpr_call = false;
4040 132350686 : if (!maybe_constexpr_fn (fun))
4041 : {
4042 847249 : if (TREE_CODE (t) == CALL_EXPR
4043 840730 : && cxx_replaceable_global_alloc_fn (fun)
4044 1323473 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4045 162907 : || is_std_allocator_allocate (ctx->call)))
4046 : {
4047 393204 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4048 393204 : const int nargs = call_expr_nargs (t);
4049 393204 : tree arg0 = NULL_TREE;
4050 676262 : for (int i = 0; i < nargs; ++i)
4051 : {
4052 394067 : tree arg = CALL_EXPR_ARG (t, i);
4053 394067 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4054 : non_constant_p, overflow_p,
4055 : jump_target);
4056 394067 : if (*jump_target)
4057 : return NULL_TREE;
4058 : /* Deleting a non-constant pointer has a better error message
4059 : below. */
4060 394059 : if (new_op_p || i != 0)
4061 353980 : VERIFY_CONSTANT (arg);
4062 242979 : if (i == 0)
4063 : arg0 = arg;
4064 : }
4065 282195 : gcc_assert (arg0);
4066 282195 : if (new_op_p)
4067 : {
4068 242116 : if (!tree_fits_uhwi_p (arg0))
4069 : {
4070 : /* We should not get here; the VERIFY_CONSTANT above
4071 : should have already caught it. */
4072 0 : gcc_checking_assert (false);
4073 : if (!ctx->quiet)
4074 : error_at (loc, "cannot allocate array: size not constant");
4075 : *non_constant_p = true;
4076 : return t;
4077 : }
4078 484232 : tree type = build_array_type_nelts (char_type_node,
4079 242116 : tree_to_uhwi (arg0));
4080 242116 : tree var = build_decl (loc, VAR_DECL,
4081 242116 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4082 : & OVL_OP_FLAG_VEC)
4083 : ? heap_vec_uninit_identifier
4084 : : heap_uninit_identifier,
4085 242116 : type);
4086 242116 : DECL_ARTIFICIAL (var) = 1;
4087 242116 : ctx->global->heap_vars.safe_push (var);
4088 242116 : ctx->global->put_value (var, NULL_TREE);
4089 242116 : return fold_convert (ptr_type_node, build_address (var));
4090 : }
4091 : else
4092 : {
4093 40079 : STRIP_NOPS (arg0);
4094 40079 : if (TREE_CODE (arg0) == ADDR_EXPR
4095 40079 : && VAR_P (TREE_OPERAND (arg0, 0)))
4096 : {
4097 40079 : tree var = TREE_OPERAND (arg0, 0);
4098 40079 : if (DECL_NAME (var) == heap_uninit_identifier
4099 40079 : || DECL_NAME (var) == heap_identifier)
4100 : {
4101 39913 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4102 : & OVL_OP_FLAG_VEC)
4103 : {
4104 9 : if (!ctx->quiet)
4105 : {
4106 3 : auto_diagnostic_group d;
4107 3 : error_at (loc, "array deallocation of object "
4108 : "allocated with non-array "
4109 : "allocation");
4110 3 : inform (DECL_SOURCE_LOCATION (var),
4111 : "allocation performed here");
4112 3 : }
4113 9 : *non_constant_p = true;
4114 9 : return t;
4115 : }
4116 39904 : DECL_NAME (var) = heap_deleted_identifier;
4117 39904 : ctx->global->destroy_value (var);
4118 39904 : ctx->global->heap_dealloc_count++;
4119 39904 : return void_node;
4120 : }
4121 166 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4122 166 : || DECL_NAME (var) == heap_vec_identifier)
4123 : {
4124 142 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4125 : & OVL_OP_FLAG_VEC) == 0)
4126 : {
4127 9 : if (!ctx->quiet)
4128 : {
4129 3 : auto_diagnostic_group d;
4130 3 : error_at (loc, "non-array deallocation of "
4131 : "object allocated with array "
4132 : "allocation");
4133 3 : inform (DECL_SOURCE_LOCATION (var),
4134 : "allocation performed here");
4135 3 : }
4136 9 : *non_constant_p = true;
4137 9 : return t;
4138 : }
4139 133 : DECL_NAME (var) = heap_deleted_identifier;
4140 133 : ctx->global->destroy_value (var);
4141 133 : ctx->global->heap_dealloc_count++;
4142 133 : return void_node;
4143 : }
4144 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4145 : {
4146 15 : if (!ctx->quiet)
4147 6 : error_at (loc, "deallocation of already deallocated "
4148 : "storage");
4149 15 : *non_constant_p = true;
4150 15 : return t;
4151 : }
4152 : }
4153 9 : if (!ctx->quiet)
4154 3 : error_at (loc, "deallocation of storage that was "
4155 : "not previously allocated");
4156 9 : *non_constant_p = true;
4157 9 : return t;
4158 : }
4159 : }
4160 : /* Allow placement new in std::construct_at, just return the second
4161 : argument. */
4162 454045 : if (TREE_CODE (t) == CALL_EXPR
4163 447526 : && cxx_placement_new_fn (fun)
4164 749676 : && is_std_construct_at (ctx->call))
4165 : {
4166 30992 : const int nargs = call_expr_nargs (t);
4167 30992 : tree arg1 = NULL_TREE;
4168 92976 : for (int i = 0; i < nargs; ++i)
4169 : {
4170 61984 : tree arg = CALL_EXPR_ARG (t, i);
4171 61984 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4172 : non_constant_p, overflow_p,
4173 : jump_target);
4174 61984 : if (*jump_target)
4175 : return NULL_TREE;
4176 61984 : if (i == 1)
4177 : arg1 = arg;
4178 : else
4179 61984 : VERIFY_CONSTANT (arg);
4180 : }
4181 30992 : gcc_assert (arg1);
4182 : return arg1;
4183 : }
4184 423053 : else if (cxx_dynamic_cast_fn_p (fun))
4185 2554 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4186 2554 : jump_target);
4187 420499 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4188 21741 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4189 : non_constant_p, overflow_p,
4190 21741 : jump_target);
4191 :
4192 : /* Calls to non-constexpr functions can be diagnosed right away
4193 : before C++26, though in C++26 evaluation of the arguments might
4194 : throw and if caught it could be still constant expression.
4195 : So for C++26 this is diagnosed only after
4196 : cxx_bind_parameters_in_call. */
4197 398758 : if (cxx_dialect >= cxx26)
4198 : non_constexpr_call = true;
4199 : else
4200 : {
4201 345991 : if (!ctx->quiet)
4202 : {
4203 193 : if (!lambda_static_thunk_p (fun))
4204 193 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4205 193 : explain_invalid_constexpr_fn (fun);
4206 : }
4207 345991 : *non_constant_p = true;
4208 345991 : return t;
4209 : }
4210 : }
4211 :
4212 131556204 : constexpr_ctx new_ctx = *ctx;
4213 131556204 : ctx = &new_ctx;
4214 151402668 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4215 134561258 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4216 : {
4217 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4218 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4219 1863 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4220 1863 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4221 1863 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4222 1863 : ctx->global->put_value (new_ctx.object, ctor);
4223 : }
4224 : /* An immediate invocation is manifestly constant evaluated including the
4225 : arguments of the call, so use mce_true even for the argument
4226 : evaluation. */
4227 263112408 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4228 2918624 : new_ctx.manifestly_const_eval = mce_true;
4229 :
4230 : /* We used to shortcut trivial constructor/op= here, but nowadays
4231 : we can only get a trivial function here with -fno-elide-constructors. */
4232 131556244 : gcc_checking_assert (!trivial_fn_p (fun)
4233 : || !flag_elide_constructors
4234 : /* Or it's a call from maybe_thunk_body (111075). */
4235 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4236 : : AGGR_INIT_FROM_THUNK_P (t))
4237 : /* We don't elide constructors when processing
4238 : a noexcept-expression. */
4239 : || cp_noexcept_operand);
4240 :
4241 131556204 : bool non_constant_args = false;
4242 131556204 : constexpr_call new_call;
4243 131556204 : new_call.bindings
4244 131556204 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4245 : overflow_p, &non_constant_args,
4246 : jump_target);
4247 :
4248 : /* We build up the bindings list before we know whether we already have this
4249 : call cached. If we don't end up saving these bindings, ggc_free them when
4250 : this function exits. */
4251 131556204 : class free_bindings
4252 : {
4253 : tree *bindings;
4254 : public:
4255 131556204 : free_bindings (tree &b): bindings (&b) { }
4256 131556202 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4257 4266230 : void preserve () { bindings = NULL; }
4258 131556204 : } fb (new_call.bindings);
4259 :
4260 131556204 : if (*jump_target)
4261 : return NULL_TREE;
4262 131556163 : if (*non_constant_p)
4263 : return t;
4264 63774877 : if (non_constexpr_call)
4265 : {
4266 4353 : if (!ctx->quiet)
4267 : {
4268 219 : if (!lambda_static_thunk_p (fun))
4269 219 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4270 219 : explain_invalid_constexpr_fn (fun);
4271 : }
4272 4353 : *non_constant_p = true;
4273 4353 : return t;
4274 : }
4275 :
4276 : /* We can't defer instantiating the function any longer. */
4277 63770524 : if (!DECL_INITIAL (fun)
4278 2699258 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4279 63770524 : && !uid_sensitive_constexpr_evaluation_p ())
4280 : {
4281 2446710 : location_t save_loc = input_location;
4282 2446710 : input_location = loc;
4283 2446710 : ++function_depth;
4284 2446710 : if (ctx->manifestly_const_eval == mce_true)
4285 269658 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4286 2446710 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4287 2446703 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4288 : else
4289 7 : synthesize_method (fun);
4290 2446710 : --function_depth;
4291 2446710 : input_location = save_loc;
4292 : }
4293 :
4294 : /* If in direct recursive call, optimize definition search. */
4295 63770524 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4296 27557 : new_call.fundef = ctx->call->fundef;
4297 : else
4298 : {
4299 63742967 : new_call.fundef = retrieve_constexpr_fundef (fun);
4300 63742967 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4301 63237431 : || new_call.fundef->result == error_mark_node
4302 63219390 : || fun == current_function_decl)
4303 : {
4304 523580 : if (!ctx->quiet)
4305 : {
4306 : /* We need to check for current_function_decl here in case we're
4307 : being called during cp_fold_function, because at that point
4308 : DECL_INITIAL is set properly and we have a fundef but we
4309 : haven't lowered invisirefs yet (c++/70344). */
4310 170 : if (DECL_INITIAL (fun) == error_mark_node
4311 170 : || fun == current_function_decl)
4312 15 : error_at (loc, "%qD called in a constant expression before its "
4313 : "definition is complete", fun);
4314 155 : else if (DECL_INITIAL (fun))
4315 : {
4316 : /* The definition of fun was somehow unsuitable. But pretend
4317 : that lambda static thunks don't exist. */
4318 118 : if (!lambda_static_thunk_p (fun))
4319 112 : error_at (loc, "%qD called in a constant expression", fun);
4320 118 : explain_invalid_constexpr_fn (fun);
4321 : }
4322 : else
4323 37 : error_at (loc, "%qD used before its definition", fun);
4324 : }
4325 523580 : *non_constant_p = true;
4326 523580 : return t;
4327 : }
4328 : }
4329 :
4330 : /* Don't complain about problems evaluating an ill-formed function. */
4331 63246944 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4332 63246944 : if (f->language->erroneous)
4333 677 : new_ctx.quiet = true;
4334 :
4335 63246944 : int depth_ok = push_cx_call_context (t);
4336 :
4337 : /* Remember the object we are constructing or destructing. */
4338 63246944 : tree new_obj = NULL_TREE;
4339 126493888 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4340 : {
4341 : /* In a cdtor, it should be the first `this' argument.
4342 : At this point it has already been evaluated in the call
4343 : to cxx_bind_parameters_in_call. */
4344 10221153 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4345 10221153 : bool empty_base = false;
4346 10221153 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4347 : &empty_base, jump_target);
4348 10221153 : if (*jump_target)
4349 0 : return NULL_TREE;
4350 : /* If we're initializing an empty class, don't set constness, because
4351 : cxx_fold_indirect_ref will return the wrong object to set constness
4352 : of. */
4353 10221153 : if (empty_base)
4354 : new_obj = NULL_TREE;
4355 4190232 : else if (ctx->call && ctx->call->fundef
4356 16751055 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4357 : {
4358 2400108 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4359 2400108 : STRIP_NOPS (cur_obj);
4360 2400108 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4361 2398936 : cur_obj = TREE_OPERAND (cur_obj, 0);
4362 2400108 : if (new_obj == cur_obj)
4363 : /* We're calling the target constructor of a delegating
4364 : constructor, or accessing a base subobject through a
4365 : NOP_EXPR as part of a call to a base constructor, so
4366 : there is no new (sub)object. */
4367 1891272 : new_obj = NULL_TREE;
4368 : }
4369 : }
4370 :
4371 63246944 : tree result = NULL_TREE;
4372 :
4373 63246944 : constexpr_call *entry = NULL;
4374 63246944 : if (depth_ok && !non_constant_args && ctx->strict)
4375 : {
4376 25714940 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4377 25714940 : new_call.hash
4378 25714940 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4379 :
4380 : /* If we have seen this call before, we are done. */
4381 25714940 : maybe_initialize_constexpr_call_table ();
4382 25714940 : bool insert = depth_ok < constexpr_cache_depth;
4383 25714940 : constexpr_call **slot
4384 30801233 : = constexpr_call_table->find_slot (&new_call,
4385 : insert ? INSERT : NO_INSERT);
4386 25714940 : entry = slot ? *slot : NULL;
4387 24689004 : if (entry == NULL)
4388 : {
4389 : /* Only cache up to constexpr_cache_depth to limit memory use. */
4390 5292166 : if (insert)
4391 : {
4392 : /* We need to keep a pointer to the entry, not just the slot, as
4393 : the slot can move during evaluation of the body. */
4394 4266230 : *slot = entry = ggc_alloc<constexpr_call> ();
4395 4266230 : *entry = new_call;
4396 4266230 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4397 4266230 : fb.preserve ();
4398 :
4399 : /* Unshare args going into the hash table to separate them
4400 : from the caller's context, for better GC and to avoid
4401 : problems with verify_gimple. */
4402 4266230 : tree bound = new_call.bindings;
4403 6932902 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4404 : {
4405 2666672 : tree &arg = TREE_VEC_ELT (bound, i);
4406 2666672 : arg = unshare_expr_without_location (arg);
4407 : }
4408 : }
4409 : }
4410 : /* Calls that are in progress have their result set to unknown_type_node,
4411 : so that we can detect circular dependencies. Now that we only cache
4412 : up to constexpr_cache_depth this won't catch circular dependencies that
4413 : start deeper, but they'll hit the recursion or ops limit. */
4414 20422774 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4415 : {
4416 6 : if (!ctx->quiet)
4417 0 : error ("call has circular dependency");
4418 6 : *non_constant_p = true;
4419 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4420 : }
4421 : else
4422 20422768 : result = entry->result (ctx->manifestly_const_eval);
4423 : }
4424 :
4425 63246944 : if (!depth_ok)
4426 : {
4427 30 : if (!ctx->quiet)
4428 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4429 : "%<-fconstexpr-depth=%> to increase the maximum)",
4430 : max_constexpr_depth);
4431 30 : *non_constant_p = true;
4432 30 : result = error_mark_node;
4433 : }
4434 : else
4435 : {
4436 63246914 : bool cacheable = !!entry;
4437 63246914 : if (result && result != error_mark_node)
4438 : /* OK */;
4439 47757076 : else if (!DECL_SAVED_TREE (fun))
4440 : {
4441 : /* When at_eof >= 3, cgraph has started throwing away
4442 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4443 : late code generation for VEC_INIT_EXPR, which needs to be
4444 : completely reconsidered. */
4445 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4446 0 : *non_constant_p = true;
4447 : }
4448 47757076 : else if (tree copy = get_fundef_copy (new_call.fundef))
4449 : {
4450 47757070 : tree body, parms, res;
4451 47757070 : releasing_vec ctors;
4452 :
4453 : /* Reuse or create a new unshared copy of this function's body. */
4454 47757070 : body = TREE_PURPOSE (copy);
4455 47757070 : parms = TREE_VALUE (copy);
4456 47757070 : res = TREE_TYPE (copy);
4457 :
4458 : /* Associate the bindings with the remapped parms. */
4459 47757070 : tree bound = new_call.bindings;
4460 47757070 : tree remapped = parms;
4461 112029014 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4462 : {
4463 64271944 : tree arg = TREE_VEC_ELT (bound, i);
4464 64271944 : if (entry)
4465 : {
4466 : /* Unshare again so the callee doesn't change the
4467 : argument values in the hash table. XXX Could we unshare
4468 : lazily in cxx_eval_store_expression? */
4469 2951927 : arg = unshare_constructor (arg);
4470 2951927 : if (TREE_CODE (arg) == CONSTRUCTOR)
4471 812598 : vec_safe_push (ctors, arg);
4472 : }
4473 64271944 : ctx->global->put_value (remapped, arg);
4474 64271944 : remapped = DECL_CHAIN (remapped);
4475 : }
4476 47757070 : if (remapped)
4477 : {
4478 : /* We shouldn't have any parms without args, but fail gracefully
4479 : in error recovery. */
4480 6 : gcc_checking_assert (seen_error ());
4481 6 : *non_constant_p = true;
4482 : }
4483 : /* Add the RESULT_DECL to the values map, too. */
4484 47757070 : gcc_assert (!DECL_BY_REFERENCE (res));
4485 47757070 : ctx->global->put_value (res, NULL_TREE);
4486 :
4487 : /* Remember the current call we're evaluating. */
4488 47757070 : constexpr_ctx call_ctx = *ctx;
4489 47757070 : call_ctx.call = &new_call;
4490 47757070 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4491 47757070 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4492 47757070 : bool save_metafns_called = ctx->global->metafns_called;
4493 :
4494 : /* Make sure we fold std::is_constant_evaluated to true in an
4495 : immediate function. */
4496 95514140 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4497 1882667 : call_ctx.manifestly_const_eval = mce_true;
4498 :
4499 : /* If this is a constexpr destructor, the object's const and volatile
4500 : semantics are no longer in effect; see [class.dtor]p5. */
4501 56086937 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4502 663782 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4503 : non_constant_p, overflow_p, jump_target);
4504 :
4505 : /* If this is a constructor, we are beginning the lifetime of the
4506 : object we are initializing. */
4507 47757070 : if (new_obj
4508 16659734 : && DECL_CONSTRUCTOR_P (fun)
4509 7666085 : && TREE_CODE (new_obj) == COMPONENT_REF
4510 49804905 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4511 : {
4512 4711 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4513 4711 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4514 4711 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4515 : new_obj, ctor);
4516 4711 : cxx_eval_constant_expression (ctx, activate,
4517 : lval, non_constant_p, overflow_p,
4518 : jump_target);
4519 4711 : ggc_free (activate);
4520 4711 : if (*jump_target)
4521 0 : return NULL_TREE;
4522 : }
4523 :
4524 47757070 : ctx->global->metafns_called = false;
4525 :
4526 47757070 : tree jmp_target = NULL_TREE;
4527 47757070 : cxx_eval_constant_expression (&call_ctx, body,
4528 : vc_discard, non_constant_p, overflow_p,
4529 : &jmp_target);
4530 :
4531 47757068 : if (!*non_constant_p && throws (&jmp_target))
4532 : {
4533 1383 : result = NULL_TREE;
4534 1383 : cacheable = false;
4535 1383 : *jump_target = jmp_target;
4536 : }
4537 95511370 : else if (DECL_CONSTRUCTOR_P (fun))
4538 : /* This can be null for a subobject constructor call, in
4539 : which case what we care about is the initialization
4540 : side-effects rather than the value. We could get at the
4541 : value by evaluating *this, but we don't bother; there's
4542 : no need to put such a call in the hash table. */
4543 9473673 : result = lval ? ctx->object : ctx->ctor;
4544 38282012 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4545 3201604 : result = void_node;
4546 : else
4547 : {
4548 35080408 : result = ctx->global->get_value (res);
4549 9901369 : if (result == NULL_TREE && !*non_constant_p
4550 35080618 : && !DECL_DESTRUCTOR_P (fun))
4551 : {
4552 105 : if (!ctx->quiet)
4553 6 : error ("%<constexpr%> call flows off the end "
4554 : "of the function");
4555 105 : *non_constant_p = true;
4556 : }
4557 : }
4558 :
4559 47757068 : if (ctx->global->metafns_called)
4560 29423 : cacheable = false;
4561 47757068 : ctx->global->metafns_called |= save_metafns_called;
4562 :
4563 : /* At this point, the object's constructor will have run, so
4564 : the object is no longer under construction, and its possible
4565 : 'const' semantics now apply. Make a note of this fact by
4566 : marking the CONSTRUCTOR TREE_READONLY. */
4567 56086935 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4568 7666085 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4569 : non_constant_p, overflow_p, jump_target);
4570 :
4571 : /* Remove the parms/result from the values map. */
4572 47757068 : destroy_value_checked (ctx, res, non_constant_p);
4573 112029017 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4574 64271949 : destroy_value_checked (ctx, parm, non_constant_p);
4575 :
4576 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4577 48569666 : while (!ctors->is_empty ())
4578 : {
4579 812598 : tree c = ctors->pop ();
4580 812598 : if (c != result)
4581 812598 : free_constructor (c);
4582 : }
4583 :
4584 : /* Make the unshared function copy we used available for re-use. */
4585 47757068 : save_fundef_copy (fun, copy);
4586 :
4587 : /* If the call allocated some heap object that hasn't been
4588 : deallocated during the call, or if it deallocated some heap
4589 : object it has not allocated, the call isn't really stateless
4590 : for the constexpr evaluation and should not be cached.
4591 : It is fine if the call allocates something and deallocates it
4592 : too. */
4593 47757068 : if (cacheable
4594 56953917 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4595 9194231 : || (save_heap_dealloc_count
4596 9194231 : != ctx->global->heap_dealloc_count)))
4597 : {
4598 2618 : tree heap_var;
4599 2618 : unsigned int i;
4600 2618 : if ((ctx->global->heap_vars.length ()
4601 2618 : - ctx->global->heap_dealloc_count)
4602 2618 : != save_heap_alloc_count - save_heap_dealloc_count)
4603 : cacheable = false;
4604 : else
4605 69999 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4606 : save_heap_alloc_count)
4607 68429 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4608 : {
4609 : cacheable = false;
4610 : break;
4611 : }
4612 : }
4613 :
4614 : /* Rewrite all occurrences of the function's RESULT_DECL with the
4615 : current object under construction. */
4616 47757068 : if (!*non_constant_p
4617 33927826 : && ctx->object
4618 9658005 : && CLASS_TYPE_P (TREE_TYPE (res))
4619 49706289 : && !is_empty_class (TREE_TYPE (res)))
4620 : {
4621 1153544 : if (!same_type_ignoring_top_level_qualifiers_p
4622 1153544 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4623 0 : *non_constant_p = true;
4624 1153544 : else if (replace_decl (&result, res, ctx->object))
4625 : {
4626 238 : cacheable = false;
4627 238 : result = cxx_eval_constant_expression (ctx, result, lval,
4628 : non_constant_p,
4629 : overflow_p,
4630 : jump_target);
4631 238 : if (*jump_target)
4632 : {
4633 0 : cacheable = false;
4634 0 : result = NULL_TREE;
4635 : }
4636 : }
4637 : }
4638 :
4639 : /* Only cache a permitted result of a constant expression. */
4640 47756830 : if (cacheable && !reduced_constant_expression_p (result))
4641 : cacheable = false;
4642 47757068 : }
4643 : else
4644 : /* Couldn't get a function copy to evaluate. */
4645 6 : *non_constant_p = true;
4646 :
4647 63246912 : if (result == error_mark_node)
4648 0 : *non_constant_p = true;
4649 63246912 : if (*non_constant_p || *overflow_p)
4650 13829404 : result = error_mark_node;
4651 49417508 : else if (!result)
4652 2974024 : result = void_node;
4653 63246912 : if (entry)
4654 : {
4655 49378006 : entry->result (ctx->manifestly_const_eval)
4656 24689003 : = cacheable ? result : error_mark_node;
4657 :
4658 24689003 : if (result != error_mark_node
4659 19826726 : && ctx->manifestly_const_eval == mce_unknown)
4660 : {
4661 : /* Evaluation succeeded and was independent of whether we're in a
4662 : manifestly constant-evaluated context, so we can also reuse
4663 : this result when evaluating this call with a fixed context. */
4664 1883782 : if (!entry->result (mce_true))
4665 724694 : entry->result (mce_true) = entry->result (mce_unknown);
4666 1883782 : if (!entry->result (mce_false))
4667 723662 : entry->result (mce_false) = entry->result (mce_unknown);
4668 : }
4669 : }
4670 : }
4671 :
4672 : /* The result of a constexpr function must be completely initialized.
4673 :
4674 : However, in C++20, a constexpr constructor doesn't necessarily have
4675 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4676 : in order to detect reading an unitialized object in constexpr instead
4677 : of value-initializing it. (reduced_constant_expression_p is expected to
4678 : take care of clearing the flag.) */
4679 63246942 : if (TREE_CODE (result) == CONSTRUCTOR
4680 63246942 : && (cxx_dialect < cxx20
4681 15213342 : || !DECL_CONSTRUCTOR_P (fun)))
4682 3715023 : clear_no_implicit_zero (result);
4683 :
4684 63246942 : pop_cx_call_context ();
4685 63246942 : return result;
4686 131556202 : }
4687 :
4688 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4689 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4690 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4691 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4692 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4693 :
4694 : bool
4695 1088367502 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4696 : {
4697 1088367502 : if (t == NULL_TREE)
4698 : return false;
4699 :
4700 1083508740 : switch (TREE_CODE (t))
4701 : {
4702 : case PTRMEM_CST:
4703 : case REFLECT_EXPR:
4704 : /* Even if we can't lower this yet, it's constant. */
4705 : return true;
4706 :
4707 : case OMP_DECLARE_MAPPER:
4708 : return true;
4709 :
4710 58797470 : case CONSTRUCTOR:
4711 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4712 58797470 : tree field;
4713 58797470 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4714 : /* A constant vector would be folded to VECTOR_CST.
4715 : A CONSTRUCTOR of scalar type means uninitialized. */
4716 : return false;
4717 58781499 : if (CONSTRUCTOR_NO_CLEARING (t))
4718 : {
4719 3249208 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4720 : {
4721 : /* There must be a valid constant initializer at every array
4722 : index. */
4723 1993 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4724 1993 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4725 1993 : tree cursor = min;
4726 27787 : for (auto &e: CONSTRUCTOR_ELTS (t))
4727 : {
4728 21918 : if (!reduced_constant_expression_p (e.value))
4729 : return false;
4730 21868 : if (array_index_cmp (cursor, e.index) != 0)
4731 : return false;
4732 21808 : if (TREE_CODE (e.index) == RANGE_EXPR)
4733 0 : cursor = TREE_OPERAND (e.index, 1);
4734 21808 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4735 0 : cursor
4736 0 : = int_const_binop (PLUS_EXPR, cursor,
4737 0 : size_int (RAW_DATA_LENGTH (e.value)));
4738 : else
4739 21808 : cursor = int_const_binop (PLUS_EXPR, cursor,
4740 21808 : size_one_node);
4741 : }
4742 1883 : if (find_array_ctor_elt (t, max) == -1)
4743 : return false;
4744 1795 : goto ok;
4745 : }
4746 3247215 : else if (cxx_dialect >= cxx20
4747 3247215 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4748 : {
4749 6046610 : if (CONSTRUCTOR_NELTS (t) == 0)
4750 : /* An initialized union has a constructor element. */
4751 : return false;
4752 : /* And it only initializes one member. */
4753 : field = NULL_TREE;
4754 : }
4755 : else
4756 3245247 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4757 : }
4758 : else
4759 : field = NULL_TREE;
4760 559020559 : for (auto &e: CONSTRUCTOR_ELTS (t))
4761 : {
4762 : /* If VAL is null, we're in the middle of initializing this
4763 : element. */
4764 439865315 : if (!reduced_constant_expression_p (e.value,
4765 439865315 : (e.index
4766 439865297 : && (TREE_CODE (e.index)
4767 : == FIELD_DECL))
4768 49941652 : ? DECL_SIZE (e.index)
4769 : : NULL_TREE))
4770 : return false;
4771 : /* We want to remove initializers for empty fields in a struct to
4772 : avoid confusing output_constructor. */
4773 439219542 : if (is_empty_field (e.index)
4774 439219542 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4775 : return false;
4776 : /* Check for non-empty fields between initialized fields when
4777 : CONSTRUCTOR_NO_CLEARING. */
4778 439066750 : for (; field && e.index != field;
4779 363142 : field = next_subobject_field (DECL_CHAIN (field)))
4780 366649 : if (!is_really_empty_class (TREE_TYPE (field),
4781 : /*ignore_vptr*/false))
4782 : return false;
4783 438700101 : if (field)
4784 3639516 : field = next_subobject_field (DECL_CHAIN (field));
4785 : }
4786 : /* There could be a non-empty field at the end. */
4787 57885911 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4788 278338 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4789 : {
4790 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4791 : the parent FIELD_DECL (if any) for classes with virtual
4792 : bases. */
4793 4751 : if (cxx_dialect >= cxx26
4794 2578 : && sz
4795 5132 : && tree_int_cst_le (sz, bit_position (field)))
4796 : break;
4797 4497 : return false;
4798 : }
4799 57607573 : ok:
4800 57609622 : if (CONSTRUCTOR_NO_CLEARING (t))
4801 : /* All the fields are initialized. */
4802 3066782 : CONSTRUCTOR_NO_CLEARING (t) = false;
4803 : return true;
4804 :
4805 1024516306 : default:
4806 : /* FIXME are we calling this too much? */
4807 1024516306 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4808 : }
4809 : }
4810 :
4811 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4812 : why and suggest what could be done about it. */
4813 :
4814 : static tree
4815 989 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4816 : {
4817 989 : bool ref_p = false;
4818 :
4819 : /* No need to look into types or unevaluated operands. */
4820 989 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4821 : {
4822 0 : *walk_subtrees = false;
4823 0 : return NULL_TREE;
4824 : }
4825 :
4826 989 : switch (TREE_CODE (*tp))
4827 : {
4828 81 : CASE_CONVERT:
4829 81 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4830 : break;
4831 62 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4832 62 : *tp = TREE_OPERAND (*tp, 0);
4833 194 : gcc_fallthrough ();
4834 194 : case ADDR_EXPR:
4835 194 : {
4836 194 : tree op = TREE_OPERAND (*tp, 0);
4837 194 : if (VAR_P (op)
4838 111 : && DECL_DECLARED_CONSTEXPR_P (op)
4839 39 : && !TREE_STATIC (op)
4840 : /* ??? We should also say something about temporaries. */
4841 230 : && !DECL_ARTIFICIAL (op))
4842 : {
4843 33 : if (ref_p)
4844 12 : inform (location_of (*tp), "reference to %qD is not a constant "
4845 : "expression", op);
4846 : else
4847 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4848 : "expression", op);
4849 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4850 33 : rich_location richloc (line_table, op_loc);
4851 33 : richloc.add_fixit_insert_before (op_loc, "static ");
4852 33 : inform (&richloc,
4853 : "address of non-static constexpr variable %qD may differ on "
4854 : "each invocation of the enclosing function; add %<static%> "
4855 : "to give it a constant address", op);
4856 33 : }
4857 : break;
4858 : }
4859 : default:
4860 : break;
4861 : }
4862 :
4863 : return NULL_TREE;
4864 : }
4865 :
4866 : /* Some expressions may have constant operands but are not constant
4867 : themselves, such as 1/0. Call this function to check for that
4868 : condition.
4869 :
4870 : We only call this in places that require an arithmetic constant, not in
4871 : places where we might have a non-constant expression that can be a
4872 : component of a constant expression, such as the address of a constexpr
4873 : variable that might be dereferenced later. */
4874 :
4875 : static bool
4876 550840677 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4877 : bool *overflow_p)
4878 : {
4879 540662452 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4880 553443140 : && t != void_node)
4881 : {
4882 2602241 : if (!allow_non_constant)
4883 : {
4884 260 : auto_diagnostic_group d;
4885 373 : error_at (cp_expr_loc_or_input_loc (t),
4886 : "%q+E is not a constant expression", t);
4887 260 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4888 : nullptr);
4889 260 : }
4890 2602241 : *non_constant_p = true;
4891 : }
4892 550840677 : if (TREE_OVERFLOW_P (t))
4893 : {
4894 678 : if (!allow_non_constant)
4895 : {
4896 155 : permerror (input_location, "overflow in constant expression");
4897 : /* If we're being permissive (and are in an enforcing
4898 : context), ignore the overflow. */
4899 155 : if (flag_permissive)
4900 75 : return *non_constant_p;
4901 : }
4902 603 : *overflow_p = true;
4903 : }
4904 550840602 : return *non_constant_p;
4905 : }
4906 :
4907 : /* Check whether the shift operation with code CODE and type TYPE on LHS
4908 : and RHS is undefined. If it is, give an error with an explanation,
4909 : and return true; return false otherwise. */
4910 :
4911 : static bool
4912 53766740 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4913 : enum tree_code code, tree type, tree lhs, tree rhs)
4914 : {
4915 53766740 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4916 3673787 : || TREE_CODE (lhs) != INTEGER_CST
4917 3673675 : || TREE_CODE (rhs) != INTEGER_CST)
4918 : return false;
4919 :
4920 3673675 : tree lhstype = TREE_TYPE (lhs);
4921 3673675 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4922 :
4923 : /* [expr.shift] The behavior is undefined if the right operand
4924 : is negative, or greater than or equal to the length in bits
4925 : of the promoted left operand. */
4926 3673675 : if (tree_int_cst_sgn (rhs) == -1)
4927 : {
4928 126 : if (!ctx->quiet)
4929 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4930 : build2_loc (loc, code, type, lhs, rhs));
4931 126 : return (!flag_permissive || ctx->quiet);
4932 : }
4933 3673549 : if (compare_tree_int (rhs, uprec) >= 0)
4934 : {
4935 200 : if (!ctx->quiet)
4936 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4937 : "than or equal to the precision %wu of the left operand",
4938 : build2_loc (loc, code, type, lhs, rhs), uprec);
4939 200 : return (!flag_permissive || ctx->quiet);
4940 : }
4941 :
4942 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4943 : if E1 has a signed type and non-negative value, and E1x2^E2 is
4944 : representable in the corresponding unsigned type of the result type,
4945 : then that value, converted to the result type, is the resulting value;
4946 : otherwise, the behavior is undefined.
4947 : For C++20:
4948 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4949 : 2^N, where N is the range exponent of the type of the result. */
4950 3673349 : if (code == LSHIFT_EXPR
4951 3498918 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4952 2055528 : && cxx_dialect >= cxx11
4953 5709030 : && cxx_dialect < cxx20)
4954 : {
4955 53335 : if (tree_int_cst_sgn (lhs) == -1)
4956 : {
4957 74 : if (!ctx->quiet)
4958 18 : permerror (loc,
4959 : "left operand of shift expression %q+E is negative",
4960 : build2_loc (loc, code, type, lhs, rhs));
4961 74 : return (!flag_permissive || ctx->quiet);
4962 : }
4963 : /* For signed x << y the following:
4964 : (unsigned) x >> ((prec (lhs) - 1) - y)
4965 : if > 1, is undefined. The right-hand side of this formula
4966 : is the highest bit of the LHS that can be set (starting from 0),
4967 : so that the shift doesn't overflow. We then right-shift the LHS
4968 : to see whether any other bit is set making the original shift
4969 : undefined -- the result is not representable in the corresponding
4970 : unsigned type. */
4971 53261 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
4972 53261 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
4973 53261 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
4974 53261 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
4975 53261 : if (tree_int_cst_lt (integer_one_node, t))
4976 : {
4977 41 : if (!ctx->quiet)
4978 7 : permerror (loc, "shift expression %q+E overflows",
4979 : build2_loc (loc, code, type, lhs, rhs));
4980 41 : return (!flag_permissive || ctx->quiet);
4981 : }
4982 : }
4983 : return false;
4984 : }
4985 :
4986 : /* Subroutine of cxx_eval_constant_expression.
4987 : Attempt to reduce the unary expression tree T to a compile time value.
4988 : If successful, return the value. Otherwise issue a diagnostic
4989 : and return error_mark_node. */
4990 :
4991 : static tree
4992 21362241 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
4993 : bool /*lval*/,
4994 : bool *non_constant_p, bool *overflow_p,
4995 : tree *jump_target)
4996 : {
4997 21362241 : tree r;
4998 21362241 : tree orig_arg = TREE_OPERAND (t, 0);
4999 21362241 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
5000 : non_constant_p, overflow_p,
5001 : jump_target);
5002 21362240 : if (*jump_target)
5003 : return NULL_TREE;
5004 21362239 : VERIFY_CONSTANT (arg);
5005 17661898 : location_t loc = EXPR_LOCATION (t);
5006 17661898 : enum tree_code code = TREE_CODE (t);
5007 17661898 : tree type = TREE_TYPE (t);
5008 17661898 : r = fold_unary_loc (loc, code, type, arg);
5009 17661898 : if (r == NULL_TREE)
5010 : {
5011 17 : if (arg == orig_arg)
5012 : r = t;
5013 : else
5014 13 : r = build1_loc (loc, code, type, arg);
5015 : }
5016 17661898 : VERIFY_CONSTANT (r);
5017 : return r;
5018 : }
5019 :
5020 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5021 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5022 : generic folding should be used. */
5023 :
5024 : static tree
5025 5903037 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5026 : tree lhs, tree rhs, bool *non_constant_p,
5027 : bool *overflow_p, tree *jump_target)
5028 : {
5029 5903037 : STRIP_NOPS (lhs);
5030 5903037 : if (TREE_CODE (lhs) != ADDR_EXPR)
5031 : return NULL_TREE;
5032 :
5033 5504520 : lhs = TREE_OPERAND (lhs, 0);
5034 :
5035 : /* &A[i] p+ j => &A[i + j] */
5036 5504520 : if (TREE_CODE (lhs) == ARRAY_REF
5037 100951 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5038 100951 : && TREE_CODE (rhs) == INTEGER_CST
5039 100951 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5040 5605471 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5041 : {
5042 100951 : tree orig_type = TREE_TYPE (t);
5043 100951 : location_t loc = EXPR_LOCATION (t);
5044 100951 : tree type = TREE_TYPE (lhs);
5045 :
5046 100951 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5047 100951 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5048 100951 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5049 : non_constant_p, overflow_p,
5050 : jump_target);
5051 100951 : if (*non_constant_p)
5052 : return NULL_TREE;
5053 100939 : if (*jump_target)
5054 : return NULL_TREE;
5055 : /* Don't fold an out-of-bound access. */
5056 100939 : if (!tree_int_cst_le (t, nelts))
5057 : return NULL_TREE;
5058 100939 : rhs = cp_fold_convert (ssizetype, rhs);
5059 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5060 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5061 100939 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5062 100939 : rhs, TYPE_SIZE_UNIT (type))))
5063 : return NULL_TREE;
5064 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5065 : as signed. */
5066 100907 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5067 100907 : TYPE_SIZE_UNIT (type));
5068 100907 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5069 100907 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5070 : t, NULL_TREE, NULL_TREE);
5071 100907 : t = cp_build_addr_expr (t, tf_warning_or_error);
5072 100907 : t = cp_fold_convert (orig_type, t);
5073 100907 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5074 : non_constant_p, overflow_p,
5075 100907 : jump_target);
5076 : }
5077 :
5078 : return NULL_TREE;
5079 : }
5080 :
5081 : /* Try to fold expressions like
5082 : (struct S *) (&a[0].D.2378 + 12)
5083 : into
5084 : &MEM <struct T> [(void *)&a + 12B]
5085 : This is something normally done by gimple_fold_stmt_to_constant_1
5086 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5087 : dereference the address because some details are lost.
5088 : For pointer comparisons we want such folding though so that
5089 : match.pd address_compare optimization works. */
5090 :
5091 : static tree
5092 7200496 : cxx_maybe_fold_addr_pointer_plus (tree t)
5093 : {
5094 14400995 : while (CONVERT_EXPR_P (t)
5095 7641964 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5096 441465 : t = TREE_OPERAND (t, 0);
5097 7200496 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5098 : return NULL_TREE;
5099 6107787 : tree op0 = TREE_OPERAND (t, 0);
5100 6107787 : tree op1 = TREE_OPERAND (t, 1);
5101 6107787 : if (TREE_CODE (op1) != INTEGER_CST)
5102 : return NULL_TREE;
5103 6107787 : while (CONVERT_EXPR_P (op0)
5104 12213522 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5105 6105735 : op0 = TREE_OPERAND (op0, 0);
5106 6107787 : if (TREE_CODE (op0) != ADDR_EXPR)
5107 : return NULL_TREE;
5108 6107787 : op1 = fold_convert (ptr_type_node, op1);
5109 6107787 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5110 6107787 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5111 : }
5112 :
5113 : /* Subroutine of cxx_eval_constant_expression.
5114 : Like cxx_eval_unary_expression, except for binary expressions. */
5115 :
5116 : static tree
5117 78328437 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5118 : value_cat lval,
5119 : bool *non_constant_p, bool *overflow_p,
5120 : tree *jump_target)
5121 : {
5122 78328437 : tree r = NULL_TREE;
5123 78328437 : tree orig_lhs = TREE_OPERAND (t, 0);
5124 78328437 : tree orig_rhs = TREE_OPERAND (t, 1);
5125 78328437 : tree lhs, rhs;
5126 78328437 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5127 : non_constant_p, overflow_p,
5128 : jump_target);
5129 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5130 : subtraction. */
5131 78328436 : if (*non_constant_p)
5132 : return t;
5133 59702426 : if (*jump_target)
5134 : return NULL_TREE;
5135 59702356 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5136 : non_constant_p, overflow_p,
5137 : jump_target);
5138 59702356 : if (*non_constant_p)
5139 : return t;
5140 58698114 : if (*jump_target)
5141 : return NULL_TREE;
5142 :
5143 58698109 : location_t loc = EXPR_LOCATION (t);
5144 58698109 : enum tree_code code = TREE_CODE (t);
5145 58698109 : tree type = TREE_TYPE (t);
5146 :
5147 58698109 : if (code == EQ_EXPR || code == NE_EXPR)
5148 : {
5149 9145668 : bool is_code_eq = (code == EQ_EXPR);
5150 :
5151 9145668 : if (TREE_CODE (lhs) == PTRMEM_CST
5152 192 : && TREE_CODE (rhs) == PTRMEM_CST)
5153 : {
5154 61 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5155 61 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5156 61 : bool eq;
5157 61 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5158 61 : && TREE_CODE (lmem) == FIELD_DECL
5159 61 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5160 88 : && same_type_p (DECL_CONTEXT (lmem),
5161 : DECL_CONTEXT (rmem)))
5162 : /* If both refer to (possibly different) members of the same union
5163 : (12.3), they compare equal. */
5164 : eq = true;
5165 : else
5166 34 : eq = cp_tree_equal (lhs, rhs);
5167 61 : r = constant_boolean_node (eq == is_code_eq, type);
5168 61 : }
5169 9145607 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5170 9145476 : || TREE_CODE (rhs) == PTRMEM_CST)
5171 9145607 : && (null_member_pointer_value_p (lhs)
5172 131 : || null_member_pointer_value_p (rhs)))
5173 131 : r = constant_boolean_node (!is_code_eq, type);
5174 9145476 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5175 0 : lhs = cplus_expand_constant (lhs);
5176 9145476 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5177 0 : rhs = cplus_expand_constant (rhs);
5178 9145476 : else if (REFLECT_EXPR_P (lhs) && REFLECT_EXPR_P (rhs))
5179 : {
5180 1920 : const bool eq = compare_reflections (lhs, rhs);
5181 1920 : r = constant_boolean_node (eq == is_code_eq, type);
5182 : }
5183 : }
5184 0 : if (r == NULL_TREE
5185 58695997 : && TREE_CODE_CLASS (code) == tcc_comparison
5186 22378849 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5187 : {
5188 3600248 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5189 2987826 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5190 3600248 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5191 3119961 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5192 : }
5193 5903192 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5194 64601301 : && integer_zerop (lhs) && !integer_zerop (rhs))
5195 : {
5196 155 : if (!ctx->quiet)
5197 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5198 155 : *non_constant_p = true;
5199 155 : return t;
5200 : }
5201 58697954 : else if (code == POINTER_PLUS_EXPR)
5202 : {
5203 5903037 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5204 : overflow_p, jump_target);
5205 5903037 : if (*jump_target)
5206 : return NULL_TREE;
5207 : }
5208 52794917 : else if (code == SPACESHIP_EXPR)
5209 : {
5210 25497 : r = genericize_spaceship (loc, type, lhs, rhs);
5211 25497 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5212 25497 : overflow_p, jump_target);
5213 : }
5214 :
5215 58672457 : if (r == NULL_TREE)
5216 : {
5217 58569438 : if (ctx->manifestly_const_eval == mce_true
5218 40672955 : && (flag_constexpr_fp_except
5219 40672952 : || TREE_CODE (type) != REAL_TYPE))
5220 : {
5221 40623799 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5222 40623799 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5223 40623799 : }
5224 : else
5225 17945639 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5226 : }
5227 :
5228 58569438 : if (r == NULL_TREE
5229 4905815 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5230 100 : && TREE_CODE (lhs) == INTEGER_CST
5231 98 : && TREE_CODE (rhs) == INTEGER_CST
5232 63578272 : && wi::neg_p (wi::to_wide (rhs)))
5233 : {
5234 : /* For diagnostics and -fpermissive emulate previous behavior of
5235 : handling shifts by negative amount. */
5236 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5237 98 : if (nrhs)
5238 119 : r = fold_binary_loc (loc,
5239 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5240 : type, lhs, nrhs);
5241 : }
5242 :
5243 58672457 : if (r == NULL_TREE)
5244 : {
5245 4905717 : if (lhs == orig_lhs && rhs == orig_rhs)
5246 : r = t;
5247 : else
5248 1316814 : r = build2_loc (loc, code, type, lhs, rhs);
5249 : }
5250 53766740 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5251 433 : *non_constant_p = true;
5252 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5253 : a local array in a constexpr function. */
5254 58672457 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5255 48773070 : if (!ptr)
5256 48773070 : VERIFY_CONSTANT (r);
5257 : return r;
5258 : }
5259 :
5260 : /* Subroutine of cxx_eval_constant_expression.
5261 : Attempt to evaluate condition expressions. */
5262 :
5263 : static tree
5264 19167008 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5265 : value_cat lval,
5266 : bool *non_constant_p, bool *overflow_p,
5267 : tree *jump_target)
5268 : {
5269 19167008 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5270 : vc_prvalue,
5271 : non_constant_p, overflow_p,
5272 : jump_target);
5273 19167008 : if (*jump_target)
5274 : return NULL_TREE;
5275 19166992 : VERIFY_CONSTANT (val);
5276 17225568 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5277 : {
5278 : /* Evaluate the condition as if it was
5279 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5280 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5281 : without manifestly_const_eval even expressions or parts thereof which
5282 : will later be manifestly const_eval evaluated), otherwise fold it to
5283 : true. */
5284 694936 : if (ctx->manifestly_const_eval == mce_unknown)
5285 : {
5286 685969 : *non_constant_p = true;
5287 685969 : return t;
5288 : }
5289 8967 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5290 : boolean_type_node);
5291 : }
5292 : /* Don't VERIFY_CONSTANT the other operands. */
5293 16539599 : const bool zero_p = integer_zerop (val);
5294 16539599 : if (zero_p)
5295 10432423 : val = TREE_OPERAND (t, 2);
5296 : else
5297 6107176 : val = TREE_OPERAND (t, 1);
5298 16539599 : if (TREE_CODE (t) == IF_STMT && !val)
5299 5026337 : val = void_node;
5300 :
5301 : /* P2564: If we aren't in immediate function context (including a manifestly
5302 : constant-evaluated expression), check any uses of immediate functions in
5303 : the arm we're discarding. But don't do this inside a call; we already
5304 : checked when parsing the function. */
5305 16539599 : if (ctx->manifestly_const_eval != mce_true
5306 3605713 : && !in_immediate_context ()
5307 3510666 : && !ctx->call
5308 17299405 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5309 759806 : ctx->manifestly_const_eval))
5310 : {
5311 7 : *non_constant_p = true;
5312 7 : return t;
5313 : }
5314 :
5315 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5316 : serve as the initializer for the same object as the outer TARGET_EXPR,
5317 : as in
5318 : A a = true ? A{} : A{};
5319 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5320 16539592 : if (TREE_CODE (val) == TARGET_EXPR)
5321 2250 : val = TARGET_EXPR_INITIAL (val);
5322 16539592 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5323 16539592 : overflow_p, jump_target);
5324 : }
5325 :
5326 : /* Subroutine of cxx_eval_constant_expression.
5327 : Attempt to evaluate vector condition expressions. Unlike
5328 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5329 : ternary arithmetics operation, where all 3 arguments have to be
5330 : evaluated as constants and then folding computes the result from
5331 : them. */
5332 :
5333 : static tree
5334 4788 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5335 : bool *non_constant_p, bool *overflow_p,
5336 : tree *jump_target)
5337 : {
5338 4788 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5339 : vc_prvalue,
5340 : non_constant_p, overflow_p,
5341 : jump_target);
5342 4788 : if (*jump_target)
5343 : return NULL_TREE;
5344 4788 : VERIFY_CONSTANT (arg1);
5345 2694 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5346 : vc_prvalue,
5347 : non_constant_p, overflow_p,
5348 : jump_target);
5349 2694 : if (*jump_target)
5350 : return NULL_TREE;
5351 2694 : VERIFY_CONSTANT (arg2);
5352 2567 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5353 : vc_prvalue,
5354 : non_constant_p, overflow_p,
5355 : jump_target);
5356 2567 : if (*jump_target)
5357 : return NULL_TREE;
5358 2567 : VERIFY_CONSTANT (arg3);
5359 2567 : location_t loc = EXPR_LOCATION (t);
5360 2567 : tree type = TREE_TYPE (t);
5361 2567 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5362 2567 : if (r == NULL_TREE)
5363 : {
5364 0 : if (arg1 == TREE_OPERAND (t, 0)
5365 0 : && arg2 == TREE_OPERAND (t, 1)
5366 0 : && arg3 == TREE_OPERAND (t, 2))
5367 : r = t;
5368 : else
5369 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5370 : }
5371 2567 : VERIFY_CONSTANT (r);
5372 : return r;
5373 : }
5374 :
5375 : /* Returns less than, equal to, or greater than zero if KEY is found to be
5376 : less than, to match, or to be greater than the constructor_elt's INDEX. */
5377 :
5378 : static int
5379 253844 : array_index_cmp (tree key, tree index)
5380 : {
5381 253844 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5382 :
5383 253844 : switch (TREE_CODE (index))
5384 : {
5385 250959 : case INTEGER_CST:
5386 250959 : return tree_int_cst_compare (key, index);
5387 2885 : case RANGE_EXPR:
5388 2885 : {
5389 2885 : tree lo = TREE_OPERAND (index, 0);
5390 2885 : tree hi = TREE_OPERAND (index, 1);
5391 2885 : if (tree_int_cst_lt (key, lo))
5392 : return -1;
5393 2593 : else if (tree_int_cst_lt (hi, key))
5394 : return 1;
5395 : else
5396 2593 : return 0;
5397 : }
5398 0 : default:
5399 0 : gcc_unreachable ();
5400 : }
5401 : }
5402 :
5403 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5404 : relative index OFF. */
5405 :
5406 : static tree
5407 29316 : raw_data_cst_elt (tree raw_data, unsigned int off)
5408 : {
5409 29316 : return build_int_cst (TREE_TYPE (raw_data),
5410 29316 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5411 58632 : ? (HOST_WIDE_INT)
5412 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
5413 : : (HOST_WIDE_INT)
5414 243 : RAW_DATA_SCHAR_ELT (raw_data, off));
5415 : }
5416 :
5417 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5418 : if none. If INSERT is true, insert a matching element rather than fail. */
5419 :
5420 : static HOST_WIDE_INT
5421 6375736 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5422 : {
5423 6375736 : if (tree_int_cst_sgn (dindex) < 0)
5424 : return -1;
5425 :
5426 6375736 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5427 6375736 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5428 6375736 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5429 :
5430 9614288 : unsigned HOST_WIDE_INT end = len;
5431 9614288 : unsigned HOST_WIDE_INT begin = 0;
5432 :
5433 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5434 : that the same is true of the other elements and index directly. */
5435 6061805 : if (end > 0)
5436 : {
5437 5938758 : tree cindex = (*elts)[end - 1].index;
5438 5938758 : if (cindex == NULL_TREE)
5439 : {
5440 : /* Verify that if the last index is missing, all indexes
5441 : are missing and there is no RAW_DATA_CST. */
5442 19554 : if (flag_checking)
5443 204348 : for (unsigned int j = 0; j < len - 1; ++j)
5444 184794 : gcc_assert ((*elts)[j].index == NULL_TREE
5445 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5446 19554 : if (i < end)
5447 19554 : return i;
5448 : else
5449 : {
5450 0 : begin = end;
5451 0 : if (i == end)
5452 : /* If the element is to be added right at the end,
5453 : make sure it is added with cleared index too. */
5454 3552483 : dindex = NULL_TREE;
5455 0 : else if (insert)
5456 : /* Otherwise, in order not to break the assumption
5457 : that CONSTRUCTOR either has all indexes or none,
5458 : we need to add indexes to all elements. */
5459 0 : for (unsigned int j = 0; j < len; ++j)
5460 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5461 : }
5462 : }
5463 5919204 : else if (TREE_CODE (cindex) == INTEGER_CST
5464 5919204 : && compare_tree_int (cindex, end - 1) == 0)
5465 : {
5466 5793683 : tree value = (*elts)[end - 1].value;
5467 5793683 : if (i < end)
5468 : {
5469 2803789 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5470 : begin = end - 1;
5471 : else
5472 2803699 : return i;
5473 : }
5474 2989894 : else if (TREE_CODE (value) == RAW_DATA_CST
5475 3022050 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5476 32156 : + RAW_DATA_LENGTH (value)))
5477 16078 : begin = end - 1;
5478 : else
5479 2973816 : begin = end;
5480 : }
5481 : }
5482 :
5483 : /* Otherwise, find a matching index by means of a binary search. */
5484 3685668 : while (begin != end)
5485 : {
5486 231973 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5487 231973 : constructor_elt &elt = (*elts)[middle];
5488 231973 : tree idx = elt.index;
5489 :
5490 231973 : int cmp = array_index_cmp (dindex, idx);
5491 231973 : if (cmp > 0
5492 61023 : && TREE_CODE (elt.value) == RAW_DATA_CST
5493 315303 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5494 83330 : + RAW_DATA_LENGTH (elt.value)))
5495 29178 : cmp = 0;
5496 231973 : if (cmp < 0)
5497 : end = middle;
5498 130633 : else if (cmp > 0)
5499 31845 : begin = middle + 1;
5500 : else
5501 : {
5502 98788 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5503 : {
5504 : /* We need to split the RAW_DATA_CST elt. */
5505 122 : constructor_elt e;
5506 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5507 244 : unsigned int off = (wi::to_offset (dindex)
5508 244 : - wi::to_offset (idx)).to_uhwi ();
5509 122 : tree value = elt.value;
5510 122 : unsigned int len = RAW_DATA_LENGTH (value);
5511 122 : if (off > 1 && len >= off + 3)
5512 22 : value = copy_node (elt.value);
5513 122 : if (off)
5514 : {
5515 33 : if (off > 1)
5516 33 : RAW_DATA_LENGTH (elt.value) = off;
5517 : else
5518 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5519 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5520 : build_int_cst (TREE_TYPE (elt.index),
5521 : off));
5522 33 : e.value = NULL_TREE;
5523 33 : ++middle;
5524 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5525 : }
5526 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5527 122 : if (len >= off + 2)
5528 : {
5529 111 : e.index = (*elts)[middle].index;
5530 111 : e.index = size_binop (PLUS_EXPR, e.index,
5531 : build_one_cst (TREE_TYPE (e.index)));
5532 111 : if (len >= off + 3)
5533 : {
5534 111 : RAW_DATA_LENGTH (value) -= off + 1;
5535 111 : RAW_DATA_POINTER (value) += off + 1;
5536 111 : e.value = value;
5537 : }
5538 : else
5539 0 : e.value = raw_data_cst_elt (value, off + 1);
5540 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5541 : }
5542 122 : return middle;
5543 : }
5544 18475 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5545 : {
5546 : /* We need to split the range. */
5547 345 : constructor_elt e;
5548 345 : tree lo = TREE_OPERAND (idx, 0);
5549 345 : tree hi = TREE_OPERAND (idx, 1);
5550 345 : tree value = elt.value;
5551 345 : dindex = fold_convert (sizetype, dindex);
5552 345 : if (tree_int_cst_lt (lo, dindex))
5553 : {
5554 : /* There are still some lower elts; shorten the range. */
5555 170 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5556 85 : size_one_node);
5557 85 : if (tree_int_cst_equal (lo, new_hi))
5558 : /* Only one element left, no longer a range. */
5559 33 : elt.index = lo;
5560 : else
5561 52 : TREE_OPERAND (idx, 1) = new_hi;
5562 : /* Append the element we want to insert. */
5563 85 : ++middle;
5564 85 : e.index = dindex;
5565 85 : e.value = unshare_constructor (value);
5566 85 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5567 : }
5568 : else
5569 : /* No lower elts, the range elt is now ours. */
5570 260 : elt.index = dindex;
5571 :
5572 345 : if (tree_int_cst_lt (dindex, hi))
5573 : {
5574 : /* There are still some higher elts; append a range. */
5575 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5576 239 : size_one_node);
5577 239 : if (tree_int_cst_equal (new_lo, hi))
5578 98 : e.index = hi;
5579 : else
5580 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5581 239 : e.value = unshare_constructor (value);
5582 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5583 : }
5584 : }
5585 98666 : return middle;
5586 : }
5587 : }
5588 :
5589 3453695 : if (insert)
5590 : {
5591 3377359 : constructor_elt e = { dindex, NULL_TREE };
5592 3377359 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5593 3377359 : return end;
5594 : }
5595 :
5596 : return -1;
5597 : }
5598 :
5599 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5600 : matching constructor_elt exists, then add one to CTOR.
5601 :
5602 : As an optimization, if POS_HINT is non-negative then it is used as a guess
5603 : for the (integer) index of the matching constructor_elt within CTOR.
5604 :
5605 : If POS_HINT is -2, it means do not insert. */
5606 :
5607 : static constructor_elt *
5608 26025309 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5609 : {
5610 : /* Check the hint first. */
5611 2231813 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5612 28257122 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5613 : return CONSTRUCTOR_ELT (ctor, pos_hint);
5614 :
5615 23793545 : bool insertp = (pos_hint != -2);
5616 23793545 : tree type = TREE_TYPE (ctor);
5617 23793545 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5618 : {
5619 8216 : gcc_assert (insertp);
5620 8216 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5621 8216 : return &CONSTRUCTOR_ELTS (ctor)->last();
5622 : }
5623 23785329 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5624 : {
5625 4437095 : if (TREE_CODE (index) == RANGE_EXPR)
5626 : {
5627 : /* Support for RANGE_EXPR index lookups is currently limited to
5628 : accessing an existing element via POS_HINT, or appending a new
5629 : element to the end of CTOR. ??? Support for other access
5630 : patterns may also be needed. */
5631 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5632 3 : if (vec_safe_length (elts))
5633 : {
5634 3 : tree lo = TREE_OPERAND (index, 0);
5635 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5636 : }
5637 3 : gcc_assert (insertp);
5638 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5639 3 : return &elts->last();
5640 : }
5641 :
5642 4437092 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5643 4437092 : if (i < 0)
5644 : {
5645 918 : gcc_assert (!insertp);
5646 : return nullptr;
5647 : }
5648 4436174 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5649 4436174 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5650 : {
5651 : /* Split a range even if we aren't inserting new entries. */
5652 0 : gcc_assert (!insertp);
5653 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5654 0 : gcc_assert (i >= 0);
5655 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5656 : }
5657 4436174 : gcc_assert (cep->index == NULL_TREE
5658 : || TREE_CODE (cep->index) != RANGE_EXPR);
5659 : return cep;
5660 : }
5661 : else
5662 : {
5663 19348234 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5664 : && (same_type_ignoring_top_level_qualifiers_p
5665 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5666 :
5667 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5668 : Usually we meet initializers in that order, but it is
5669 : possible for base types to be placed not in program
5670 : order. */
5671 19348234 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5672 19348234 : unsigned HOST_WIDE_INT idx = 0;
5673 19348234 : constructor_elt *cep = NULL;
5674 :
5675 : /* Check if we're changing the active member of a union. */
5676 297049 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5677 19495754 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5678 3474 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5679 : /* If the bit offset of INDEX is larger than that of the last
5680 : constructor_elt, then we can just immediately append a new
5681 : constructor_elt to the end of CTOR. */
5682 19344760 : else if (CONSTRUCTOR_NELTS (ctor)
5683 25520450 : && tree_int_cst_compare (bit_position (index),
5684 25050612 : bit_position (CONSTRUCTOR_ELTS (ctor)
5685 12525306 : ->last().index)) > 0)
5686 : {
5687 3644777 : idx = CONSTRUCTOR_NELTS (ctor);
5688 3644777 : goto insert;
5689 : }
5690 :
5691 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5692 : appropriately. */
5693 :
5694 17363359 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5695 1659902 : idx++, fields = DECL_CHAIN (fields))
5696 : {
5697 10540431 : if (index == cep->index)
5698 8854811 : goto found;
5699 :
5700 : /* The field we're initializing must be on the field
5701 : list. Look to see if it is present before the
5702 : field the current ELT initializes. */
5703 17737801 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5704 16077899 : if (index == fields)
5705 25718 : goto insert;
5706 : }
5707 : /* We fell off the end of the CONSTRUCTOR, so insert a new
5708 : entry at the end. */
5709 :
5710 10493423 : insert:
5711 10493423 : if (!insertp)
5712 : return nullptr;
5713 :
5714 10493420 : {
5715 10493420 : constructor_elt ce = { index, NULL_TREE };
5716 :
5717 10493420 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5718 10493420 : cep = CONSTRUCTOR_ELT (ctor, idx);
5719 : }
5720 19348231 : found:;
5721 :
5722 19348231 : return cep;
5723 : }
5724 : }
5725 :
5726 : /* Under the control of CTX, issue a detailed diagnostic for
5727 : an out-of-bounds subscript INDEX into the expression ARRAY. */
5728 :
5729 : static void
5730 566 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5731 : {
5732 566 : if (!ctx->quiet)
5733 : {
5734 116 : tree arraytype = TREE_TYPE (array);
5735 :
5736 : /* Convert the unsigned array subscript to a signed integer to avoid
5737 : printing huge numbers for small negative values. */
5738 116 : tree sidx = fold_convert (ssizetype, index);
5739 116 : STRIP_ANY_LOCATION_WRAPPER (array);
5740 116 : if (DECL_P (array))
5741 : {
5742 75 : auto_diagnostic_group d;
5743 75 : if (TYPE_DOMAIN (arraytype))
5744 69 : error_at (loc, "array subscript value %qE is outside the bounds "
5745 : "of array %qD of type %qT", sidx, array, arraytype);
5746 : else
5747 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5748 : "type %qT with unknown bounds", sidx, array, arraytype);
5749 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5750 75 : }
5751 41 : else if (TYPE_DOMAIN (arraytype))
5752 38 : error_at (loc, "array subscript value %qE is outside the bounds "
5753 : "of array type %qT", sidx, arraytype);
5754 : else
5755 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5756 : "with unknown bounds", sidx, arraytype);
5757 : }
5758 566 : }
5759 :
5760 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5761 : a VECTOR_TYPE). */
5762 :
5763 : static tree
5764 12198541 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5765 : bool *non_constant_p, bool *overflow_p,
5766 : tree *jump_target)
5767 : {
5768 12198541 : tree nelts;
5769 12198541 : if (TREE_CODE (type) == ARRAY_TYPE)
5770 : {
5771 12198541 : if (TYPE_DOMAIN (type))
5772 12198293 : nelts = array_type_nelts_top (type);
5773 : else
5774 248 : nelts = size_zero_node;
5775 : }
5776 0 : else if (VECTOR_TYPE_P (type))
5777 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5778 : else
5779 0 : gcc_unreachable ();
5780 :
5781 : /* For VLAs, the number of elements won't be an integer constant. */
5782 12198541 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5783 : non_constant_p, overflow_p,
5784 : jump_target);
5785 12198541 : return nelts;
5786 : }
5787 :
5788 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5789 : STRING_CST STRING. */
5790 :
5791 : static tree
5792 1659085 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5793 : {
5794 1659085 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5795 1659085 : tree r;
5796 :
5797 1659085 : if (chars_per_elt == 1)
5798 1491545 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5799 : else
5800 : {
5801 167540 : const unsigned char *ptr
5802 167540 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5803 167540 : + index * chars_per_elt);
5804 167540 : r = native_interpret_expr (type, ptr, chars_per_elt);
5805 : }
5806 1659085 : return r;
5807 : }
5808 :
5809 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5810 : subscript, diagnose any problems with it, and return the result. */
5811 :
5812 : static tree
5813 12385138 : eval_and_check_array_index (const constexpr_ctx *ctx,
5814 : tree t, bool allow_one_past,
5815 : bool *non_constant_p, bool *overflow_p,
5816 : tree *jump_target)
5817 : {
5818 12385138 : location_t loc = cp_expr_loc_or_input_loc (t);
5819 12385138 : tree ary = TREE_OPERAND (t, 0);
5820 12385138 : t = TREE_OPERAND (t, 1);
5821 12385138 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5822 : non_constant_p, overflow_p,
5823 : jump_target);
5824 12385138 : if (*jump_target)
5825 : return NULL_TREE;
5826 12385138 : VERIFY_CONSTANT (index);
5827 :
5828 12198007 : if (!tree_fits_shwi_p (index)
5829 12198007 : || tree_int_cst_sgn (index) < 0)
5830 : {
5831 108 : diag_array_subscript (loc, ctx, ary, index);
5832 108 : *non_constant_p = true;
5833 108 : return t;
5834 : }
5835 :
5836 12197899 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5837 : overflow_p, jump_target);
5838 12197899 : if (*jump_target)
5839 : return NULL_TREE;
5840 12197899 : VERIFY_CONSTANT (nelts);
5841 12197836 : if (allow_one_past
5842 12197836 : ? !tree_int_cst_le (index, nelts)
5843 7853177 : : !tree_int_cst_lt (index, nelts))
5844 : {
5845 458 : diag_array_subscript (loc, ctx, ary, index);
5846 458 : *non_constant_p = true;
5847 458 : return t;
5848 : }
5849 :
5850 : return index;
5851 : }
5852 :
5853 : /* Subroutine of cxx_eval_constant_expression.
5854 : Attempt to reduce a reference to an array slot. */
5855 :
5856 : static tree
5857 8370362 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5858 : value_cat lval,
5859 : bool *non_constant_p, bool *overflow_p,
5860 : tree *jump_target)
5861 : {
5862 8370362 : tree oldary = TREE_OPERAND (t, 0);
5863 8370362 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5864 : lval,
5865 : non_constant_p, overflow_p,
5866 : jump_target);
5867 8370362 : if (*non_constant_p)
5868 : return t;
5869 8171373 : if (*jump_target)
5870 : return NULL_TREE;
5871 8171373 : if (!lval
5872 3825428 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5873 54102 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5874 8225475 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5875 54102 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5876 54102 : ary = TREE_OPERAND (ary, 0);
5877 :
5878 8171373 : tree oldidx = TREE_OPERAND (t, 1);
5879 8171373 : tree index = eval_and_check_array_index (ctx, t, lval,
5880 : non_constant_p, overflow_p,
5881 : jump_target);
5882 8171373 : if (*non_constant_p)
5883 : return t;
5884 7983679 : if (*jump_target)
5885 : return NULL_TREE;
5886 :
5887 7983679 : if (lval && ary == oldary && index == oldidx)
5888 : return t;
5889 4445857 : else if (lval == vc_discard)
5890 : return t;
5891 4445857 : else if (lval)
5892 806597 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5893 :
5894 3639260 : unsigned len = 0, elem_nchars = 1;
5895 3639260 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5896 3639260 : if (TREE_CODE (ary) == CONSTRUCTOR)
5897 1936761 : len = CONSTRUCTOR_NELTS (ary);
5898 1702499 : else if (TREE_CODE (ary) == STRING_CST)
5899 : {
5900 1648405 : elem_nchars = (TYPE_PRECISION (elem_type)
5901 1648405 : / TYPE_PRECISION (char_type_node));
5902 1648405 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5903 : }
5904 54094 : else if (TREE_CODE (ary) == VECTOR_CST)
5905 : /* We don't create variable-length VECTOR_CSTs. */
5906 54094 : len = VECTOR_CST_NELTS (ary).to_constant ();
5907 : else
5908 : {
5909 : /* We can't do anything with other tree codes, so use
5910 : VERIFY_CONSTANT to complain and fail. */
5911 0 : VERIFY_CONSTANT (ary);
5912 0 : gcc_unreachable ();
5913 : }
5914 :
5915 3639260 : bool found;
5916 3639260 : HOST_WIDE_INT i = 0;
5917 3639260 : if (TREE_CODE (ary) == CONSTRUCTOR)
5918 : {
5919 1936761 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5920 1936761 : found = (ix >= 0);
5921 1936761 : if (found)
5922 1861431 : i = ix;
5923 : }
5924 : else
5925 : {
5926 1702499 : i = tree_to_shwi (index);
5927 1702499 : found = (i < len);
5928 : }
5929 :
5930 3639260 : if (found)
5931 : {
5932 3563479 : tree r;
5933 3563479 : if (TREE_CODE (ary) == CONSTRUCTOR)
5934 : {
5935 1861431 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5936 1861431 : if (TREE_CODE (r) == RAW_DATA_CST)
5937 : {
5938 29194 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5939 29194 : gcc_checking_assert (ridx);
5940 29194 : unsigned int off
5941 29194 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5942 29194 : r = raw_data_cst_elt (r, off);
5943 : }
5944 : }
5945 1702048 : else if (TREE_CODE (ary) == VECTOR_CST)
5946 54094 : r = VECTOR_CST_ELT (ary, i);
5947 : else
5948 1647954 : r = extract_string_elt (ary, elem_nchars, i);
5949 :
5950 1731242 : if (r)
5951 : /* Don't VERIFY_CONSTANT here. */
5952 3563479 : return r;
5953 :
5954 : /* Otherwise the element doesn't have a value yet. */
5955 : }
5956 :
5957 : /* Not found. */
5958 :
5959 75781 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
5960 63 : return build_constructor (elem_type, NULL);
5961 :
5962 75718 : if (TREE_CODE (ary) == CONSTRUCTOR
5963 75718 : && CONSTRUCTOR_NO_CLEARING (ary))
5964 : {
5965 : /* 'ary' is part of the aggregate initializer we're currently
5966 : building; if there's no initializer for this element yet,
5967 : that's an error. */
5968 51 : if (!ctx->quiet)
5969 16 : error ("accessing uninitialized array element");
5970 51 : *non_constant_p = true;
5971 51 : return t;
5972 : }
5973 :
5974 : /* If it's within the array bounds but doesn't have an explicit
5975 : initializer, it's initialized from {}. But use build_value_init
5976 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
5977 75667 : tree val;
5978 75667 : constexpr_ctx new_ctx;
5979 75667 : if (CP_AGGREGATE_TYPE_P (elem_type))
5980 : {
5981 485 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
5982 485 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
5983 : }
5984 : else
5985 75182 : val = build_value_init (elem_type, tf_warning_or_error);
5986 :
5987 : /* Create a new constructor only if we don't already have a suitable one. */
5988 75667 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
5989 76184 : && (!ctx->ctor
5990 45 : || !same_type_ignoring_top_level_qualifiers_p
5991 45 : (elem_type, TREE_TYPE (ctx->ctor))));
5992 508 : if (new_ctor)
5993 : {
5994 508 : new_ctx = *ctx;
5995 : /* We clear the object here. We used to replace it with T, but that
5996 : caused problems (101371, 108158); and anyway, T is the initializer,
5997 : not the target object. */
5998 508 : new_ctx.object = NULL_TREE;
5999 508 : new_ctx.ctor = build_constructor (elem_type, NULL);
6000 508 : ctx = &new_ctx;
6001 : }
6002 75667 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6003 : overflow_p, jump_target);
6004 75667 : if (new_ctor && t != ctx->ctor)
6005 487 : free_constructor (ctx->ctor);
6006 : return t;
6007 : }
6008 :
6009 : /* Subroutine of cxx_eval_constant_expression.
6010 : Attempt to reduce a field access of a value of class type. */
6011 :
6012 : static tree
6013 69567490 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
6014 : value_cat lval,
6015 : bool *non_constant_p, bool *overflow_p,
6016 : tree *jump_target)
6017 : {
6018 69567490 : unsigned HOST_WIDE_INT i;
6019 69567490 : tree field;
6020 69567490 : tree value;
6021 69567490 : tree part = TREE_OPERAND (t, 1);
6022 69567490 : tree orig_whole = TREE_OPERAND (t, 0);
6023 69567490 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6024 : lval,
6025 : non_constant_p, overflow_p,
6026 : jump_target);
6027 69567490 : if (*non_constant_p)
6028 : return t;
6029 47419794 : if (*jump_target)
6030 : return NULL_TREE;
6031 47419791 : if (INDIRECT_REF_P (whole)
6032 47419791 : && integer_zerop (TREE_OPERAND (whole, 0)))
6033 : {
6034 177 : if (!ctx->quiet)
6035 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6036 177 : *non_constant_p = true;
6037 177 : return t;
6038 : }
6039 :
6040 47419614 : if (TREE_CODE (whole) == PTRMEM_CST)
6041 1486 : whole = cplus_expand_constant (whole);
6042 47419614 : if (whole == orig_whole)
6043 : return t;
6044 29790473 : if (lval == vc_discard)
6045 : return t;
6046 29790449 : if (lval)
6047 11257677 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6048 : whole, part, NULL_TREE);
6049 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6050 : CONSTRUCTOR. */
6051 18532772 : if (TREE_CODE (whole) != CONSTRUCTOR)
6052 : {
6053 0 : if (!ctx->quiet)
6054 0 : error ("%qE is not a constant expression", orig_whole);
6055 0 : *non_constant_p = true;
6056 0 : return t;
6057 : }
6058 18531281 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6059 18534445 : && DECL_MUTABLE_P (part))
6060 : {
6061 144 : if (!ctx->quiet)
6062 16 : error ("mutable %qD is not usable in a constant expression", part);
6063 144 : *non_constant_p = true;
6064 144 : return t;
6065 : }
6066 :
6067 18532628 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6068 26718828 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6069 : {
6070 : /* Use name match for PMF fields, as a variant will have a
6071 : different FIELD_DECL with a different type. */
6072 26620130 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6073 : : field == part)
6074 : {
6075 18433930 : if (value == void_node)
6076 3 : goto uninit;
6077 18433927 : if (value)
6078 : {
6079 18433909 : STRIP_ANY_LOCATION_WRAPPER (value);
6080 18433909 : return value;
6081 : }
6082 : else
6083 : /* We're in the middle of initializing it. */
6084 : break;
6085 : }
6086 : }
6087 98716 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6088 : {
6089 106 : if (CONSTRUCTOR_NELTS (whole) > 0)
6090 : {
6091 : /* DR 1188 says we don't have to deal with this. */
6092 91 : if (!ctx->quiet)
6093 : {
6094 19 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6095 19 : if (cep->value == NULL_TREE)
6096 9 : error ("accessing uninitialized member %qD", part);
6097 : else
6098 10 : error ("accessing %qD member instead of active %qD member "
6099 : "in constant expression", part, cep->index);
6100 : }
6101 91 : *non_constant_p = true;
6102 91 : return t;
6103 : }
6104 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6105 : {
6106 : /* Value-initialized union, check if looking at the first member. */
6107 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6108 12 : if (first != part)
6109 : {
6110 9 : if (!ctx->quiet)
6111 3 : error ("accessing %qD member instead of initialized %qD "
6112 : "member in constant expression", part, first);
6113 9 : *non_constant_p = true;
6114 9 : return t;
6115 : }
6116 : }
6117 : }
6118 :
6119 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6120 : classes never get represented; throw together a value now. */
6121 98616 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6122 90022 : return build_constructor (TREE_TYPE (t), NULL);
6123 :
6124 8594 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6125 :
6126 8594 : if (CONSTRUCTOR_NO_CLEARING (whole))
6127 : {
6128 110 : uninit:
6129 : /* 'whole' is part of the aggregate initializer we're currently
6130 : building; if there's no initializer for this member yet, that's an
6131 : error. */
6132 113 : if (!ctx->quiet)
6133 22 : error ("accessing uninitialized member %qD", part);
6134 113 : *non_constant_p = true;
6135 113 : return t;
6136 : }
6137 :
6138 : /* If there's no explicit init for this field, it's value-initialized. */
6139 :
6140 8484 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6141 : {
6142 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6143 : and copy the flags. */
6144 7988 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6145 7988 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6146 7988 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6147 7988 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6148 7988 : return value;
6149 : }
6150 :
6151 496 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6152 496 : return cxx_eval_constant_expression (ctx, value,
6153 : lval,
6154 : non_constant_p, overflow_p,
6155 496 : jump_target);
6156 : }
6157 :
6158 : /* Subroutine of cxx_eval_constant_expression.
6159 : Attempt to reduce a field access of a value of class type that is
6160 : expressed as a BIT_FIELD_REF. */
6161 :
6162 : static tree
6163 16029 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6164 : value_cat lval,
6165 : bool *non_constant_p, bool *overflow_p,
6166 : tree *jump_target)
6167 : {
6168 16029 : tree orig_whole = TREE_OPERAND (t, 0);
6169 16029 : tree retval, fldval, utype, mask;
6170 16029 : bool fld_seen = false;
6171 16029 : HOST_WIDE_INT istart, isize;
6172 16029 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6173 : lval,
6174 : non_constant_p, overflow_p,
6175 : jump_target);
6176 16029 : tree start, field, value;
6177 16029 : unsigned HOST_WIDE_INT i;
6178 :
6179 16029 : if (*jump_target)
6180 : return NULL_TREE;
6181 16029 : if (whole == orig_whole)
6182 : return t;
6183 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6184 : CONSTRUCTOR. */
6185 15818 : if (!*non_constant_p
6186 15818 : && TREE_CODE (whole) != VECTOR_CST
6187 1739 : && TREE_CODE (whole) != CONSTRUCTOR)
6188 : {
6189 0 : if (!ctx->quiet)
6190 0 : error ("%qE is not a constant expression", orig_whole);
6191 0 : *non_constant_p = true;
6192 : }
6193 15818 : if (*non_constant_p)
6194 : return t;
6195 :
6196 15818 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6197 : {
6198 14079 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6199 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6200 : return r;
6201 0 : if (!ctx->quiet)
6202 0 : error ("%qE is not a constant expression", orig_whole);
6203 0 : *non_constant_p = true;
6204 0 : return t;
6205 : }
6206 :
6207 1739 : start = TREE_OPERAND (t, 2);
6208 1739 : istart = tree_to_shwi (start);
6209 1739 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6210 1739 : utype = TREE_TYPE (t);
6211 1739 : if (!TYPE_UNSIGNED (utype))
6212 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6213 1739 : retval = build_int_cst (utype, 0);
6214 24346 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6215 : {
6216 22607 : tree bitpos = bit_position (field);
6217 22607 : STRIP_ANY_LOCATION_WRAPPER (value);
6218 22607 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6219 : return value;
6220 22607 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6221 22607 : && TREE_CODE (value) == INTEGER_CST
6222 22607 : && tree_fits_shwi_p (bitpos)
6223 45214 : && tree_fits_shwi_p (DECL_SIZE (field)))
6224 : {
6225 22607 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6226 22607 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6227 22607 : HOST_WIDE_INT shift;
6228 22607 : if (bit >= istart && bit + sz <= istart + isize)
6229 : {
6230 5217 : fldval = fold_convert (utype, value);
6231 5217 : mask = build_int_cst_type (utype, -1);
6232 5217 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6233 : size_int (TYPE_PRECISION (utype) - sz));
6234 5217 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6235 : size_int (TYPE_PRECISION (utype) - sz));
6236 5217 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6237 5217 : shift = bit - istart;
6238 5217 : if (BYTES_BIG_ENDIAN)
6239 : shift = TYPE_PRECISION (utype) - shift - sz;
6240 5217 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6241 : size_int (shift));
6242 5217 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6243 5217 : fld_seen = true;
6244 : }
6245 : }
6246 : }
6247 1739 : if (fld_seen)
6248 1739 : return fold_convert (TREE_TYPE (t), retval);
6249 0 : gcc_unreachable ();
6250 : return error_mark_node;
6251 : }
6252 :
6253 : /* Helper for cxx_eval_bit_cast.
6254 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6255 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6256 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6257 : data members of reference type. */
6258 :
6259 : static bool
6260 6018 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6261 : tree orig_type)
6262 : {
6263 6563 : if (TREE_CODE (type) == UNION_TYPE)
6264 : {
6265 63 : if (!ctx->quiet)
6266 : {
6267 21 : if (type == orig_type)
6268 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6269 : "a union type", "__builtin_bit_cast", type);
6270 : else
6271 15 : error_at (loc, "%qs is not a constant expression because %qT "
6272 : "contains a union type", "__builtin_bit_cast",
6273 : orig_type);
6274 : }
6275 63 : return true;
6276 : }
6277 : if (TREE_CODE (type) == POINTER_TYPE)
6278 : {
6279 57 : if (!ctx->quiet)
6280 : {
6281 18 : if (type == orig_type)
6282 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6283 : "a pointer type", "__builtin_bit_cast", type);
6284 : else
6285 12 : error_at (loc, "%qs is not a constant expression because %qT "
6286 : "contains a pointer type", "__builtin_bit_cast",
6287 : orig_type);
6288 : }
6289 57 : return true;
6290 : }
6291 : if (TREE_CODE (type) == REFERENCE_TYPE)
6292 : {
6293 0 : if (!ctx->quiet)
6294 : {
6295 0 : if (type == orig_type)
6296 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6297 : "a reference type", "__builtin_bit_cast", type);
6298 : else
6299 0 : error_at (loc, "%qs is not a constant expression because %qT "
6300 : "contains a reference type", "__builtin_bit_cast",
6301 : orig_type);
6302 : }
6303 0 : return true;
6304 : }
6305 2113 : if (TYPE_PTRMEM_P (type))
6306 : {
6307 36 : if (!ctx->quiet)
6308 : {
6309 12 : if (type == orig_type)
6310 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6311 : "a pointer to member type", "__builtin_bit_cast",
6312 : type);
6313 : else
6314 0 : error_at (loc, "%qs is not a constant expression because %qT "
6315 : "contains a pointer to member type",
6316 : "__builtin_bit_cast", orig_type);
6317 : }
6318 36 : return true;
6319 : }
6320 6407 : if (TYPE_VOLATILE (type))
6321 : {
6322 0 : if (!ctx->quiet)
6323 : {
6324 0 : if (type == orig_type)
6325 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6326 : "volatile", "__builtin_bit_cast", type);
6327 : else
6328 0 : error_at (loc, "%qs is not a constant expression because %qT "
6329 : "contains a volatile subobject",
6330 : "__builtin_bit_cast", orig_type);
6331 : }
6332 0 : return true;
6333 : }
6334 6407 : if (TREE_CODE (type) == RECORD_TYPE)
6335 70710 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6336 68705 : if (TREE_CODE (field) == FIELD_DECL
6337 68705 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6338 : return true;
6339 6317 : if (TREE_CODE (type) == ARRAY_TYPE)
6340 545 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6341 : return false;
6342 : }
6343 :
6344 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6345 : std::byte members of CONSTRUCTOR (recursively) if they contain
6346 : some indeterminate bits (as set in MASK), remove the ctor elts,
6347 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6348 : bits in MASK. */
6349 :
6350 : static void
6351 707 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6352 : {
6353 707 : if (TREE_CODE (t) != CONSTRUCTOR)
6354 : return;
6355 :
6356 : unsigned i, j = 0;
6357 : tree index, value;
6358 2284 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6359 : {
6360 1577 : tree type = TREE_TYPE (value);
6361 1577 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6362 2715 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6363 : {
6364 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6365 : {
6366 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6367 135 : gcc_assert (fldsz != 0);
6368 135 : HOST_WIDE_INT pos = int_byte_position (index);
6369 135 : HOST_WIDE_INT bpos
6370 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6371 135 : bpos %= BITS_PER_UNIT;
6372 135 : HOST_WIDE_INT end
6373 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6374 135 : gcc_assert (end == 1 || end == 2);
6375 135 : unsigned char *p = mask + pos;
6376 135 : unsigned char mask_save[2];
6377 135 : mask_save[0] = mask[pos];
6378 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6379 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6380 : sorry_at (loc, "PDP11 bit-field handling unsupported"
6381 : " in %qs", "__builtin_bit_cast");
6382 135 : else if (BYTES_BIG_ENDIAN)
6383 : {
6384 : /* Big endian. */
6385 : if (bpos + fldsz <= BITS_PER_UNIT)
6386 : *p &= ~(((1 << fldsz) - 1)
6387 : << (BITS_PER_UNIT - bpos - fldsz));
6388 : else
6389 : {
6390 : gcc_assert (bpos);
6391 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6392 : p++;
6393 : fldsz -= BITS_PER_UNIT - bpos;
6394 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6395 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6396 : }
6397 : }
6398 : else
6399 : {
6400 : /* Little endian. */
6401 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6402 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6403 : else
6404 : {
6405 0 : gcc_assert (bpos);
6406 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6407 0 : p++;
6408 0 : fldsz -= BITS_PER_UNIT - bpos;
6409 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6410 0 : *p &= ~((1 << fldsz) - 1);
6411 : }
6412 : }
6413 135 : if (mask_save[0] != mask[pos]
6414 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6415 : {
6416 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6417 36 : continue;
6418 : }
6419 : }
6420 : }
6421 1307 : else if (is_byte_access_type_not_plain_char (type))
6422 : {
6423 228 : HOST_WIDE_INT pos;
6424 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6425 132 : pos = tree_to_shwi (index);
6426 : else
6427 96 : pos = int_byte_position (index);
6428 228 : if (mask[pos])
6429 : {
6430 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6431 48 : mask[pos] = 0;
6432 48 : continue;
6433 : }
6434 : }
6435 1493 : if (TREE_CODE (value) == CONSTRUCTOR)
6436 : {
6437 368 : HOST_WIDE_INT pos;
6438 368 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6439 144 : pos = tree_to_shwi (index)
6440 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6441 : else
6442 296 : pos = int_byte_position (index);
6443 368 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6444 : }
6445 1493 : if (i != j)
6446 : {
6447 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6448 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6449 : }
6450 1493 : ++j;
6451 : }
6452 1414 : if (CONSTRUCTOR_NELTS (t) != j)
6453 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6454 : }
6455 :
6456 : /* Subroutine of cxx_eval_constant_expression.
6457 : Attempt to evaluate a BIT_CAST_EXPR. */
6458 :
6459 : static tree
6460 1238 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6461 : bool *overflow_p, tree *jump_target)
6462 : {
6463 1238 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6464 1238 : TREE_TYPE (t))
6465 4004 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6466 1157 : EXPR_LOCATION (t)),
6467 1157 : TREE_TYPE (TREE_OPERAND (t, 0)),
6468 1157 : TREE_TYPE (TREE_OPERAND (t, 0))))
6469 : {
6470 156 : *non_constant_p = true;
6471 156 : return t;
6472 : }
6473 :
6474 1082 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6475 : non_constant_p, overflow_p,
6476 : jump_target);
6477 1082 : if (*non_constant_p)
6478 : return t;
6479 708 : if (*jump_target)
6480 : return NULL_TREE;
6481 :
6482 708 : location_t loc = EXPR_LOCATION (t);
6483 708 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6484 : {
6485 : if (!ctx->quiet)
6486 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6487 : "__builtin_bit_cast");
6488 : *non_constant_p = true;
6489 : return t;
6490 : }
6491 :
6492 708 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6493 : {
6494 0 : if (!ctx->quiet)
6495 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6496 : "type is too large", "__builtin_bit_cast");
6497 0 : *non_constant_p = true;
6498 0 : return t;
6499 : }
6500 :
6501 708 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6502 708 : if (len < 0 || (int) len != len)
6503 : {
6504 0 : if (!ctx->quiet)
6505 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6506 : "type is too large", "__builtin_bit_cast");
6507 0 : *non_constant_p = true;
6508 0 : return t;
6509 : }
6510 :
6511 708 : unsigned char buf[64];
6512 708 : unsigned char *ptr, *mask;
6513 708 : size_t alen = (size_t) len * 2;
6514 708 : if (alen <= sizeof (buf))
6515 : ptr = buf;
6516 : else
6517 3 : ptr = XNEWVEC (unsigned char, alen);
6518 708 : mask = ptr + (size_t) len;
6519 : /* At the beginning consider everything indeterminate. */
6520 708 : memset (mask, ~0, (size_t) len);
6521 :
6522 708 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6523 : {
6524 0 : if (!ctx->quiet)
6525 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6526 : "argument cannot be encoded", "__builtin_bit_cast");
6527 0 : *non_constant_p = true;
6528 0 : if (ptr != buf)
6529 0 : XDELETE (ptr);
6530 0 : return t;
6531 : }
6532 :
6533 708 : tree r = NULL_TREE;
6534 708 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6535 : {
6536 369 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6537 369 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6538 : {
6539 46 : gcc_assert (len == 1);
6540 46 : if (mask[0])
6541 : {
6542 24 : memset (mask, 0, len);
6543 24 : r = build_constructor (TREE_TYPE (r), NULL);
6544 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6545 : }
6546 : }
6547 : }
6548 339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6549 : {
6550 339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6551 339 : if (r != NULL_TREE)
6552 : {
6553 339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6554 339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6555 339 : if (CHECKING_P)
6556 : {
6557 339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6558 : non_constant_p, overflow_p,
6559 : jump_target);
6560 339 : gcc_checking_assert (e == r && !*jump_target);
6561 : r = e;
6562 : }
6563 : }
6564 : }
6565 :
6566 708 : if (r != NULL_TREE)
6567 : {
6568 5935 : for (int i = 0; i < len; i++)
6569 5317 : if (mask[i])
6570 : {
6571 90 : if (!ctx->quiet)
6572 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6573 : "__builtin_bit_cast", i);
6574 90 : *non_constant_p = true;
6575 90 : r = t;
6576 90 : break;
6577 : }
6578 708 : if (ptr != buf)
6579 3 : XDELETE (ptr);
6580 708 : return r;
6581 : }
6582 :
6583 0 : if (!ctx->quiet)
6584 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6585 : "argument cannot be interpreted", "__builtin_bit_cast");
6586 0 : *non_constant_p = true;
6587 0 : if (ptr != buf)
6588 0 : XDELETE (ptr);
6589 : return t;
6590 : }
6591 :
6592 : /* Subroutine of cxx_eval_constant_expression.
6593 : Evaluate a short-circuited logical expression T in the context
6594 : of a given constexpr CALL. BAILOUT_VALUE is the value for
6595 : early return. CONTINUE_VALUE is used here purely for
6596 : sanity check purposes. */
6597 :
6598 : static tree
6599 10768476 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6600 : tree bailout_value, tree continue_value,
6601 : bool *non_constant_p, bool *overflow_p,
6602 : tree *jump_target)
6603 : {
6604 10768476 : tree r;
6605 10768476 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6606 : vc_prvalue, non_constant_p,
6607 : overflow_p, jump_target);
6608 10768475 : if (*jump_target)
6609 : return NULL_TREE;
6610 10768466 : VERIFY_CONSTANT (lhs);
6611 6728549 : if (tree_int_cst_equal (lhs, bailout_value))
6612 : return lhs;
6613 5475720 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6614 5475720 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6615 : vc_prvalue, non_constant_p,
6616 : overflow_p, jump_target);
6617 5475720 : if (*jump_target)
6618 : return NULL_TREE;
6619 5475720 : VERIFY_CONSTANT (r);
6620 : return r;
6621 : }
6622 :
6623 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6624 : CONSTRUCTOR elements to initialize (part of) an object containing that
6625 : field. Return a pointer to the constructor_elt corresponding to the
6626 : initialization of the field. */
6627 :
6628 : static constructor_elt *
6629 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6630 : {
6631 0 : tree aggr = TREE_OPERAND (ref, 0);
6632 0 : tree field = TREE_OPERAND (ref, 1);
6633 0 : HOST_WIDE_INT i;
6634 0 : constructor_elt *ce;
6635 :
6636 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6637 :
6638 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6639 : {
6640 0 : constructor_elt *base_ce
6641 0 : = base_field_constructor_elt (v, aggr);
6642 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6643 : }
6644 :
6645 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6646 0 : if (ce->index == field)
6647 0 : return ce;
6648 :
6649 0 : gcc_unreachable ();
6650 : return NULL;
6651 : }
6652 :
6653 : /* Some of the expressions fed to the constexpr mechanism are calls to
6654 : constructors, which have type void. In that case, return the type being
6655 : initialized by the constructor. */
6656 :
6657 : static tree
6658 520857923 : initialized_type (tree t)
6659 : {
6660 522204655 : if (TYPE_P (t))
6661 : return t;
6662 522204186 : tree type = TREE_TYPE (t);
6663 522204186 : if (TREE_CODE (t) == CALL_EXPR)
6664 : {
6665 : /* A constructor call has void type, so we need to look deeper. */
6666 68189093 : tree fn = get_function_named_in_call (t);
6667 68187099 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6668 136256885 : && DECL_CXX_CONSTRUCTOR_P (fn))
6669 3764140 : type = DECL_CONTEXT (fn);
6670 : }
6671 454015093 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6672 1346732 : return initialized_type (TREE_OPERAND (t, 1));
6673 452668361 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6674 428016 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6675 520857454 : return cv_unqualified (type);
6676 : }
6677 :
6678 : /* We're about to initialize element INDEX of an array or class from VALUE.
6679 : Set up NEW_CTX appropriately by adjusting .object to refer to the
6680 : subobject and creating a new CONSTRUCTOR if the element is itself
6681 : a class or array. */
6682 :
6683 : static void
6684 2015869 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6685 : tree index, tree &value)
6686 : {
6687 2015869 : new_ctx = *ctx;
6688 :
6689 2015869 : if (index && TREE_CODE (index) != INTEGER_CST
6690 1747762 : && TREE_CODE (index) != FIELD_DECL
6691 3 : && TREE_CODE (index) != RANGE_EXPR)
6692 : /* This won't have an element in the new CONSTRUCTOR. */
6693 : return;
6694 :
6695 2015869 : tree type = initialized_type (value);
6696 2015869 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6697 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6698 : return;
6699 :
6700 601732 : tree ctxtype = NULL_TREE;
6701 601732 : if (ctx->ctor)
6702 601724 : ctxtype = TREE_TYPE (ctx->ctor);
6703 8 : else if (ctx->object)
6704 8 : ctxtype = TREE_TYPE (ctx->object);
6705 : else
6706 0 : gcc_unreachable ();
6707 :
6708 601732 : if (VECTOR_TYPE_P (type)
6709 2884 : && VECTOR_TYPE_P (ctxtype)
6710 923 : && index == NULL_TREE)
6711 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6712 : vector is constructed from smaller vectors, doesn't get its own
6713 : CONSTRUCTOR either. */
6714 : return;
6715 :
6716 : /* The sub-aggregate initializer might contain a placeholder;
6717 : update object to refer to the subobject and ctor to refer to
6718 : the (newly created) sub-initializer. */
6719 600809 : if (ctx->object)
6720 : {
6721 444020 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6722 : /* There's no well-defined subobject for this index. */
6723 3 : new_ctx.object = NULL_TREE;
6724 : else
6725 444017 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6726 : }
6727 :
6728 600809 : if (is_empty_class (type)
6729 600809 : && TREE_CODE (ctxtype) != UNION_TYPE)
6730 : /* Leave ctor null for an empty subobject of a non-union class, they aren't
6731 : represented in the result of evaluation. */
6732 521769 : new_ctx.ctor = NULL_TREE;
6733 : else
6734 : {
6735 79040 : tree elt = build_constructor (type, NULL);
6736 79040 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6737 79040 : new_ctx.ctor = elt;
6738 : }
6739 :
6740 600809 : if (TREE_CODE (value) == TARGET_EXPR)
6741 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6742 55963 : value = TARGET_EXPR_INITIAL (value);
6743 : }
6744 :
6745 : /* We're about to process an initializer for a class or array TYPE. Make
6746 : sure that CTX is set up appropriately. */
6747 :
6748 : static void
6749 1624533 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6750 : {
6751 : /* We don't bother building a ctor for an empty base subobject. */
6752 1624533 : if (is_empty_class (type))
6753 : return;
6754 :
6755 : /* We're in the middle of an initializer that might involve placeholders;
6756 : our caller should have created a CONSTRUCTOR for us to put the
6757 : initializer into. We will either return that constructor or T. */
6758 1109718 : gcc_assert (ctx->ctor);
6759 1109718 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6760 : (type, TREE_TYPE (ctx->ctor)));
6761 : /* We used to check that ctx->ctor was empty, but that isn't the case when
6762 : the object is zero-initialized before calling the constructor. */
6763 1109718 : if (ctx->object)
6764 : {
6765 968899 : tree otype = TREE_TYPE (ctx->object);
6766 968899 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6767 : /* Handle flexible array members. */
6768 : || (TREE_CODE (otype) == ARRAY_TYPE
6769 : && TYPE_DOMAIN (otype) == NULL_TREE
6770 : && TREE_CODE (type) == ARRAY_TYPE
6771 : && (same_type_ignoring_top_level_qualifiers_p
6772 : (TREE_TYPE (type), TREE_TYPE (otype)))));
6773 : }
6774 1109718 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6775 : || ctx->global->get_value (ctx->object) == ctx->ctor);
6776 : }
6777 :
6778 : /* Subroutine of cxx_eval_constant_expression.
6779 : The expression tree T denotes a C-style array or a C-style
6780 : aggregate. Reduce it to a constant expression. */
6781 :
6782 : static tree
6783 1623891 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6784 : value_cat lval,
6785 : bool *non_constant_p, bool *overflow_p,
6786 : tree *jump_target)
6787 : {
6788 1623891 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6789 1623891 : bool changed = false;
6790 1623891 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6791 1623891 : tree type = TREE_TYPE (t);
6792 :
6793 1623891 : constexpr_ctx new_ctx;
6794 1623891 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6795 : {
6796 : /* We don't really need the ctx->ctor business for a PMF or
6797 : vector, but it's simpler to use the same code. */
6798 32072 : new_ctx = *ctx;
6799 32072 : new_ctx.ctor = build_constructor (type, NULL);
6800 32072 : new_ctx.object = NULL_TREE;
6801 32072 : ctx = &new_ctx;
6802 1623891 : };
6803 1623891 : verify_ctor_sanity (ctx, type);
6804 1623891 : vec<constructor_elt, va_gc> **p = nullptr;
6805 1623891 : if (ctx->ctor)
6806 : {
6807 1623883 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6808 3243624 : vec_alloc (*p, vec_safe_length (v));
6809 1623883 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6810 16053 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6811 : }
6812 :
6813 1623891 : unsigned i;
6814 1623891 : tree index, value;
6815 1623891 : bool constant_p = true;
6816 1623891 : bool side_effects_p = false;
6817 3102918 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6818 : {
6819 2009715 : tree orig_value = value;
6820 2009715 : init_subob_ctx (ctx, new_ctx, index, value);
6821 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6822 2009715 : bool no_slot = new_ctx.ctor == NULL_TREE;
6823 2009715 : int pos_hint = -1;
6824 2009715 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6825 : {
6826 : /* If we built a new CONSTRUCTOR, attach it now so that other
6827 : initializers can refer to it. */
6828 73082 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6829 73082 : cep->value = new_ctx.ctor;
6830 73082 : pos_hint = cep - (*p)->begin();
6831 73082 : }
6832 1936633 : else if (TREE_CODE (type) == UNION_TYPE)
6833 : /* Otherwise if we're constructing a non-aggregate union member, set
6834 : the active union member now so that we can later detect and diagnose
6835 : if its initializer attempts to activate another member. */
6836 1116 : get_or_insert_ctor_field (ctx->ctor, index);
6837 2009715 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6838 : lval,
6839 : non_constant_p, overflow_p,
6840 : jump_target);
6841 2009715 : if (*jump_target)
6842 : return NULL_TREE;
6843 : /* Don't VERIFY_CONSTANT here. */
6844 2009712 : if (ctx->quiet && *non_constant_p)
6845 : break;
6846 1479027 : if (elt != orig_value)
6847 497323 : changed = true;
6848 :
6849 1479027 : if (!TREE_CONSTANT (elt))
6850 475933 : constant_p = false;
6851 1479027 : if (TREE_SIDE_EFFECTS (elt))
6852 191 : side_effects_p = true;
6853 1479027 : if (index && TREE_CODE (index) == COMPONENT_REF)
6854 : {
6855 : /* This is an initialization of a vfield inside a base
6856 : subaggregate that we already initialized; push this
6857 : initialization into the previous initialization. */
6858 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6859 0 : inner->value = elt;
6860 0 : changed = true;
6861 0 : }
6862 1479027 : else if (no_slot)
6863 : /* This is an initializer for an empty field; now that we've
6864 : checked that it's constant, we can ignore it. */
6865 : changed = true;
6866 957982 : else if (index
6867 949766 : && (TREE_CODE (index) == NOP_EXPR
6868 949766 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
6869 : {
6870 : /* Old representation of empty bases. FIXME remove. */
6871 0 : gcc_checking_assert (false);
6872 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
6873 : changed = true;
6874 : }
6875 : else
6876 : {
6877 957982 : if (TREE_CODE (type) == UNION_TYPE
6878 957982 : && (*p)->last().index != index)
6879 : /* The initializer erroneously changed the active union member that
6880 : we're initializing. */
6881 7 : gcc_assert (*non_constant_p);
6882 : else
6883 : {
6884 : /* The initializer might have mutated the underlying CONSTRUCTOR,
6885 : so recompute the location of the target constructer_elt. */
6886 957975 : constructor_elt *cep
6887 957975 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6888 957975 : cep->value = elt;
6889 : }
6890 :
6891 : /* Adding or replacing an element might change the ctor's flags. */
6892 957982 : TREE_CONSTANT (ctx->ctor) = constant_p;
6893 957982 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6894 : }
6895 : }
6896 1623888 : if (*non_constant_p)
6897 : return t;
6898 1093152 : if (!changed)
6899 : {
6900 189884 : if (VECTOR_TYPE_P (type))
6901 14351 : t = fold (t);
6902 189884 : return t;
6903 : }
6904 903268 : t = ctx->ctor;
6905 903268 : if (!t)
6906 8 : t = build_constructor (type, NULL);
6907 : /* We're done building this CONSTRUCTOR, so now we can interpret an
6908 : element without an explicit initializer as value-initialized. */
6909 903268 : CONSTRUCTOR_NO_CLEARING (t) = false;
6910 903268 : TREE_CONSTANT (t) = constant_p;
6911 903268 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6912 903268 : if (VECTOR_TYPE_P (type))
6913 4200 : t = fold (t);
6914 : return t;
6915 : }
6916 :
6917 : /* Subroutine of cxx_eval_constant_expression.
6918 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6919 : initialization of a non-static data member of array type. Reduce it to a
6920 : CONSTRUCTOR.
6921 :
6922 : Note that apart from value-initialization (when VALUE_INIT is true),
6923 : this is only intended to support value-initialization and the
6924 : initializations done by defaulted constructors for classes with
6925 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6926 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6927 : for the copy/move constructor. */
6928 :
6929 : static tree
6930 642 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6931 : bool value_init, value_cat lval,
6932 : bool *non_constant_p, bool *overflow_p,
6933 : tree *jump_target)
6934 : {
6935 642 : tree elttype = TREE_TYPE (atype);
6936 642 : verify_ctor_sanity (ctx, atype);
6937 642 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6938 642 : bool pre_init = false;
6939 642 : unsigned HOST_WIDE_INT i;
6940 642 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6941 :
6942 642 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6943 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6944 0 : non_constant_p, overflow_p, jump_target);
6945 :
6946 : /* We already checked access when building the VEC_INIT_EXPR. */
6947 642 : deferring_access_check_sentinel acs (dk_deferred);
6948 :
6949 : /* For the default constructor, build up a call to the default
6950 : constructor of the element type. We only need to handle class types
6951 : here, as for a constructor to be constexpr, all members must be
6952 : initialized, which for a defaulted default constructor means they must
6953 : be of a class type with a constexpr default constructor. */
6954 642 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6955 : /* We only do this at the lowest level. */;
6956 593 : else if (value_init)
6957 : {
6958 182 : init = build_value_init (elttype, complain);
6959 182 : pre_init = true;
6960 : }
6961 411 : else if (!init)
6962 : {
6963 268 : releasing_vec argvec;
6964 268 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6965 : &argvec, elttype, LOOKUP_NORMAL,
6966 : complain);
6967 268 : init = build_aggr_init_expr (elttype, init);
6968 268 : pre_init = true;
6969 268 : }
6970 :
6971 642 : bool zeroed_out = false;
6972 642 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
6973 : {
6974 : /* We're initializing an array object that had been zero-initialized
6975 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
6976 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
6977 : initializers we append to it. */
6978 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
6979 156 : zeroed_out = true;
6980 156 : vec_safe_truncate (*p, 0);
6981 : }
6982 :
6983 642 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
6984 : overflow_p, jump_target);
6985 642 : if (*jump_target)
6986 : return NULL_TREE;
6987 642 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
6988 6365 : for (i = 0; i < max; ++i)
6989 : {
6990 6154 : tree idx = build_int_cst (size_type_node, i);
6991 6154 : tree eltinit;
6992 6154 : bool reuse = false;
6993 6154 : constexpr_ctx new_ctx;
6994 6623 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
6995 6154 : bool no_slot = new_ctx.ctor == NULL_TREE;
6996 6154 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6997 : {
6998 5958 : if (zeroed_out)
6999 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
7000 5958 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
7001 : }
7002 6154 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7003 : {
7004 : /* A multidimensional array; recurse. */
7005 181 : if (value_init || init == NULL_TREE)
7006 : {
7007 171 : eltinit = NULL_TREE;
7008 171 : reuse = i == 0;
7009 : }
7010 : else
7011 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7012 181 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
7013 : value_init, lval, non_constant_p,
7014 : overflow_p, jump_target);
7015 : }
7016 5973 : else if (pre_init)
7017 : {
7018 : /* Initializing an element using value or default initialization
7019 : we just pre-built above. */
7020 5685 : if (init == void_node)
7021 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
7022 3 : return ctx->ctor;
7023 5682 : eltinit = init;
7024 5682 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7025 : /* Clarify what object is being initialized (118285). */
7026 5538 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7027 5682 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7028 : non_constant_p, overflow_p,
7029 : jump_target);
7030 5682 : reuse = i == 0;
7031 : }
7032 : else
7033 : {
7034 : /* Copying an element. */
7035 288 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7036 288 : if (!lvalue_p (init))
7037 257 : eltinit = move (eltinit);
7038 288 : eltinit = (perform_implicit_conversion_flags
7039 288 : (elttype, eltinit, complain,
7040 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7041 281 : if (CLASS_TYPE_P (elttype)
7042 281 : && new_ctx.object
7043 558 : && !error_operand_p (eltinit))
7044 : /* Clarify what object is being initialized (118285). */
7045 266 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7046 288 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7047 : non_constant_p, overflow_p,
7048 : jump_target);
7049 : }
7050 6151 : if (*jump_target)
7051 : return NULL_TREE;
7052 6151 : if (*non_constant_p)
7053 : break;
7054 5987 : if (no_slot)
7055 : {
7056 : /* This is an initializer for an empty subobject; now that we've
7057 : checked that it's constant, we can ignore it. */
7058 18 : gcc_checking_assert (i == 0);
7059 : break;
7060 : }
7061 5969 : else if (new_ctx.ctor != ctx->ctor)
7062 : {
7063 : /* We appended this element above; update the value. */
7064 5831 : gcc_assert ((*p)->last().index == idx);
7065 5831 : (*p)->last().value = eltinit;
7066 : }
7067 : else
7068 138 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7069 : /* Reuse the result of cxx_eval_constant_expression call
7070 : from the first iteration to all others if it is a constant
7071 : initializer that doesn't require relocations. */
7072 5969 : if (reuse
7073 5969 : && max > 1
7074 5969 : && (eltinit == NULL_TREE
7075 399 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7076 399 : == null_pointer_node)))
7077 : {
7078 246 : if (new_ctx.ctor != ctx->ctor)
7079 114 : eltinit = new_ctx.ctor;
7080 246 : tree range = build2 (RANGE_EXPR, size_type_node,
7081 : build_int_cst (size_type_node, 1),
7082 246 : build_int_cst (size_type_node, max - 1));
7083 246 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7084 246 : break;
7085 : }
7086 5723 : else if (i == 0)
7087 208 : vec_safe_reserve (*p, max);
7088 : }
7089 :
7090 639 : if (!*non_constant_p)
7091 : {
7092 475 : init = ctx->ctor;
7093 475 : CONSTRUCTOR_NO_CLEARING (init) = false;
7094 : }
7095 639 : return init;
7096 : }
7097 :
7098 : static tree
7099 524 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7100 : value_cat lval,
7101 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7102 : {
7103 524 : tree atype = TREE_TYPE (t);
7104 524 : tree init = VEC_INIT_EXPR_INIT (t);
7105 524 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7106 524 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7107 : ;
7108 74 : else if (CONSTRUCTOR_NELTS (init) == 0
7109 74 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7110 : {
7111 : /* Handle {} as value-init. */
7112 : init = NULL_TREE;
7113 : value_init = true;
7114 : }
7115 : else
7116 : {
7117 : /* This is a more complicated case, like needing to loop over trailing
7118 : elements; call build_vec_init and evaluate the result. */
7119 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7120 63 : constexpr_ctx new_ctx = *ctx;
7121 63 : if (!ctx->object)
7122 : {
7123 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7124 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7125 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7126 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7127 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7128 51 : ctx->global->put_value (new_ctx.object, ctor);
7129 51 : ctx = &new_ctx;
7130 : }
7131 63 : init = expand_vec_init_expr (ctx->object, t, complain);
7132 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7133 : overflow_p, jump_target);
7134 : }
7135 461 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7136 : lval, non_constant_p, overflow_p, jump_target);
7137 461 : if (*non_constant_p)
7138 : return t;
7139 : else
7140 320 : return r;
7141 : }
7142 :
7143 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7144 : where the desired type is an array of unknown bounds because the variable
7145 : has had its bounds deduced since the wrapping expression was created. */
7146 :
7147 : static bool
7148 252008555 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7149 : {
7150 252008555 : while (TREE_CODE (type1) == ARRAY_TYPE
7151 54425 : && TREE_CODE (type2) == ARRAY_TYPE
7152 252008559 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7153 : {
7154 2 : type1 = TREE_TYPE (type1);
7155 2 : type2 = TREE_TYPE (type2);
7156 : }
7157 252008555 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7158 : }
7159 :
7160 : /* Try to determine the currently active union member for an expression
7161 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7162 : otherwise return NULL_TREE. */
7163 :
7164 : static tree
7165 109 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7166 : {
7167 109 : constexpr_ctx new_ctx = *ctx;
7168 109 : new_ctx.quiet = true;
7169 109 : bool non_constant_p = false, overflow_p = false;
7170 109 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7171 : &non_constant_p,
7172 : &overflow_p, jump_target);
7173 109 : if (*jump_target)
7174 : return NULL_TREE;
7175 109 : if (TREE_CODE (ctor) == CONSTRUCTOR
7176 40 : && CONSTRUCTOR_NELTS (ctor) == 1
7177 16 : && CONSTRUCTOR_ELT (ctor, 0)->index
7178 125 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7179 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7180 : return NULL_TREE;
7181 : }
7182 :
7183 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7184 :
7185 : static tree
7186 9147295 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7187 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7188 : tree *jump_target)
7189 : {
7190 14257691 : tree optype = TREE_TYPE (op);
7191 14257691 : unsigned HOST_WIDE_INT const_nunits;
7192 14257691 : if (off == 0 && similar_type_p (optype, type))
7193 : return op;
7194 9144568 : else if (cxx_dialect >= cxx26
7195 3369204 : && VAR_P (op)
7196 1381178 : && DECL_VTABLE_OR_VTT_P (op)
7197 12841 : && same_type_ignoring_top_level_qualifiers_p (type,
7198 : ptrdiff_type_node)
7199 9145551 : && POINTER_TYPE_P (strip_array_types (optype)))
7200 : {
7201 : /* We often read some virtual table elements using ptrdiff_t rather
7202 : than pointer type. */
7203 983 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7204 : strip_array_types (optype),
7205 : op, off, empty_base,
7206 : jump_target))
7207 983 : return fold_convert (type, ret);
7208 : }
7209 9143585 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7210 9143585 : && similar_type_p (type, TREE_TYPE (optype)))
7211 : {
7212 : /* *(foo *)&complexfoo => __real__ complexfoo */
7213 0 : if (off == 0)
7214 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7215 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7216 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7217 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7218 : }
7219 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7220 9143585 : else if (VECTOR_TYPE_P (optype)
7221 0 : && similar_type_p (type, TREE_TYPE (optype))
7222 9143585 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7223 : {
7224 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7225 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7226 0 : if (off < max_offset && off % part_width == 0)
7227 : {
7228 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7229 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7230 0 : TYPE_SIZE (type), index);
7231 : }
7232 : }
7233 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7234 9143585 : else if (TREE_CODE (optype) == ARRAY_TYPE
7235 5110396 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7236 14253981 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7237 : {
7238 5110396 : tree type_domain = TYPE_DOMAIN (optype);
7239 5110396 : tree min_val = size_zero_node;
7240 5110396 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7241 5110257 : min_val = TYPE_MIN_VALUE (type_domain);
7242 5110396 : unsigned HOST_WIDE_INT el_sz
7243 5110396 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7244 5110396 : unsigned HOST_WIDE_INT idx = off / el_sz;
7245 5110396 : unsigned HOST_WIDE_INT rem = off % el_sz;
7246 5110396 : if (tree_fits_uhwi_p (min_val))
7247 : {
7248 5110396 : tree index = size_int (idx + tree_to_uhwi (min_val));
7249 5110396 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7250 : NULL_TREE, NULL_TREE);
7251 5110396 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7252 5110396 : empty_base, jump_target);
7253 : }
7254 : }
7255 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7256 4033189 : else if (TREE_CODE (optype) == RECORD_TYPE
7257 4033189 : || TREE_CODE (optype) == UNION_TYPE)
7258 : {
7259 4029753 : if (TREE_CODE (optype) == UNION_TYPE)
7260 : /* For unions prefer the currently active member. */
7261 109 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7262 : {
7263 16 : unsigned HOST_WIDE_INT el_sz
7264 16 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7265 16 : if (off < el_sz)
7266 : {
7267 16 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7268 : op, field, NULL_TREE);
7269 16 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7270 : off, empty_base,
7271 : jump_target))
7272 : return ret;
7273 : }
7274 : }
7275 :
7276 : /* Handle conversion to "as base" type. */
7277 4029743 : if (CLASS_TYPE_P (optype)
7278 8059322 : && CLASSTYPE_AS_BASE (optype) == type)
7279 : return op;
7280 :
7281 : /* Handle conversion to an empty base class, which is represented with a
7282 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7283 : which is likely to be a waste of time (109678). */
7284 4027184 : if (is_empty_class (type)
7285 3857183 : && CLASS_TYPE_P (optype)
7286 7884361 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
7287 : {
7288 1893270 : if (empty_base)
7289 1893270 : *empty_base = true;
7290 1893270 : return op;
7291 : }
7292 :
7293 2133914 : for (tree field = TYPE_FIELDS (optype);
7294 25851687 : field; field = DECL_CHAIN (field))
7295 25836127 : if (TREE_CODE (field) == FIELD_DECL
7296 2139702 : && TREE_TYPE (field) != error_mark_node
7297 27975829 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
7298 : {
7299 2139702 : tree pos = byte_position (field);
7300 2139702 : if (!tree_fits_uhwi_p (pos))
7301 0 : continue;
7302 2139702 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
7303 2139702 : unsigned HOST_WIDE_INT el_sz;
7304 2139702 : if (DECL_FIELD_IS_BASE (field)
7305 391214 : && CLASS_TYPE_P (optype)
7306 2530916 : && CLASSTYPE_VBASECLASSES (optype))
7307 6188 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
7308 : else
7309 2133514 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7310 2139702 : if (upos <= off && off < upos + el_sz)
7311 : {
7312 2133794 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7313 : op, field, NULL_TREE);
7314 2133794 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7315 : off - upos,
7316 : empty_base,
7317 : jump_target))
7318 : return ret;
7319 : }
7320 : }
7321 : }
7322 :
7323 : return NULL_TREE;
7324 : }
7325 :
7326 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7327 : match. We want to be less strict for simple *& folding; if we have a
7328 : non-const temporary that we access through a const pointer, that should
7329 : work. We handle this here rather than change fold_indirect_ref_1
7330 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7331 : don't really make sense outside of constant expression evaluation. Also
7332 : we want to allow folding to COMPONENT_REF, which could cause trouble
7333 : with TBAA in fold_indirect_ref_1. */
7334 :
7335 : static tree
7336 116065317 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7337 : tree op0, bool *empty_base, tree *jump_target)
7338 : {
7339 116065317 : tree sub = op0;
7340 116065317 : tree subtype;
7341 :
7342 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7343 243050889 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7344 324767290 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7345 : {
7346 90672416 : if (TREE_CODE (sub) == NOP_EXPR
7347 90672416 : && REINTERPRET_CAST_P (sub))
7348 : return NULL_TREE;
7349 90672416 : sub = TREE_OPERAND (sub, 0);
7350 : }
7351 :
7352 116065317 : subtype = TREE_TYPE (sub);
7353 116065317 : if (!INDIRECT_TYPE_P (subtype))
7354 : return NULL_TREE;
7355 :
7356 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7357 : the innermost component into the offset until it would make the
7358 : offset positive, so that cxx_fold_indirect_ref_1 can identify
7359 : more folding opportunities. */
7360 123077767 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7361 7012502 : if (cxx_dialect >= cxx26)
7362 : {
7363 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7364 : to access virtual base members. */
7365 2337481 : tree nobj = obj;
7366 2337481 : while (TREE_CODE (nobj) == COMPONENT_REF
7367 2356363 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7368 18882 : nobj = TREE_OPERAND (nobj, 0);
7369 2337481 : if (nobj != obj
7370 14743 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7371 2352224 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7372 2041 : while (obj != nobj)
7373 : {
7374 1175 : tree field = TREE_OPERAND (obj, 1);
7375 1175 : tree pos = byte_position (field);
7376 1175 : off = int_const_binop (PLUS_EXPR, off, pos);
7377 1175 : obj = TREE_OPERAND (obj, 0);
7378 : }
7379 : }
7380 9154638 : while (TREE_CODE (obj) == COMPONENT_REF
7381 : /* We need to preserve union member accesses so that we can
7382 : later properly diagnose accessing the wrong member. */
7383 4873488 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7384 13901435 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7385 : {
7386 2218709 : tree field = TREE_OPERAND (obj, 1);
7387 2218709 : tree pos = byte_position (field);
7388 2218709 : if (integer_zerop (off) && integer_nonzerop (pos))
7389 : /* If the offset is already 0, keep going as long as the
7390 : component is at position 0. */
7391 : break;
7392 2142136 : off = int_const_binop (PLUS_EXPR, off, pos);
7393 2142136 : obj = TREE_OPERAND (obj, 0);
7394 : }
7395 7012502 : };
7396 :
7397 116065265 : if (TREE_CODE (sub) == ADDR_EXPR)
7398 : {
7399 51129441 : tree op = TREE_OPERAND (sub, 0);
7400 51129441 : tree optype = TREE_TYPE (op);
7401 :
7402 : /* *&CONST_DECL -> to the value of the const decl. */
7403 51129441 : if (TREE_CODE (op) == CONST_DECL)
7404 0 : return DECL_INITIAL (op);
7405 : /* *&p => p; make sure to handle *&"str"[cst] here. */
7406 51129441 : if (similar_type_p (optype, type))
7407 : {
7408 48459795 : tree fop = fold_read_from_constant_string (op);
7409 48459795 : if (fop)
7410 : return fop;
7411 : else
7412 48357737 : return op;
7413 : }
7414 : else
7415 : {
7416 2669646 : tree off = integer_zero_node;
7417 2669646 : canonicalize_obj_off (op, off);
7418 2669646 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7419 : tree_to_uhwi (off), empty_base,
7420 : jump_target);
7421 : }
7422 : }
7423 64935824 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7424 64935824 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7425 : {
7426 4684499 : tree op00 = TREE_OPERAND (sub, 0);
7427 4684499 : tree off = TREE_OPERAND (sub, 1);
7428 :
7429 4684499 : STRIP_NOPS (op00);
7430 4684499 : if (TREE_CODE (op00) == ADDR_EXPR)
7431 : {
7432 4342856 : tree obj = TREE_OPERAND (op00, 0);
7433 4342856 : canonicalize_obj_off (obj, off);
7434 4342856 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7435 : tree_to_uhwi (off), empty_base,
7436 : jump_target);
7437 : }
7438 : }
7439 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7440 60251325 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7441 60251325 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7442 : {
7443 0 : tree type_domain;
7444 0 : tree min_val = size_zero_node;
7445 0 : tree newsub
7446 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7447 : jump_target);
7448 0 : if (*jump_target)
7449 : return NULL_TREE;
7450 0 : if (newsub)
7451 : sub = newsub;
7452 : else
7453 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7454 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7455 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7456 0 : min_val = TYPE_MIN_VALUE (type_domain);
7457 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7458 0 : NULL_TREE);
7459 : }
7460 :
7461 : return NULL_TREE;
7462 : }
7463 :
7464 : static tree
7465 63688253 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7466 : value_cat lval,
7467 : bool *non_constant_p, bool *overflow_p,
7468 : tree *jump_target)
7469 : {
7470 63688253 : tree orig_op0 = TREE_OPERAND (t, 0);
7471 63688253 : bool empty_base = false;
7472 :
7473 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7474 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7475 :
7476 63688253 : if (TREE_CODE (t) == MEM_REF
7477 63688253 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7478 : {
7479 9 : gcc_assert (ctx->quiet);
7480 9 : *non_constant_p = true;
7481 9 : return t;
7482 : }
7483 :
7484 : /* First try to simplify it directly. */
7485 63688244 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7486 : orig_op0, &empty_base, jump_target);
7487 63688244 : if (*jump_target)
7488 : return NULL_TREE;
7489 63688244 : if (!r)
7490 : {
7491 : /* If that didn't work, evaluate the operand first. */
7492 59963327 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7493 : vc_prvalue, non_constant_p,
7494 : overflow_p, jump_target);
7495 59963327 : if (*jump_target)
7496 : return NULL_TREE;
7497 : /* Don't VERIFY_CONSTANT here. */
7498 59963316 : if (*non_constant_p)
7499 : return t;
7500 :
7501 42145772 : if (!lval && integer_zerop (op0))
7502 : {
7503 70 : if (!ctx->quiet)
7504 12 : error ("dereferencing a null pointer");
7505 70 : *non_constant_p = true;
7506 70 : return t;
7507 : }
7508 :
7509 42145702 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7510 : &empty_base, jump_target);
7511 42145702 : if (*jump_target)
7512 : return NULL_TREE;
7513 42145702 : if (r == NULL_TREE)
7514 : {
7515 : /* We couldn't fold to a constant value. Make sure it's not
7516 : something we should have been able to fold. */
7517 633194 : tree sub = op0;
7518 633194 : STRIP_NOPS (sub);
7519 633194 : if (TREE_CODE (sub) == ADDR_EXPR)
7520 : {
7521 1694 : gcc_assert (!similar_type_p
7522 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7523 : /* DR 1188 says we don't have to deal with this. */
7524 1694 : if (!ctx->quiet)
7525 : {
7526 8 : auto_diagnostic_group d;
7527 13 : error_at (cp_expr_loc_or_input_loc (t),
7528 : "accessing value of %qT object through a %qT "
7529 : "glvalue in a constant expression",
7530 8 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7531 8 : tree ob = build_fold_indirect_ref (sub);
7532 8 : if (DECL_P (ob))
7533 : {
7534 8 : if (DECL_ARTIFICIAL (ob))
7535 1 : inform (DECL_SOURCE_LOCATION (ob),
7536 1 : "%qT object created here", TREE_TYPE (ob));
7537 : else
7538 7 : inform (DECL_SOURCE_LOCATION (ob),
7539 : "%q#D declared here", ob);
7540 : }
7541 8 : }
7542 1694 : *non_constant_p = true;
7543 1694 : return t;
7544 : }
7545 :
7546 631500 : if (lval == vc_glvalue && op0 != orig_op0)
7547 74339 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7548 557161 : if (!lval)
7549 468840 : VERIFY_CONSTANT (t);
7550 557161 : return t;
7551 : }
7552 : }
7553 :
7554 45237425 : r = cxx_eval_constant_expression (ctx, r,
7555 : lval, non_constant_p, overflow_p,
7556 : jump_target);
7557 45237424 : if (*jump_target)
7558 : return NULL_TREE;
7559 45237424 : if (*non_constant_p)
7560 : return t;
7561 :
7562 : /* If we're pulling out the value of an empty base, just return an empty
7563 : CONSTRUCTOR. */
7564 37144234 : if (empty_base && !lval)
7565 : {
7566 16817 : r = build_constructor (TREE_TYPE (t), NULL);
7567 16817 : TREE_CONSTANT (r) = true;
7568 : }
7569 :
7570 : return r;
7571 : }
7572 :
7573 : /* Complain about R, a DECL that is accessed outside its lifetime. */
7574 :
7575 : static void
7576 36 : outside_lifetime_error (location_t loc, tree r)
7577 : {
7578 36 : auto_diagnostic_group d;
7579 36 : if (DECL_NAME (r) == heap_deleted_identifier)
7580 : {
7581 : /* Provide a more accurate message for deleted variables. */
7582 6 : error_at (loc, "use of allocated storage after deallocation "
7583 : "in a constant expression");
7584 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7585 : }
7586 : else
7587 : {
7588 30 : error_at (loc, "accessing %qE outside its lifetime", r);
7589 30 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7590 : }
7591 36 : }
7592 :
7593 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7594 : FUNDEF_P is true if we're checking a constexpr function body.
7595 : Shared between potential_constant_expression and
7596 : cxx_eval_constant_expression. */
7597 :
7598 : static void
7599 343 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7600 : {
7601 343 : auto_diagnostic_group d;
7602 343 : tree type = TREE_TYPE (r);
7603 343 : if (DECL_NAME (r) == heap_uninit_identifier
7604 343 : || DECL_NAME (r) == heap_identifier
7605 340 : || DECL_NAME (r) == heap_vec_uninit_identifier
7606 683 : || DECL_NAME (r) == heap_vec_identifier)
7607 : {
7608 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7609 : "storage is not usable in a constant expression"))
7610 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7611 3 : return;
7612 : }
7613 340 : if (DECL_NAME (r) == heap_deleted_identifier)
7614 : {
7615 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7616 : "deallocation in a constant expression"))
7617 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7618 0 : return;
7619 : }
7620 340 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7621 : "a constant expression", r))
7622 : return;
7623 : /* Avoid error cascade. */
7624 337 : if (DECL_INITIAL (r) == error_mark_node)
7625 : return;
7626 323 : if (DECL_DECLARED_CONSTEXPR_P (r))
7627 3 : inform (DECL_SOURCE_LOCATION (r),
7628 : "%qD used in its own initializer", r);
7629 320 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7630 : {
7631 219 : if (!CP_TYPE_CONST_P (type))
7632 190 : inform (DECL_SOURCE_LOCATION (r),
7633 : "%q#D is not const", r);
7634 29 : else if (CP_TYPE_VOLATILE_P (type))
7635 0 : inform (DECL_SOURCE_LOCATION (r),
7636 : "%q#D is volatile", r);
7637 29 : else if (!DECL_INITIAL (r)
7638 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7639 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7640 29 : inform (DECL_SOURCE_LOCATION (r),
7641 : "%qD was not initialized with a constant "
7642 : "expression", r);
7643 : else
7644 0 : gcc_unreachable ();
7645 : }
7646 101 : else if (TYPE_REF_P (type))
7647 9 : inform (DECL_SOURCE_LOCATION (r),
7648 : "%qD was not initialized with a constant "
7649 : "expression", r);
7650 : else
7651 : {
7652 92 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7653 92 : inform (DECL_SOURCE_LOCATION (r),
7654 : "%qD was not declared %<constexpr%>", r);
7655 : else
7656 0 : inform (DECL_SOURCE_LOCATION (r),
7657 : "%qD does not have integral or enumeration type",
7658 : r);
7659 : }
7660 343 : }
7661 :
7662 : /* Subroutine of cxx_eval_constant_expression.
7663 : Like cxx_eval_unary_expression, except for trinary expressions. */
7664 :
7665 : static tree
7666 15128 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7667 : value_cat lval,
7668 : bool *non_constant_p, bool *overflow_p,
7669 : tree *jump_target)
7670 : {
7671 15128 : int i;
7672 15128 : tree args[3];
7673 15128 : tree val;
7674 :
7675 58319 : for (i = 0; i < 3; i++)
7676 : {
7677 43922 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7678 : lval,
7679 : non_constant_p, overflow_p,
7680 : jump_target);
7681 43922 : if (*jump_target)
7682 : return NULL_TREE;
7683 43922 : VERIFY_CONSTANT (args[i]);
7684 : }
7685 :
7686 14397 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7687 : args[0], args[1], args[2]);
7688 14397 : if (val == NULL_TREE)
7689 : return t;
7690 14397 : VERIFY_CONSTANT (val);
7691 : return val;
7692 : }
7693 :
7694 : /* True if T was declared in a function declared to be constexpr, and
7695 : therefore potentially constant in C++14. */
7696 :
7697 : bool
7698 84570621 : var_in_constexpr_fn (tree t)
7699 : {
7700 84570621 : tree ctx = DECL_CONTEXT (t);
7701 84570621 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7702 161022374 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7703 : }
7704 :
7705 : /* True if a function might be constexpr: either a function that was
7706 : declared constexpr, or a C++17 lambda op(). */
7707 :
7708 : bool
7709 611028807 : maybe_constexpr_fn (tree t)
7710 : {
7711 611028807 : return (DECL_DECLARED_CONSTEXPR_P (t)
7712 191207611 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7713 794431828 : || (flag_implicit_constexpr
7714 92 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7715 : }
7716 :
7717 : /* True if T was declared in a function that might be constexpr: either a
7718 : function that was declared constexpr, or a C++17 lambda op(). */
7719 :
7720 : bool
7721 61864837 : var_in_maybe_constexpr_fn (tree t)
7722 : {
7723 123729076 : return (DECL_FUNCTION_SCOPE_P (t)
7724 112587177 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7725 : }
7726 :
7727 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7728 : build_over_call we implement trivial copy of a class with tail padding using
7729 : assignment of character arrays, which is valid in normal code, but not in
7730 : constexpr evaluation. We don't need to worry about clobbering tail padding
7731 : in constexpr evaluation, so strip the type punning. */
7732 :
7733 : static void
7734 56811270 : maybe_simplify_trivial_copy (tree &target, tree &init)
7735 : {
7736 56811270 : if (TREE_CODE (target) == MEM_REF
7737 3724 : && TREE_CODE (init) == MEM_REF
7738 3560 : && TREE_TYPE (target) == TREE_TYPE (init)
7739 3560 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7740 56814830 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7741 : {
7742 3560 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7743 3560 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7744 : }
7745 56811270 : }
7746 :
7747 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7748 : of constant type. This does not check for 'mutable', so the
7749 : caller is expected to be mindful of that. */
7750 :
7751 : static bool
7752 447 : cref_has_const_field (tree ref)
7753 : {
7754 516 : while (TREE_CODE (ref) == COMPONENT_REF)
7755 : {
7756 507 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7757 : return true;
7758 69 : ref = TREE_OPERAND (ref, 0);
7759 : }
7760 : return false;
7761 : }
7762 :
7763 : /* Return true if we are modifying something that is const during constant
7764 : expression evaluation. CODE is the code of the statement, OBJ is the
7765 : object in question, MUTABLE_P is true if one of the subobjects were
7766 : declared mutable. */
7767 :
7768 : static bool
7769 56341503 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7770 : {
7771 : /* If this is initialization, there's no problem. */
7772 56341503 : if (code != MODIFY_EXPR)
7773 : return false;
7774 :
7775 : /* [basic.type.qualifier] "A const object is an object of type
7776 : const T or a non-mutable subobject of a const object." */
7777 18125448 : if (mutable_p)
7778 : return false;
7779 :
7780 18124712 : if (TREE_READONLY (obj))
7781 : return true;
7782 :
7783 18120791 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7784 : {
7785 : /* Although a COMPONENT_REF may have a const type, we should
7786 : only consider it modifying a const object when any of the
7787 : field components is const. This can happen when using
7788 : constructs such as const_cast<const T &>(m), making something
7789 : const even though it wasn't declared const. */
7790 30750 : if (TREE_CODE (obj) == COMPONENT_REF)
7791 447 : return cref_has_const_field (obj);
7792 : else
7793 : return true;
7794 : }
7795 :
7796 : return false;
7797 : }
7798 :
7799 : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7800 :
7801 : static tree
7802 56811270 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7803 : value_cat lval,
7804 : bool *non_constant_p, bool *overflow_p,
7805 : tree *jump_target)
7806 : {
7807 56811270 : constexpr_ctx new_ctx = *ctx;
7808 :
7809 56811270 : tree init = TREE_OPERAND (t, 1);
7810 :
7811 : /* First we figure out where we're storing to. */
7812 56811270 : tree target = TREE_OPERAND (t, 0);
7813 :
7814 56811270 : maybe_simplify_trivial_copy (target, init);
7815 :
7816 56811270 : tree type = TREE_TYPE (target);
7817 56811270 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7818 45180293 : if (preeval && !TREE_CLOBBER_P (init))
7819 : {
7820 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7821 44685865 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7822 15705698 : && TREE_CODE (init) == CALL_EXPR
7823 2214024 : && CALL_EXPR_FN (init) == NULL_TREE
7824 44685869 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7825 4 : return void_node;
7826 :
7827 : /* Evaluate the value to be stored without knowing what object it will be
7828 : stored in, so that any side-effects happen first. */
7829 44685861 : if (!SCALAR_TYPE_P (type))
7830 66463 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7831 44685861 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7832 : non_constant_p, overflow_p,
7833 : jump_target);
7834 44685859 : if (*jump_target)
7835 : return NULL_TREE;
7836 44685736 : if (*non_constant_p)
7837 : return t;
7838 : }
7839 :
7840 48093811 : bool evaluated = false;
7841 48093811 : if (lval == vc_glvalue)
7842 : {
7843 : /* If we want to return a reference to the target, we need to evaluate it
7844 : as a whole; otherwise, only evaluate the innermost piece to avoid
7845 : building up unnecessary *_REFs. */
7846 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7847 : non_constant_p, overflow_p,
7848 : jump_target);
7849 0 : evaluated = true;
7850 0 : if (*jump_target)
7851 : return NULL_TREE;
7852 0 : if (*non_constant_p)
7853 : return t;
7854 : }
7855 :
7856 : /* Find the underlying variable. */
7857 48093811 : releasing_vec refs;
7858 48093811 : tree object = NULL_TREE;
7859 : /* If we're modifying a const object, save it. */
7860 48093811 : tree const_object_being_modified = NULL_TREE;
7861 48093811 : bool mutable_p = false;
7862 : /* If we see a union, we can't ignore clobbers. */
7863 48093811 : int seen_union = 0;
7864 167233412 : for (tree probe = target; object == NULL_TREE; )
7865 : {
7866 119139973 : switch (TREE_CODE (probe))
7867 : {
7868 22952668 : case BIT_FIELD_REF:
7869 22952668 : case COMPONENT_REF:
7870 22952668 : case ARRAY_REF:
7871 22952668 : {
7872 22952668 : tree ob = TREE_OPERAND (probe, 0);
7873 22952668 : tree elt = TREE_OPERAND (probe, 1);
7874 22952668 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7875 : mutable_p = true;
7876 22952668 : if (TREE_CODE (probe) == ARRAY_REF)
7877 : {
7878 4213765 : elt = eval_and_check_array_index (ctx, probe, false,
7879 : non_constant_p, overflow_p,
7880 : jump_target);
7881 4213765 : if (*jump_target)
7882 66 : return NULL_TREE;
7883 4213765 : if (*non_constant_p)
7884 : return t;
7885 : }
7886 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7887 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7888 : the array isn't const. Instead, check "a" in the next iteration;
7889 : that will detect modifying "const int a[10]". */
7890 18738903 : else if (evaluated
7891 8248064 : && modifying_const_object_p (TREE_CODE (t), probe,
7892 : mutable_p)
7893 18739443 : && const_object_being_modified == NULL_TREE)
7894 : const_object_being_modified = probe;
7895 :
7896 : /* Track named member accesses for unions to validate modifications
7897 : that change active member. */
7898 22952602 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7899 10490839 : vec_safe_push (refs, probe);
7900 : else
7901 12461763 : vec_safe_push (refs, NULL_TREE);
7902 :
7903 22952602 : vec_safe_push (refs, elt);
7904 22952602 : vec_safe_push (refs, TREE_TYPE (probe));
7905 22952602 : probe = ob;
7906 22952602 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
7907 295651 : ++seen_union;
7908 : }
7909 22952602 : break;
7910 :
7911 16 : case REALPART_EXPR:
7912 16 : gcc_assert (refs->is_empty ());
7913 16 : vec_safe_push (refs, NULL_TREE);
7914 16 : vec_safe_push (refs, probe);
7915 16 : vec_safe_push (refs, TREE_TYPE (probe));
7916 16 : probe = TREE_OPERAND (probe, 0);
7917 16 : break;
7918 :
7919 17 : case IMAGPART_EXPR:
7920 17 : gcc_assert (refs->is_empty ());
7921 17 : vec_safe_push (refs, NULL_TREE);
7922 17 : vec_safe_push (refs, probe);
7923 17 : vec_safe_push (refs, TREE_TYPE (probe));
7924 17 : probe = TREE_OPERAND (probe, 0);
7925 17 : break;
7926 :
7927 96187272 : default:
7928 96187272 : if (evaluated)
7929 : object = probe;
7930 : else
7931 : {
7932 48093833 : tree pvar = tree_strip_any_location_wrapper (probe);
7933 48093833 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7934 : {
7935 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7936 : active union member. */
7937 55 : probe = DECL_VALUE_EXPR (pvar);
7938 55 : break;
7939 : }
7940 48093778 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7941 : non_constant_p, overflow_p,
7942 : jump_target);
7943 48093778 : evaluated = true;
7944 48093778 : if (*jump_target)
7945 : return NULL_TREE;
7946 48093778 : if (*non_constant_p)
7947 : return t;
7948 : }
7949 : break;
7950 : }
7951 : }
7952 :
7953 48093439 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7954 48093439 : && const_object_being_modified == NULL_TREE)
7955 48093439 : const_object_being_modified = object;
7956 :
7957 48093439 : if (DECL_P (object)
7958 48092482 : && TREE_CLOBBER_P (init)
7959 48595597 : && DECL_NAME (object) == heap_deleted_identifier)
7960 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
7961 : message later when operator delete is called. */
7962 15 : return void_node;
7963 :
7964 : /* And then find/build up our initializer for the path to the subobject
7965 : we're initializing. */
7966 48093424 : tree *valp;
7967 48093424 : if (DECL_P (object))
7968 48092467 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7969 : else
7970 957 : valp = NULL;
7971 48093424 : if (!valp)
7972 : {
7973 : /* A constant-expression cannot modify objects from outside the
7974 : constant-expression. */
7975 6305 : if (!ctx->quiet)
7976 : {
7977 29 : auto_diagnostic_group d;
7978 29 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
7979 : {
7980 0 : error ("modification of allocated storage after deallocation "
7981 : "is not a constant expression");
7982 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
7983 : }
7984 29 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
7985 : {
7986 14 : if (TREE_CLOBBER_P (init))
7987 6 : error ("destroying %qE outside its lifetime", object);
7988 : else
7989 8 : error ("modification of %qE outside its lifetime "
7990 : "is not a constant expression", object);
7991 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
7992 : }
7993 : else
7994 : {
7995 15 : if (TREE_CLOBBER_P (init))
7996 6 : error ("destroying %qE from outside current evaluation "
7997 : "is not a constant expression", object);
7998 : else
7999 9 : error ("modification of %qE from outside current evaluation "
8000 : "is not a constant expression", object);
8001 : }
8002 29 : }
8003 6305 : *non_constant_p = true;
8004 6305 : return t;
8005 : }
8006 :
8007 : /* Handle explicit end-of-lifetime. */
8008 48087119 : if (TREE_CLOBBER_P (init))
8009 : {
8010 502093 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
8011 502093 : && refs->is_empty ())
8012 : {
8013 119196 : ctx->global->destroy_value (object);
8014 119196 : return void_node;
8015 : }
8016 :
8017 377986 : if (!seen_union && !*valp
8018 386274 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
8019 3343 : return void_node;
8020 :
8021 : /* Ending the lifetime of a const object is OK. */
8022 : const_object_being_modified = NULL_TREE;
8023 : }
8024 :
8025 47964580 : type = TREE_TYPE (object);
8026 47964580 : bool no_zero_init = true;
8027 47964580 : bool zero_padding_bits = false;
8028 :
8029 95929160 : auto_vec<tree *> ctors;
8030 95929160 : releasing_vec indexes;
8031 95929160 : auto_vec<int> index_pos_hints;
8032 47964580 : bool activated_union_member_p = false;
8033 47964580 : bool empty_base = false;
8034 70778021 : while (!refs->is_empty ())
8035 : {
8036 22938075 : if (*valp == NULL_TREE)
8037 : {
8038 2190243 : *valp = build_constructor (type, NULL);
8039 2190243 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8040 2190243 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8041 : }
8042 20747832 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8043 20747832 : TREE_CODE (*valp) == STRING_CST)
8044 : {
8045 : /* An array was initialized with a string constant, and now
8046 : we're writing into one of its elements. Explode the
8047 : single initialization into a set of element
8048 : initializations. */
8049 10977 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8050 :
8051 10977 : tree string = *valp;
8052 10977 : tree elt_type = TREE_TYPE (type);
8053 10977 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8054 10977 : / TYPE_PRECISION (char_type_node));
8055 10977 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8056 10977 : tree ary_ctor = build_constructor (type, NULL);
8057 :
8058 10977 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8059 22108 : for (unsigned ix = 0; ix != num_elts; ix++)
8060 : {
8061 11131 : constructor_elt elt =
8062 : {
8063 11131 : build_int_cst (size_type_node, ix),
8064 11131 : extract_string_elt (string, chars_per_elt, ix)
8065 11131 : };
8066 11131 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8067 : }
8068 :
8069 10977 : *valp = ary_ctor;
8070 : }
8071 :
8072 22938075 : enum tree_code code = TREE_CODE (type);
8073 22938075 : tree reftype = refs->pop();
8074 22938075 : tree index = refs->pop();
8075 22938075 : bool is_access_expr = refs->pop() != NULL_TREE;
8076 :
8077 22938075 : if (code == COMPLEX_TYPE)
8078 : {
8079 33 : if (TREE_CODE (*valp) == COMPLEX_CST)
8080 29 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8081 29 : TREE_IMAGPART (*valp));
8082 4 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8083 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8084 6 : && CONSTRUCTOR_NO_CLEARING (*valp))
8085 : {
8086 2 : tree r = build_constructor (reftype, NULL);
8087 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8088 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8089 : }
8090 33 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8091 33 : ctors.safe_push (valp);
8092 33 : vec_safe_push (indexes, index);
8093 33 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8094 33 : gcc_checking_assert (refs->is_empty ());
8095 : type = reftype;
8096 123713 : break;
8097 : }
8098 :
8099 : /* If the value of object is already zero-initialized, any new ctors for
8100 : subobjects will also be zero-initialized. Similarly with zeroing of
8101 : padding bits. */
8102 22938042 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8103 22938042 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8104 :
8105 22938042 : if (code == RECORD_TYPE && is_empty_field (index))
8106 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8107 : have no data and might have an offset lower than previously declared
8108 : fields, which confuses the middle-end. The code below will notice
8109 : that we don't have a CONSTRUCTOR for our inner target and just
8110 : return init. */
8111 : {
8112 : empty_base = true;
8113 : break;
8114 : }
8115 :
8116 : /* If a union is zero-initialized, its first non-static named data member
8117 : is zero-initialized (and therefore active). */
8118 22814362 : if (code == UNION_TYPE
8119 22814362 : && !no_zero_init
8120 22814362 : && CONSTRUCTOR_NELTS (*valp) == 0)
8121 79 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8122 79 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8123 :
8124 : /* Check for implicit change of active member for a union. */
8125 :
8126 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8127 : now allow initializing an array element to activate the array. */
8128 22850003 : auto only_array_refs = [](const releasing_vec &refs)
8129 : {
8130 35645 : for (unsigned i = 1; i < refs->length(); i += 3)
8131 34 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8132 : return false;
8133 : return true;
8134 : };
8135 :
8136 22814362 : if (code == UNION_TYPE
8137 294901 : && (CONSTRUCTOR_NELTS (*valp) == 0
8138 147491 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8139 : /* An INIT_EXPR of the last member in an access chain is always OK,
8140 : but still check implicit change of members earlier on; see
8141 : cpp2a/constexpr-union6.C. */
8142 22965240 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8143 : {
8144 115267 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8145 115267 : tree inner = strip_array_types (reftype);
8146 :
8147 115267 : if (has_active_member && cxx_dialect < cxx20)
8148 : {
8149 58 : if (!ctx->quiet)
8150 19 : error_at (cp_expr_loc_or_input_loc (t),
8151 : "change of the active member of a union "
8152 : "from %qD to %qD is not a constant expression "
8153 : "before C++20",
8154 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8155 : index);
8156 58 : *non_constant_p = true;
8157 : }
8158 115209 : else if (!is_access_expr
8159 26394 : || (TREE_CLOBBER_P (init)
8160 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8161 141603 : || (TREE_CODE (t) == MODIFY_EXPR
8162 26382 : && CLASS_TYPE_P (inner)
8163 17 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8164 : {
8165 : /* Diagnose changing active union member after initialization
8166 : without a valid member access expression, as described in
8167 : [class.union.general] p5. */
8168 88824 : if (!ctx->quiet)
8169 : {
8170 33 : auto_diagnostic_group d;
8171 33 : if (has_active_member)
8172 24 : error_at (cp_expr_loc_or_input_loc (t),
8173 : "accessing %qD member instead of initialized "
8174 : "%qD member in constant expression",
8175 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8176 : else
8177 9 : error_at (cp_expr_loc_or_input_loc (t),
8178 : "accessing uninitialized member %qD",
8179 : index);
8180 33 : if (is_access_expr)
8181 3 : inform (DECL_SOURCE_LOCATION (index),
8182 : "%qD does not implicitly begin its lifetime "
8183 : "because %qT does not have a non-deleted "
8184 : "trivial default constructor, use "
8185 : "%<std::construct_at%> instead",
8186 : index, inner);
8187 : else
8188 30 : inform (DECL_SOURCE_LOCATION (index),
8189 : "initializing %qD requires a member access "
8190 : "expression as the left operand of the assignment",
8191 : index);
8192 33 : }
8193 88824 : *non_constant_p = true;
8194 : }
8195 26385 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8196 : {
8197 : /* Diagnose changing the active union member while the union
8198 : is in the process of being initialized. */
8199 18 : if (!ctx->quiet)
8200 6 : error_at (cp_expr_loc_or_input_loc (t),
8201 : "change of the active member of a union "
8202 : "from %qD to %qD during initialization",
8203 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8204 : index);
8205 18 : *non_constant_p = true;
8206 : }
8207 : no_zero_init = true;
8208 : }
8209 :
8210 22814362 : ctors.safe_push (valp);
8211 22814362 : vec_safe_push (indexes, index);
8212 :
8213 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8214 : uninitialized. */
8215 21867388 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8216 878813 : && CONSTRUCTOR_NO_CLEARING (*valp)
8217 23593256 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8218 22814362 : constructor_elt *cep
8219 22814362 : = get_or_insert_ctor_field (*valp, index, pos);
8220 22814362 : if (cep == nullptr)
8221 921 : return void_node;
8222 22813441 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8223 :
8224 22813441 : if (code == UNION_TYPE)
8225 : {
8226 294901 : activated_union_member_p = true;
8227 294901 : --seen_union;
8228 : }
8229 :
8230 22813441 : valp = &cep->value;
8231 22813441 : type = reftype;
8232 : }
8233 :
8234 : /* Change an "as-base" clobber to the real type;
8235 : we don't need to worry about padding in constexpr. */
8236 47963659 : tree itype = initialized_type (init);
8237 47963659 : if (IS_FAKE_BASE_TYPE (itype))
8238 2186 : itype = TYPE_CONTEXT (itype);
8239 :
8240 : /* For initialization of an empty base, the original target will be
8241 : *(base*)this, evaluation of which resolves to the object
8242 : argument, which has the derived type rather than the base type. */
8243 95803638 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8244 47839979 : (itype, type)))
8245 : {
8246 12204 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8247 : empty_base = true;
8248 : }
8249 :
8250 : /* Detect modifying a constant object in constexpr evaluation.
8251 : We have found a const object that is being modified. Figure out
8252 : if we need to issue an error. Consider
8253 :
8254 : struct A {
8255 : int n;
8256 : constexpr A() : n(1) { n = 2; } // #1
8257 : };
8258 : struct B {
8259 : const A a;
8260 : constexpr B() { a.n = 3; } // #2
8261 : };
8262 : constexpr B b{};
8263 :
8264 : #1 is OK, since we're modifying an object under construction, but
8265 : #2 is wrong, since "a" is const and has been fully constructed.
8266 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8267 : which means that the object is read-only. For the example above, the
8268 : *ctors stack at the point of #2 will look like:
8269 :
8270 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
8271 : ctors[1] = {.n=2} TREE_READONLY = 1
8272 :
8273 : and we're modifying "b.a", so we search the stack and see if the
8274 : constructor for "b.a" has already run. */
8275 47963659 : if (const_object_being_modified)
8276 : {
8277 33507 : bool fail = false;
8278 33507 : tree const_objtype
8279 33507 : = strip_array_types (TREE_TYPE (const_object_being_modified));
8280 33507 : if (!CLASS_TYPE_P (const_objtype))
8281 : fail = true;
8282 : else
8283 : {
8284 : /* [class.ctor]p5 "A constructor can be invoked for a const,
8285 : volatile, or const volatile object. const and volatile
8286 : semantics are not applied on an object under construction.
8287 : They come into effect when the constructor for the most
8288 : derived object ends." */
8289 100646 : for (tree *elt : ctors)
8290 33842 : if (same_type_ignoring_top_level_qualifiers_p
8291 33842 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
8292 : {
8293 33402 : fail = TREE_READONLY (*elt);
8294 33402 : break;
8295 : }
8296 : }
8297 33402 : if (fail)
8298 : {
8299 198 : if (!ctx->quiet)
8300 63 : modifying_const_object_error (t, const_object_being_modified);
8301 198 : *non_constant_p = true;
8302 198 : return t;
8303 : }
8304 : }
8305 :
8306 47963461 : if (!preeval)
8307 : {
8308 : /* We're handling an INIT_EXPR of class type, so the value of the
8309 : initializer can depend on the object it's initializing. */
8310 :
8311 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
8312 : wants to modify it. */
8313 11622712 : if (*valp == NULL_TREE)
8314 : {
8315 11072416 : *valp = build_constructor (type, NULL);
8316 11072416 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8317 11072416 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8318 : }
8319 11622712 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8320 11622712 : new_ctx.object = target;
8321 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8322 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8323 : expansion of those trees uses ctx instead. */
8324 11622712 : if (TREE_CODE (init) == TARGET_EXPR)
8325 2470741 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8326 2470741 : init = tinit;
8327 11622712 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8328 : non_constant_p, overflow_p,
8329 : jump_target);
8330 11622712 : if (*jump_target)
8331 : return NULL_TREE;
8332 : /* The hash table might have moved since the get earlier, and the
8333 : initializer might have mutated the underlying CONSTRUCTORs, so we must
8334 : recompute VALP. */
8335 11622647 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8336 13793433 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8337 : {
8338 2170786 : ctors[i] = valp;
8339 2170786 : constructor_elt *cep
8340 2170786 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8341 2170786 : valp = &cep->value;
8342 : }
8343 : }
8344 :
8345 47963396 : if (*non_constant_p)
8346 : return t;
8347 :
8348 : /* Don't share a CONSTRUCTOR that might be changed later. */
8349 45854648 : init = unshare_constructor (init);
8350 :
8351 45854648 : gcc_checking_assert (!*valp
8352 : || *valp == void_node
8353 : || (same_type_ignoring_top_level_qualifiers_p
8354 : (TREE_TYPE (*valp), type)));
8355 45854648 : if (empty_base)
8356 : {
8357 : /* Just evaluate the initializer and return, since there's no actual data
8358 : to store, and we didn't build a CONSTRUCTOR. */
8359 128432 : if (!*valp)
8360 : {
8361 : /* But do make sure we have something in *valp. */
8362 0 : *valp = build_constructor (type, nullptr);
8363 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8364 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8365 : }
8366 : }
8367 45726216 : else if (TREE_CLOBBER_P (init))
8368 : {
8369 378633 : if (AGGREGATE_TYPE_P (type))
8370 : {
8371 265812 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
8372 265789 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8373 : else
8374 23 : *valp = build_constructor (type, nullptr);
8375 265812 : TREE_CONSTANT (*valp) = true;
8376 265812 : TREE_SIDE_EFFECTS (*valp) = false;
8377 265812 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8378 265812 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8379 : }
8380 : else
8381 112821 : *valp = void_node;
8382 : }
8383 45347583 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8384 9530071 : && TREE_CODE (init) == CONSTRUCTOR)
8385 : {
8386 : /* An outer ctx->ctor might be pointing to *valp, so replace
8387 : its contents. */
8388 2709323 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8389 2709323 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8390 2709323 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8391 8127969 : CONSTRUCTOR_NO_CLEARING (*valp)
8392 2709323 : = CONSTRUCTOR_NO_CLEARING (init);
8393 8127969 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8394 2709323 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8395 : }
8396 : else
8397 42638260 : *valp = init;
8398 :
8399 : /* After initialization, 'const' semantics apply to the value of the
8400 : object. Make a note of this fact by marking the CONSTRUCTOR
8401 : TREE_READONLY. */
8402 45854648 : if (TREE_CODE (t) == INIT_EXPR
8403 32466492 : && !empty_base
8404 32338060 : && TREE_CODE (*valp) == CONSTRUCTOR
8405 48508289 : && TYPE_READONLY (type))
8406 : {
8407 20810 : tree target_type = TREE_TYPE (target);
8408 20810 : if (IS_FAKE_BASE_TYPE (target_type))
8409 0 : target_type = TYPE_CONTEXT (target_type);
8410 20810 : if (INDIRECT_REF_P (target)
8411 35873 : && (is_this_parameter
8412 15063 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8413 : /* We've just initialized '*this' (perhaps via the target
8414 : constructor of a delegating constructor). Leave it up to the
8415 : caller that set 'this' to set TREE_READONLY appropriately. */
8416 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8417 : (target_type, type) || empty_base);
8418 : else
8419 20787 : TREE_READONLY (*valp) = true;
8420 : }
8421 :
8422 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8423 : CONSTRUCTORs, if any. */
8424 45854648 : bool c = TREE_CONSTANT (init);
8425 45854648 : bool s = TREE_SIDE_EFFECTS (init);
8426 45854648 : if (!indexes->is_empty ())
8427 : {
8428 12954861 : tree last = indexes->last ();
8429 12954861 : if (TREE_CODE (last) == REALPART_EXPR
8430 12954861 : || TREE_CODE (last) == IMAGPART_EXPR)
8431 : {
8432 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8433 : possible. */
8434 33 : tree *cexpr = ctors.last ();
8435 33 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8436 33 : TREE_OPERAND (*cexpr, 0),
8437 33 : TREE_OPERAND (*cexpr, 1)))
8438 31 : *cexpr = c;
8439 : else
8440 : {
8441 4 : TREE_CONSTANT (*cexpr)
8442 2 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8443 2 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8444 4 : TREE_SIDE_EFFECTS (*cexpr)
8445 4 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8446 2 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8447 : }
8448 33 : c = TREE_CONSTANT (*cexpr);
8449 33 : s = TREE_SIDE_EFFECTS (*cexpr);
8450 : }
8451 : }
8452 45854648 : if (!c || s || activated_union_member_p)
8453 28735079 : for (tree *elt : ctors)
8454 : {
8455 6336763 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8456 0 : continue;
8457 6336763 : if (!c)
8458 5484133 : TREE_CONSTANT (*elt) = false;
8459 6336763 : if (s)
8460 0 : TREE_SIDE_EFFECTS (*elt) = true;
8461 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8462 : this union. */
8463 6336763 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8464 205781 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8465 : }
8466 :
8467 45854648 : if (lval)
8468 : return target;
8469 : else
8470 283383 : return init;
8471 48093811 : }
8472 :
8473 : /* Evaluate a ++ or -- expression. */
8474 :
8475 : static tree
8476 5007340 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8477 : value_cat lval,
8478 : bool *non_constant_p, bool *overflow_p,
8479 : tree *jump_target)
8480 : {
8481 5007340 : enum tree_code code = TREE_CODE (t);
8482 5007340 : tree type = TREE_TYPE (t);
8483 5007340 : tree op = TREE_OPERAND (t, 0);
8484 5007340 : tree offset = TREE_OPERAND (t, 1);
8485 5007340 : gcc_assert (TREE_CONSTANT (offset));
8486 :
8487 : /* OFFSET is constant, but perhaps not constant enough. We need to
8488 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8489 5007340 : offset = fold_simple (offset);
8490 :
8491 : /* The operand as an lvalue. */
8492 5007340 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8493 : non_constant_p, overflow_p,
8494 : jump_target);
8495 5007340 : if (*jump_target)
8496 : return NULL_TREE;
8497 :
8498 : /* The operand as an rvalue. */
8499 5007340 : tree val
8500 5007340 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8501 : non_constant_p, overflow_p,
8502 : jump_target);
8503 5007340 : if (*jump_target)
8504 : return NULL_TREE;
8505 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8506 : a local array in a constexpr function. */
8507 5007340 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8508 1929300 : if (!ptr)
8509 1929300 : VERIFY_CONSTANT (val);
8510 :
8511 : /* The modified value. */
8512 4954878 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8513 4954878 : tree mod;
8514 4954878 : if (INDIRECT_TYPE_P (type))
8515 : {
8516 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8517 3078040 : offset = convert_to_ptrofftype (offset);
8518 3078040 : if (!inc)
8519 52566 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8520 3078040 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8521 : }
8522 1876838 : else if (c_promoting_integer_type_p (type)
8523 5085 : && !TYPE_UNSIGNED (type)
8524 1876962 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8525 : {
8526 124 : offset = fold_convert (integer_type_node, offset);
8527 124 : mod = fold_convert (integer_type_node, val);
8528 153 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8529 : mod, offset);
8530 124 : mod = fold_convert (type, t);
8531 124 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8532 9 : TREE_OVERFLOW (mod) = false;
8533 : }
8534 : else
8535 2120357 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8536 4954878 : if (!ptr)
8537 1876838 : VERIFY_CONSTANT (mod);
8538 :
8539 : /* Storing the modified value. */
8540 7276598 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8541 : MODIFY_EXPR, type, op, mod);
8542 4954878 : mod = cxx_eval_constant_expression (ctx, store, lval,
8543 : non_constant_p, overflow_p,
8544 : jump_target);
8545 4954878 : ggc_free (store);
8546 4954878 : if (*jump_target)
8547 : return NULL_TREE;
8548 4954878 : if (*non_constant_p)
8549 : return t;
8550 :
8551 : /* And the value of the expression. */
8552 4890272 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8553 : /* Prefix ops are lvalues, but the caller might want an rvalue;
8554 : lval has already been taken into account in the store above. */
8555 : return mod;
8556 : else
8557 : /* Postfix ops are rvalues. */
8558 281929 : return val;
8559 : }
8560 :
8561 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8562 : STMT matches *jump_target. If we're looking for a case label and we see
8563 : the default label, note it in ctx->css_state. */
8564 :
8565 : static bool
8566 336097 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8567 : {
8568 336097 : switch (TREE_CODE (*jump_target))
8569 : {
8570 90 : case LABEL_DECL:
8571 90 : if (TREE_CODE (stmt) == LABEL_EXPR
8572 90 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8573 : return true;
8574 : break;
8575 :
8576 333898 : case INTEGER_CST:
8577 333898 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8578 : {
8579 333882 : gcc_assert (ctx->css_state != NULL);
8580 333882 : if (!CASE_LOW (stmt))
8581 : {
8582 : /* default: should appear just once in a SWITCH_EXPR
8583 : body (excluding nested SWITCH_EXPR). */
8584 55316 : gcc_assert (*ctx->css_state != css_default_seen);
8585 : /* When evaluating SWITCH_EXPR body for the second time,
8586 : return true for the default: label. */
8587 55316 : if (*ctx->css_state == css_default_processing)
8588 : return true;
8589 27670 : *ctx->css_state = css_default_seen;
8590 : }
8591 278566 : else if (CASE_HIGH (stmt))
8592 : {
8593 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8594 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8595 : return true;
8596 : }
8597 278546 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8598 : return true;
8599 : }
8600 : break;
8601 :
8602 : case BREAK_STMT:
8603 : case CONTINUE_STMT:
8604 : /* These two are handled directly in cxx_eval_loop_expr by testing
8605 : breaks (jump_target) or continues (jump_target). */
8606 : break;
8607 :
8608 : case VAR_DECL:
8609 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8610 : and other places by testing throws (jump_target). */
8611 : break;
8612 :
8613 0 : default:
8614 0 : gcc_unreachable ();
8615 : }
8616 : return false;
8617 : }
8618 :
8619 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8620 : semantics, for switch, break, continue, and return. */
8621 :
8622 : static tree
8623 32416724 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8624 : bool *non_constant_p, bool *overflow_p,
8625 : tree *jump_target)
8626 : {
8627 : /* In a statement-expression we want to return the last value.
8628 : For empty statement expression return void_node. */
8629 32416724 : tree r = void_node;
8630 77650924 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8631 : {
8632 63051208 : tree stmt = *i;
8633 :
8634 : /* We've found a continue, so skip everything until we reach
8635 : the label its jumping to. */
8636 63051208 : if (continues (jump_target))
8637 : {
8638 2199 : if (label_matches (ctx, jump_target, stmt))
8639 : /* Found it. */
8640 18 : *jump_target = NULL_TREE;
8641 : else
8642 2181 : continue;
8643 : }
8644 63049027 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8645 8924316 : continue;
8646 :
8647 54124711 : value_cat lval = vc_discard;
8648 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8649 78047906 : if (tsi_one_before_end_p (i)
8650 23924404 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8651 : lval = vc_prvalue;
8652 :
8653 54124711 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8654 : non_constant_p, overflow_p,
8655 : jump_target);
8656 54124709 : if (*non_constant_p)
8657 : break;
8658 44753500 : if (returns (jump_target)
8659 36329633 : || breaks (jump_target)
8660 45234200 : || throws (jump_target))
8661 : break;
8662 : }
8663 32416722 : return r;
8664 : }
8665 :
8666 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8667 : semantics; continue semantics are covered by cxx_eval_statement_list. */
8668 :
8669 : static tree
8670 3332441 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8671 : bool *non_constant_p, bool *overflow_p,
8672 : tree *jump_target)
8673 : {
8674 3332441 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8675 3332441 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8676 3332441 : unsigned cond_cleanup_depth = 0;
8677 3332441 : int count = 0;
8678 3332441 : switch (TREE_CODE (t))
8679 : {
8680 1618 : case LOOP_EXPR:
8681 1618 : body = LOOP_EXPR_BODY (t);
8682 1618 : break;
8683 2689980 : case DO_STMT:
8684 2689980 : body = DO_BODY (t);
8685 2689980 : cond = DO_COND (t);
8686 2689980 : break;
8687 133989 : case WHILE_STMT:
8688 133989 : body = WHILE_BODY (t);
8689 133989 : cond = WHILE_COND (t);
8690 133989 : cond_prep = WHILE_COND_PREP (t);
8691 133989 : cond_cleanup = WHILE_COND_CLEANUP (t);
8692 133989 : count = -1;
8693 133989 : break;
8694 506854 : case FOR_STMT:
8695 506854 : if (FOR_INIT_STMT (t))
8696 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8697 : non_constant_p, overflow_p, jump_target);
8698 506854 : if (*non_constant_p)
8699 : return NULL_TREE;
8700 506854 : body = FOR_BODY (t);
8701 506854 : cond = FOR_COND (t);
8702 506854 : expr = FOR_EXPR (t);
8703 506854 : cond_prep = FOR_COND_PREP (t);
8704 506854 : cond_cleanup = FOR_COND_CLEANUP (t);
8705 506854 : count = -1;
8706 506854 : break;
8707 0 : default:
8708 0 : gcc_unreachable ();
8709 : }
8710 3332441 : if (cond_prep)
8711 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8712 18456042 : auto cleanup_cond = [&] {
8713 : /* Clean up the condition variable after each iteration. */
8714 15123601 : if (cond_cleanup_depth && !*non_constant_p)
8715 : {
8716 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8717 105 : tree s = BIND_EXPR_BODY (cond_prep);
8718 105 : unsigned i;
8719 210 : for (i = cond_cleanup_depth; i; --i)
8720 : {
8721 105 : tree_stmt_iterator iter = tsi_last (s);
8722 105 : s = tsi_stmt (iter);
8723 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8724 105 : s = CLEANUP_BODY (s);
8725 : }
8726 105 : tree c;
8727 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8728 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8729 : overflow_p, jump_target);
8730 105 : }
8731 15123601 : if (cond_prep)
8732 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8733 222 : decl; decl = DECL_CHAIN (decl))
8734 111 : destroy_value_checked (ctx, decl, non_constant_p);
8735 3332441 : };
8736 12434039 : do
8737 : {
8738 12434039 : if (count != -1)
8739 : {
8740 11793196 : if (body)
8741 11793196 : cxx_eval_constant_expression (ctx, body, vc_discard,
8742 : non_constant_p, overflow_p,
8743 : jump_target);
8744 11793196 : if (breaks (jump_target))
8745 : {
8746 2036 : *jump_target = NULL_TREE;
8747 2036 : break;
8748 : }
8749 :
8750 11791160 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8751 586 : *jump_target = NULL_TREE;
8752 :
8753 11791160 : if (expr)
8754 3982906 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8755 : non_constant_p, overflow_p,
8756 : jump_target);
8757 11791160 : cleanup_cond ();
8758 : }
8759 :
8760 12432003 : if (cond_prep)
8761 : {
8762 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8763 222 : decl; decl = DECL_CHAIN (decl))
8764 111 : ctx->global->clear_value (decl);
8765 111 : if (cond_cleanup)
8766 : {
8767 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8768 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8769 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8770 : at the end of those STATEMENT_LISTs. */
8771 111 : cond_cleanup_depth = 0;
8772 111 : tree s = BIND_EXPR_BODY (cond_prep);
8773 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8774 222 : depth; --depth)
8775 : {
8776 111 : for (tree_stmt_iterator i = tsi_start (s);
8777 321 : !tsi_end_p (i); ++i)
8778 : {
8779 321 : tree stmt = *i;
8780 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8781 0 : continue;
8782 321 : if (tsi_one_before_end_p (i))
8783 : {
8784 : /* The last statement in the STATEMENT_LIST
8785 : has to be a CLEANUP_STMT (verified in
8786 : finish_loop_cond_prep). We want to
8787 : evaluate just its CLEANUP_BODY part but not
8788 : CLEANUP_EXPR part just yet. */
8789 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8790 : /* If the CLEANUP_STMT is not actually to be
8791 : evaluated, don't increment cond_cleanup_depth
8792 : so that we don't evaluate the CLEANUP_EXPR
8793 : for it later either. */
8794 105 : if (*jump_target)
8795 : {
8796 : depth = 1;
8797 : break;
8798 : }
8799 105 : ++cond_cleanup_depth;
8800 : /* If not in the innermost one, next iteration
8801 : will handle CLEANUP_BODY similarly. */
8802 105 : if (depth > 1)
8803 : {
8804 0 : s = CLEANUP_BODY (stmt);
8805 0 : break;
8806 : }
8807 : /* The innermost one can be evaluated normally. */
8808 105 : cxx_eval_constant_expression (ctx,
8809 105 : CLEANUP_BODY (stmt),
8810 : vc_discard,
8811 : non_constant_p,
8812 : overflow_p,
8813 : jump_target);
8814 105 : break;
8815 : }
8816 : /* And so should be evaluated statements which aren't
8817 : last in the STATEMENT_LIST. */
8818 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8819 : non_constant_p, overflow_p,
8820 : jump_target);
8821 216 : if (*non_constant_p
8822 210 : || returns (jump_target)
8823 210 : || breaks (jump_target)
8824 210 : || continues (jump_target)
8825 531 : || throws (jump_target))
8826 : {
8827 : depth = 1;
8828 : break;
8829 : }
8830 : }
8831 : }
8832 : }
8833 : else
8834 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8835 : vc_discard, non_constant_p,
8836 : overflow_p, jump_target);
8837 : }
8838 :
8839 12432003 : if (cond)
8840 : {
8841 12429594 : tree res
8842 12429594 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8843 : non_constant_p, overflow_p,
8844 : jump_target);
8845 12429594 : if (res)
8846 : {
8847 12380016 : if (verify_constant (res, ctx->quiet, non_constant_p,
8848 : overflow_p))
8849 : break;
8850 12239497 : if (integer_zerop (res))
8851 : break;
8852 : }
8853 : else
8854 49578 : gcc_assert (*jump_target);
8855 : }
8856 :
8857 9151430 : if (++count >= constexpr_loop_limit)
8858 : {
8859 27 : if (!ctx->quiet)
8860 9 : error_at (cp_expr_loc_or_input_loc (t),
8861 : "%<constexpr%> loop iteration count exceeds limit of %d "
8862 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8863 : constexpr_loop_limit);
8864 27 : *non_constant_p = true;
8865 27 : break;
8866 : }
8867 : }
8868 27404602 : while (!returns (jump_target)
8869 9101796 : && !breaks (jump_target)
8870 9101796 : && !continues (jump_target)
8871 9101889 : && (!switches (jump_target) || count == 0)
8872 9101766 : && !throws (jump_target)
8873 18302967 : && !*non_constant_p);
8874 :
8875 3332441 : cleanup_cond ();
8876 :
8877 3332441 : return NULL_TREE;
8878 : }
8879 :
8880 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8881 : semantics. */
8882 :
8883 : static tree
8884 34540 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8885 : bool *non_constant_p, bool *overflow_p,
8886 : tree *jump_target)
8887 : {
8888 34540 : tree cond
8889 34540 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8890 34540 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8891 : non_constant_p, overflow_p,
8892 : jump_target);
8893 34540 : if (*jump_target)
8894 : return NULL_TREE;
8895 34540 : VERIFY_CONSTANT (cond);
8896 34297 : if (TREE_CODE (cond) != INTEGER_CST)
8897 : {
8898 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8899 : switch condition even if it's constant enough for other things
8900 : (c++/113545). */
8901 0 : gcc_checking_assert (ctx->quiet);
8902 0 : *non_constant_p = true;
8903 0 : return t;
8904 : }
8905 :
8906 34297 : *jump_target = cond;
8907 :
8908 34297 : tree body
8909 34297 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8910 34297 : constexpr_ctx new_ctx = *ctx;
8911 34297 : constexpr_switch_state css = css_default_not_seen;
8912 34297 : new_ctx.css_state = &css;
8913 34297 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8914 : non_constant_p, overflow_p, jump_target);
8915 61977 : if (switches (jump_target) && css == css_default_seen)
8916 : {
8917 : /* If the SWITCH_EXPR body has default: label, process it once again,
8918 : this time instructing label_matches to return true for default:
8919 : label on switches (jump_target). */
8920 27646 : css = css_default_processing;
8921 27646 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8922 : non_constant_p, overflow_p, jump_target);
8923 : }
8924 34297 : if (breaks (jump_target) || switches (jump_target))
8925 19942 : *jump_target = NULL_TREE;
8926 : return NULL_TREE;
8927 : }
8928 :
8929 : /* Find the object of TYPE under initialization in CTX. */
8930 :
8931 : static tree
8932 16621 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8933 : {
8934 16621 : if (!ctx)
8935 : return NULL_TREE;
8936 :
8937 : /* Prefer the outermost matching object, but don't cross
8938 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8939 16294 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8940 505 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8941 : return outer_ob;
8942 :
8943 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8944 : can is a minor optimization. */
8945 16116 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8946 300 : return ctx->ctor;
8947 :
8948 15816 : if (!ctx->object)
8949 : return NULL_TREE;
8950 :
8951 : /* Since an object cannot have a field of its own type, we can search outward
8952 : from ctx->object to find the unique containing object of TYPE. */
8953 : tree ob = ctx->object;
8954 15804 : while (ob)
8955 : {
8956 15804 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
8957 : break;
8958 219 : if (handled_component_p (ob))
8959 217 : ob = TREE_OPERAND (ob, 0);
8960 : else
8961 : ob = NULL_TREE;
8962 : }
8963 :
8964 : return ob;
8965 : }
8966 :
8967 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
8968 : true, we're checking a constexpr function body. */
8969 :
8970 : static void
8971 34 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
8972 : {
8973 34 : auto_diagnostic_group d;
8974 34 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
8975 : "constant expression"))
8976 34 : inform (loc, "only unevaluated inline assembly is allowed in a "
8977 : "%<constexpr%> function in C++20");
8978 34 : }
8979 :
8980 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
8981 : context; maybe complain about that. */
8982 :
8983 : static void
8984 85137407 : maybe_warn_about_constant_value (location_t loc, tree decl)
8985 : {
8986 85137407 : static bool explained = false;
8987 85137407 : if (cxx_dialect >= cxx17
8988 84973818 : && warn_interference_size
8989 84973818 : && !OPTION_SET_P (param_destruct_interfere_size)
8990 84973818 : && DECL_CONTEXT (decl) == std_node
8991 17561264 : && DECL_NAME (decl)
8992 17561258 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
8993 12 : && (LOCATION_FILE (input_location) != main_input_filename
8994 6 : || module_exporting_p ())
8995 85137410 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
8996 85137416 : && !explained)
8997 : {
8998 6 : explained = true;
8999 6 : inform (loc, "its value can vary between compiler versions or "
9000 : "with different %<-mtune%> or %<-mcpu%> flags");
9001 6 : inform (loc, "if this use is part of a public ABI, change it to "
9002 : "instead use a constant variable you define");
9003 6 : inform (loc, "the default value for the current CPU tuning "
9004 : "is %d bytes", param_destruct_interfere_size);
9005 6 : inform (loc, "you can stabilize this value with %<--param "
9006 : "hardware_destructive_interference_size=%d%>, or disable "
9007 : "this warning with %<-Wno-interference-size%>",
9008 : param_destruct_interfere_size);
9009 : }
9010 85137407 : }
9011 :
9012 : /* For element type ELT_TYPE, return the appropriate type of the heap object
9013 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
9014 : in bytes. If COOKIE_SIZE is NULL, return array type
9015 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
9016 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
9017 : where N is computed such that the size of the struct fits into FULL_SIZE.
9018 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
9019 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
9020 : will be also 0 and so it is not possible to determine the actual array
9021 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
9022 : expression evaluation of subexpressions of ARG_SIZE. */
9023 :
9024 : static tree
9025 64277 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9026 : tree cookie_size, tree full_size, tree arg_size,
9027 : bool *non_constant_p, bool *overflow_p,
9028 : tree *jump_target)
9029 : {
9030 64277 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9031 64277 : gcc_assert (tree_fits_uhwi_p (full_size));
9032 64277 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9033 64277 : if (arg_size)
9034 : {
9035 9 : STRIP_NOPS (arg_size);
9036 9 : if (cookie_size)
9037 : {
9038 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9039 : arg_size = NULL_TREE;
9040 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9041 0 : && tree_int_cst_equal (cookie_size,
9042 0 : TREE_OPERAND (arg_size, 0)))
9043 : {
9044 0 : arg_size = TREE_OPERAND (arg_size, 1);
9045 0 : STRIP_NOPS (arg_size);
9046 : }
9047 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9048 0 : && tree_int_cst_equal (cookie_size,
9049 0 : TREE_OPERAND (arg_size, 1)))
9050 : {
9051 0 : arg_size = TREE_OPERAND (arg_size, 0);
9052 0 : STRIP_NOPS (arg_size);
9053 : }
9054 : else
9055 : arg_size = NULL_TREE;
9056 : }
9057 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9058 : {
9059 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9060 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9061 9 : if (integer_zerop (op0))
9062 9 : arg_size
9063 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9064 : non_constant_p, overflow_p,
9065 : jump_target);
9066 0 : else if (integer_zerop (op1))
9067 0 : arg_size
9068 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9069 : non_constant_p, overflow_p,
9070 : jump_target);
9071 : else
9072 : arg_size = NULL_TREE;
9073 9 : if (*jump_target)
9074 : return NULL_TREE;
9075 : }
9076 : else
9077 : arg_size = NULL_TREE;
9078 : }
9079 :
9080 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9081 64277 : if (!arg_size)
9082 : {
9083 64268 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9084 64268 : gcc_assert (fsz >= csz);
9085 64268 : fsz -= csz;
9086 64268 : if (esz)
9087 64268 : fsz /= esz;
9088 : }
9089 64277 : tree itype2 = build_index_type (size_int (fsz - 1));
9090 64277 : if (!cookie_size)
9091 64268 : return build_cplus_array_type (elt_type, itype2);
9092 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9093 : }
9094 :
9095 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9096 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9097 : the expression which needed the cleanup threw. If both threw, diagnose
9098 : it and return NULL, otherwise return R. If only the cleanup threw, set
9099 : *JUMP_TARGET to the exception object from the cleanup. */
9100 :
9101 : static tree
9102 805827 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9103 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9104 : {
9105 805828 : if (!throws (&jmp_target))
9106 : return r;
9107 7 : if (throws (jump_target))
9108 : {
9109 : /* [except.throw]/9 - If the exception handling mechanism
9110 : handling an uncaught exception directly invokes a function
9111 : that exits via an exception, the function std::terminate is
9112 : invoked. */
9113 6 : if (!ctx->quiet)
9114 : {
9115 2 : auto_diagnostic_group d;
9116 2 : diagnose_std_terminate (loc, ctx, *jump_target);
9117 2 : inform (loc, "destructor exited with an exception");
9118 2 : }
9119 6 : *non_constant_p = true;
9120 6 : *jump_target = NULL_TREE;
9121 6 : return NULL_TREE;
9122 : }
9123 1 : *jump_target = jmp_target;
9124 1 : return r;
9125 : }
9126 :
9127 : /* Attempt to reduce the expression T to a constant value.
9128 : On failure, issue diagnostic and return error_mark_node. */
9129 : /* FIXME unify with c_fully_fold */
9130 : /* FIXME overflow_p is too global */
9131 :
9132 : tree
9133 2016680015 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9134 : value_cat lval,
9135 : bool *non_constant_p, bool *overflow_p,
9136 : tree *jump_target)
9137 : {
9138 2016680015 : if (*jump_target)
9139 : {
9140 : /* If we are jumping, ignore all statements/expressions except those
9141 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9142 1300838 : switch (TREE_CODE (t))
9143 : {
9144 : case BIND_EXPR:
9145 : case STATEMENT_LIST:
9146 : case LOOP_EXPR:
9147 : case COND_EXPR:
9148 : case IF_STMT:
9149 : case DO_STMT:
9150 : case WHILE_STMT:
9151 : case FOR_STMT:
9152 : break;
9153 333898 : case LABEL_EXPR:
9154 333898 : case CASE_LABEL_EXPR:
9155 333898 : if (label_matches (ctx, jump_target, t))
9156 : /* Found it. */
9157 34263 : *jump_target = NULL_TREE;
9158 333898 : return NULL_TREE;
9159 : default:
9160 : return NULL_TREE;
9161 : }
9162 : }
9163 2015596354 : if (error_operand_p (t))
9164 : {
9165 139 : *non_constant_p = true;
9166 139 : return t;
9167 : }
9168 :
9169 : /* Change the input location to the currently processed expression for
9170 : better error messages when a subexpression has no location. */
9171 2015596215 : location_t loc = cp_expr_loc_or_input_loc (t);
9172 4031187017 : iloc_sentinel sentinel (loc);
9173 :
9174 2015596215 : STRIP_ANY_LOCATION_WRAPPER (t);
9175 :
9176 2015596215 : if (CONSTANT_CLASS_P (t))
9177 : {
9178 372695147 : if (TREE_OVERFLOW (t))
9179 : {
9180 62 : if (!ctx->quiet)
9181 15 : permerror (input_location, "overflow in constant expression");
9182 62 : if (!flag_permissive || ctx->quiet)
9183 59 : *overflow_p = true;
9184 : }
9185 :
9186 372695147 : if (TREE_CODE (t) == INTEGER_CST
9187 358188700 : && TYPE_PTR_P (TREE_TYPE (t))
9188 : /* INTEGER_CST with pointer-to-method type is only used
9189 : for a virtual method in a pointer to member function.
9190 : Don't reject those. */
9191 1623817 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9192 374318697 : && !integer_zerop (t))
9193 : {
9194 5 : if (!ctx->quiet)
9195 0 : error ("value %qE of type %qT is not a constant expression",
9196 0 : t, TREE_TYPE (t));
9197 5 : *non_constant_p = true;
9198 : }
9199 :
9200 372695147 : return t;
9201 : }
9202 :
9203 : /* Avoid excessively long constexpr evaluations. */
9204 1642901068 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9205 : {
9206 9 : if (!ctx->quiet)
9207 3 : error_at (loc,
9208 : "%<constexpr%> evaluation operation count exceeds limit of "
9209 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9210 : constexpr_ops_limit);
9211 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9212 9 : *non_constant_p = true;
9213 9 : return t;
9214 : }
9215 :
9216 1642901059 : constexpr_ctx new_ctx;
9217 1642901059 : tree r = t;
9218 :
9219 1642901059 : tree_code tcode = TREE_CODE (t);
9220 1642901059 : switch (tcode)
9221 : {
9222 26034103 : case RESULT_DECL:
9223 26034103 : if (lval)
9224 : return t;
9225 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9226 : through an invisible reference, or in named return value
9227 : optimization. */
9228 39716 : if (tree v = ctx->global->get_value (t))
9229 : return v;
9230 : else
9231 : {
9232 3 : if (!ctx->quiet)
9233 0 : error ("%qE is not a constant expression", t);
9234 3 : *non_constant_p = true;
9235 : }
9236 3 : break;
9237 :
9238 226241363 : case VAR_DECL:
9239 226241363 : if (DECL_HAS_VALUE_EXPR_P (t))
9240 : {
9241 4463045 : if (is_normal_capture_proxy (t)
9242 4463045 : && current_function_decl == DECL_CONTEXT (t))
9243 : {
9244 : /* Function parms aren't constexpr within the function
9245 : definition, so don't try to look at the closure. But if the
9246 : captured variable is constant, try to evaluate it directly. */
9247 343219 : r = DECL_CAPTURED_VARIABLE (t);
9248 : }
9249 : else
9250 4119826 : r = DECL_VALUE_EXPR (t);
9251 :
9252 4463045 : tree type = TREE_TYPE (t);
9253 4463045 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
9254 : {
9255 : /* Adjust r to match the reference-ness of t. */
9256 154022 : if (TYPE_REF_P (type))
9257 151721 : r = build_address (r);
9258 : else
9259 2301 : r = convert_from_reference (r);
9260 : }
9261 4463045 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
9262 4463045 : overflow_p, jump_target);
9263 : }
9264 : /* fall through */
9265 230525838 : case CONST_DECL:
9266 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
9267 : CONST_DECL for aggregate constants. */
9268 230525838 : if (lval)
9269 : return t;
9270 171030883 : else if (t == ctx->object)
9271 1027186 : return ctx->ctor;
9272 170003697 : if (VAR_P (t))
9273 : {
9274 161256177 : if (tree v = ctx->global->get_value (t))
9275 : {
9276 : r = v;
9277 : break;
9278 : }
9279 125731725 : if (ctx->global->is_outside_lifetime (t))
9280 : {
9281 103 : if (!ctx->quiet)
9282 30 : outside_lifetime_error (loc, t);
9283 103 : *non_constant_p = true;
9284 103 : break;
9285 : }
9286 : }
9287 134479142 : if (ctx->manifestly_const_eval == mce_true)
9288 85137407 : maybe_warn_about_constant_value (loc, t);
9289 134479142 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
9290 134479142 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9291 : {
9292 : /* If the class is empty, we aren't actually loading anything. */
9293 134602 : r = build_constructor (TREE_TYPE (t), NULL);
9294 134602 : TREE_CONSTANT (r) = true;
9295 : }
9296 134344540 : else if (ctx->strict)
9297 134066728 : r = decl_really_constant_value (t, /*unshare_p=*/false);
9298 : else
9299 277812 : r = decl_constant_value (t, /*unshare_p=*/false);
9300 134476445 : if (TREE_CODE (r) == TARGET_EXPR
9301 134476445 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9302 0 : r = TARGET_EXPR_INITIAL (r);
9303 134476445 : if (DECL_P (r)
9304 : /* P2280 allows references to unknown. */
9305 134734705 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
9306 : {
9307 30437700 : if (!ctx->quiet)
9308 180 : non_const_var_error (loc, r, /*fundef_p*/false);
9309 30437700 : *non_constant_p = true;
9310 : }
9311 : break;
9312 :
9313 : case DEBUG_BEGIN_STMT:
9314 : /* ??? It might be nice to retain this information somehow, so
9315 : as to be able to step into a constexpr function call. */
9316 : /* Fall through. */
9317 :
9318 : case FUNCTION_DECL:
9319 : case TEMPLATE_DECL:
9320 : case LABEL_DECL:
9321 : case LABEL_EXPR:
9322 : case CASE_LABEL_EXPR:
9323 : case PREDICT_EXPR:
9324 : case OMP_DECLARE_MAPPER:
9325 : case REFLECT_EXPR:
9326 : return t;
9327 :
9328 183529813 : case PARM_DECL:
9329 183529813 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9330 : {
9331 : /* glvalue use. */
9332 8966353 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9333 147157 : if (tree v = ctx->global->get_value (t))
9334 1287927290 : r = v;
9335 : }
9336 174563460 : else if (tree v = ctx->global->get_value (t))
9337 : {
9338 76395351 : r = v;
9339 76395351 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9340 537 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9341 : non_constant_p, overflow_p,
9342 : jump_target);
9343 76395351 : if (*jump_target)
9344 : return NULL_TREE;
9345 : }
9346 98168109 : else if (lval)
9347 : /* Defer in case this is only used for its type. */;
9348 98168109 : else if (ctx->global->is_outside_lifetime (t))
9349 : {
9350 18 : if (!ctx->quiet)
9351 6 : outside_lifetime_error (loc, t);
9352 18 : *non_constant_p = true;
9353 18 : break;
9354 : }
9355 98168091 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9356 98168091 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9357 : {
9358 : /* If the class is empty, we aren't actually loading anything. */
9359 7246 : r = build_constructor (TREE_TYPE (t), NULL);
9360 7246 : TREE_CONSTANT (r) = true;
9361 : }
9362 98160845 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9363 : /* P2280 allows references to unknown... */;
9364 97907649 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9365 : /* ...as well as the this pointer. */;
9366 : else
9367 : {
9368 97500927 : if (!ctx->quiet)
9369 195 : error ("%qE is not a constant expression", t);
9370 97500927 : *non_constant_p = true;
9371 : }
9372 : break;
9373 :
9374 147287123 : case CALL_EXPR:
9375 147287123 : case AGGR_INIT_EXPR:
9376 147287123 : r = cxx_eval_call_expression (ctx, t, lval,
9377 : non_constant_p, overflow_p, jump_target);
9378 147287123 : break;
9379 :
9380 11241275 : case DECL_EXPR:
9381 11241275 : {
9382 11241275 : r = DECL_EXPR_DECL (t);
9383 11241275 : if (TREE_CODE (r) == USING_DECL)
9384 : {
9385 9071 : r = void_node;
9386 9071 : break;
9387 : }
9388 :
9389 11232204 : if (VAR_P (r)
9390 11232204 : && (TREE_STATIC (r)
9391 11108744 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9392 : /* Allow __FUNCTION__ etc. */
9393 123460 : && !DECL_ARTIFICIAL (r)
9394 11232246 : && !decl_constant_var_p (r))
9395 : {
9396 7 : if (!ctx->quiet)
9397 : {
9398 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9399 1 : error_at (loc, "control passes through definition of %qD "
9400 : "with thread storage duration", r);
9401 : else
9402 1 : error_at (loc, "control passes through definition of %qD "
9403 : "with static storage duration", r);
9404 : }
9405 7 : *non_constant_p = true;
9406 7 : break;
9407 : }
9408 :
9409 : /* make_rtl_for_nonlocal_decl could have deferred emission of
9410 : a local static var, but if it appears in a statement expression
9411 : which is constant expression evaluated to e.g. just the address
9412 : of the variable, its DECL_EXPR will never be seen during
9413 : gimple lowering's record_vars_into as the statement expression
9414 : will not be in the IL at all. */
9415 11232197 : if (VAR_P (r)
9416 11232197 : && TREE_STATIC (r)
9417 123453 : && !DECL_REALLY_EXTERN (r)
9418 123442 : && DECL_FUNCTION_SCOPE_P (r)
9419 123442 : && !var_in_maybe_constexpr_fn (r)
9420 11232200 : && decl_constant_var_p (r))
9421 : {
9422 3 : varpool_node *node = varpool_node::get (r);
9423 3 : if (node == NULL || !node->definition)
9424 3 : rest_of_decl_compilation (r, 0, at_eof);
9425 : }
9426 :
9427 22306265 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9428 21207856 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9429 : {
9430 1257644 : new_ctx = *ctx;
9431 1257644 : new_ctx.object = r;
9432 1257644 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9433 1257644 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9434 1257644 : ctx->global->put_value (r, new_ctx.ctor);
9435 1257644 : ctx = &new_ctx;
9436 : }
9437 :
9438 11232197 : if (tree init = DECL_INITIAL (r))
9439 : {
9440 6231730 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9441 : non_constant_p, overflow_p,
9442 : jump_target);
9443 6231730 : if (*jump_target)
9444 : return NULL_TREE;
9445 : /* Don't share a CONSTRUCTOR that might be changed. */
9446 6231730 : init = unshare_constructor (init);
9447 : /* Remember that a constant object's constructor has already
9448 : run. */
9449 12463460 : if (CLASS_TYPE_P (TREE_TYPE (r))
9450 6549582 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9451 36351 : TREE_READONLY (init) = true;
9452 6231730 : ctx->global->put_value (r, init);
9453 : }
9454 5000467 : else if (ctx == &new_ctx)
9455 : /* We gave it a CONSTRUCTOR above. */;
9456 : else
9457 4095310 : ctx->global->put_value (r, NULL_TREE);
9458 : }
9459 : break;
9460 :
9461 20103889 : case TARGET_EXPR:
9462 20103889 : {
9463 20103889 : tree type = TREE_TYPE (t);
9464 :
9465 20103889 : if (!literal_type_p (type))
9466 : {
9467 6300 : if (!ctx->quiet)
9468 : {
9469 0 : auto_diagnostic_group d;
9470 0 : error ("temporary of non-literal type %qT in a "
9471 : "constant expression", type);
9472 0 : explain_non_literal_class (type);
9473 0 : }
9474 6300 : *non_constant_p = true;
9475 10811889 : break;
9476 : }
9477 20097589 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9478 : /* Avoid evaluating a TARGET_EXPR more than once. */
9479 20097589 : tree slot = TARGET_EXPR_SLOT (t);
9480 20097589 : if (tree v = ctx->global->get_value (slot))
9481 : {
9482 1773 : if (lval)
9483 7480946 : return slot;
9484 : r = v;
9485 : break;
9486 : }
9487 20095816 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9488 : {
9489 : /* We're being expanded without an explicit target, so start
9490 : initializing a new object; expansion with an explicit target
9491 : strips the TARGET_EXPR before we get here. */
9492 16180899 : new_ctx = *ctx;
9493 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9494 : any PLACEHOLDER_EXPR within the initializer that refers to the
9495 : former object under construction. */
9496 16180899 : new_ctx.parent = ctx;
9497 16180899 : new_ctx.ctor = build_constructor (type, NULL);
9498 16180899 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9499 16180899 : new_ctx.object = slot;
9500 16180899 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9501 16180899 : ctx = &new_ctx;
9502 : }
9503 :
9504 : /* If the initializer is complex, evaluate it to initialize slot. */
9505 20095816 : bool is_complex = target_expr_needs_replace (t);
9506 20095816 : if (is_complex)
9507 : /* In case no initialization actually happens, clear out any
9508 : void_node from a previous evaluation. */
9509 273 : ctx->global->put_value (slot, NULL_TREE);
9510 :
9511 : /* Pass vc_prvalue because this indicates
9512 : initialization of a temporary. */
9513 20095816 : r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t),
9514 : vc_prvalue, non_constant_p,
9515 : overflow_p, jump_target);
9516 20095816 : if (*non_constant_p)
9517 : break;
9518 9291210 : if (ctx->save_exprs)
9519 4595763 : ctx->save_exprs->safe_push (slot);
9520 9291210 : if (*jump_target)
9521 : {
9522 251 : if (!is_complex
9523 251 : && !(AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9524 : /* If TARGET_EXPR_INITIAL throws exception and slot's value
9525 : has not been changed yet, CLEANUP_POINT_EXPR handling
9526 : could see there void_node from a previous evaluation
9527 : and complain. */
9528 3 : ctx->global->put_value (slot, NULL_TREE);
9529 251 : return NULL_TREE;
9530 : }
9531 9290959 : if (!is_complex)
9532 : {
9533 9290738 : r = unshare_constructor (r);
9534 : /* Adjust the type of the result to the type of the temporary. */
9535 9290738 : r = adjust_temp_type (type, r);
9536 9290738 : ctx->global->put_value (slot, r);
9537 : }
9538 9290959 : if (TARGET_EXPR_CLEANUP (t)
9539 9290959 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9540 : {
9541 770395 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9542 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9543 : them. */
9544 770395 : if (CLEANUP_EH_ONLY (t))
9545 993 : ctx->global->cleanups->safe_push (NULL_TREE);
9546 : }
9547 9290959 : if (lval)
9548 : return slot;
9549 1811054 : if (is_complex)
9550 0 : r = ctx->global->get_value (slot);
9551 : }
9552 1811054 : break;
9553 :
9554 56811270 : case INIT_EXPR:
9555 56811270 : case MODIFY_EXPR:
9556 56811270 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9557 56811270 : r = cxx_eval_store_expression (ctx, t, lval,
9558 : non_constant_p, overflow_p, jump_target);
9559 56811270 : break;
9560 :
9561 0 : case SCOPE_REF:
9562 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9563 : lval,
9564 : non_constant_p, overflow_p,
9565 : jump_target);
9566 0 : break;
9567 :
9568 32840804 : case RETURN_EXPR:
9569 32840804 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9570 32665488 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9571 : lval,
9572 : non_constant_p, overflow_p,
9573 : jump_target);
9574 32840802 : if (!throws (jump_target))
9575 32840663 : *jump_target = t;
9576 : break;
9577 20954 : case BREAK_STMT:
9578 20954 : case CONTINUE_STMT:
9579 20954 : *jump_target = t;
9580 20954 : break;
9581 :
9582 599102 : case SAVE_EXPR:
9583 : /* Avoid evaluating a SAVE_EXPR more than once. */
9584 599102 : if (tree v = ctx->global->get_value (t))
9585 : r = v;
9586 : else
9587 : {
9588 588971 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9589 : vc_prvalue, non_constant_p,
9590 : overflow_p, jump_target);
9591 588971 : if (*non_constant_p || *jump_target)
9592 : break;
9593 7942 : ctx->global->put_value (t, r);
9594 7942 : if (ctx->save_exprs)
9595 5804 : ctx->save_exprs->safe_push (t);
9596 : }
9597 : break;
9598 :
9599 33395824 : case NON_LVALUE_EXPR:
9600 33395824 : case EXPR_STMT:
9601 33395824 : case EH_SPEC_BLOCK:
9602 33395824 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9603 : lval,
9604 : non_constant_p, overflow_p,
9605 : jump_target);
9606 33395824 : break;
9607 :
9608 2266 : case TRY_BLOCK:
9609 2266 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9610 : non_constant_p, overflow_p,
9611 : jump_target);
9612 2266 : if (!*non_constant_p && throws (jump_target))
9613 1733 : if (tree h = TRY_HANDLERS (t))
9614 : {
9615 1733 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9616 1733 : if (TREE_CODE (h) == STATEMENT_LIST)
9617 : {
9618 468 : for (tree stmt : tsi_range (h))
9619 451 : if (TREE_CODE (stmt) == HANDLER
9620 451 : && handler_match_for_exception_type (stmt, type))
9621 : {
9622 : h = stmt;
9623 : break;
9624 : }
9625 230 : if (TREE_CODE (h) == STATEMENT_LIST)
9626 : h = NULL_TREE;
9627 : }
9628 1503 : else if (TREE_CODE (h) != HANDLER
9629 1503 : || !handler_match_for_exception_type (h, type))
9630 : h = NULL_TREE;
9631 : if (h)
9632 : {
9633 1716 : gcc_assert (VAR_P (*jump_target));
9634 1716 : ctx->global->caught_exceptions.safe_push (*jump_target);
9635 1716 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9636 1716 : *jump_target = NULL_TREE;
9637 1716 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9638 : vc_discard, non_constant_p,
9639 : overflow_p, jump_target);
9640 : }
9641 : }
9642 : break;
9643 :
9644 53943718 : case CLEANUP_POINT_EXPR:
9645 53943718 : {
9646 53943718 : auto_vec<tree, 2> cleanups;
9647 53943718 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9648 53943718 : ctx->global->cleanups = &cleanups;
9649 :
9650 53943718 : auto_vec<tree, 10> save_exprs;
9651 53943718 : constexpr_ctx new_ctx = *ctx;
9652 53943718 : new_ctx.save_exprs = &save_exprs;
9653 :
9654 53943718 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9655 : lval,
9656 : non_constant_p, overflow_p,
9657 : jump_target);
9658 :
9659 53943717 : ctx->global->cleanups = prev_cleanups;
9660 53943717 : unsigned int i;
9661 53943717 : tree cleanup, jmp_target = NULL_TREE;
9662 53943717 : bool eh = throws (jump_target);
9663 : /* Evaluate the cleanups. */
9664 108528146 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9665 640712 : if (cleanup == NULL_TREE)
9666 : {
9667 : /* NULL_TREE cleanup is a marker that before it is
9668 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9669 : if the body didn't throw. */
9670 970 : if (!eh)
9671 969 : --i;
9672 : }
9673 : else
9674 639742 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9675 : non_constant_p, overflow_p,
9676 : &jmp_target);
9677 :
9678 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9679 : full-expression. */
9680 166432718 : for (tree save_expr : save_exprs)
9681 4601567 : destroy_value_checked (ctx, save_expr, non_constant_p);
9682 53943717 : if (throws (&jmp_target))
9683 0 : *jump_target = jmp_target;
9684 53943717 : }
9685 53943717 : break;
9686 :
9687 31313493 : case MUST_NOT_THROW_EXPR:
9688 31313493 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9689 : lval,
9690 : non_constant_p, overflow_p,
9691 : jump_target);
9692 31313492 : if (throws (jump_target))
9693 : {
9694 : /* [except.handle]/7 - If the search for a handler exits the
9695 : function body of a function with a non-throwing exception
9696 : specification, the function std::terminate is invoked. */
9697 27 : if (!ctx->quiet)
9698 : {
9699 9 : auto_diagnostic_group d;
9700 9 : diagnose_std_terminate (loc, ctx, *jump_target);
9701 9 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9702 7 : && ctx->call
9703 16 : && ctx->call->fundef)
9704 7 : inform (loc, "uncaught exception exited from %<noexcept%> "
9705 : "function %qD",
9706 : ctx->call->fundef->decl);
9707 2 : else if (MUST_NOT_THROW_THROW_P (t))
9708 1 : inform (loc, "destructor exited with an exception after "
9709 : "initializing the exception object");
9710 1 : else if (MUST_NOT_THROW_CATCH_P (t))
9711 1 : inform (loc, "constructor exited with another exception while "
9712 : "entering handler");
9713 9 : }
9714 27 : *non_constant_p = true;
9715 27 : *jump_target = NULL_TREE;
9716 27 : r = NULL_TREE;
9717 : }
9718 : break;
9719 :
9720 0 : case TRY_CATCH_EXPR:
9721 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9722 : {
9723 0 : r = void_node;
9724 0 : break;
9725 : }
9726 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9727 : non_constant_p, overflow_p,
9728 : jump_target);
9729 0 : if (!*non_constant_p && throws (jump_target))
9730 : {
9731 0 : tree jmp_target = NULL_TREE;
9732 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9733 : non_constant_p, overflow_p,
9734 : &jmp_target);
9735 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9736 : jmp_target);
9737 : }
9738 : break;
9739 :
9740 620 : case TRY_FINALLY_EXPR:
9741 620 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9742 : non_constant_p, overflow_p,
9743 : jump_target);
9744 620 : if (!*non_constant_p)
9745 : {
9746 605 : tree jmp_target = NULL_TREE;
9747 : /* Also evaluate the cleanup. */
9748 605 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9749 605 : && throws (jump_target))
9750 0 : cxx_eval_constant_expression (ctx,
9751 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9752 : 1), vc_discard,
9753 : non_constant_p, overflow_p,
9754 : &jmp_target);
9755 : else
9756 605 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9757 : non_constant_p, overflow_p,
9758 : &jmp_target);
9759 605 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9760 : jmp_target);
9761 : }
9762 : break;
9763 :
9764 0 : case EH_ELSE_EXPR:
9765 : /* Evaluate any cleanup that applies to non-EH exits. */
9766 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9767 : non_constant_p, overflow_p,
9768 : jump_target);
9769 :
9770 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9771 0 : break;
9772 :
9773 2528359 : case CLEANUP_STMT:
9774 2528359 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9775 : non_constant_p, overflow_p,
9776 : jump_target);
9777 2528358 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9778 : {
9779 805222 : iloc_sentinel ils (loc);
9780 805222 : tree jmp_target = NULL_TREE;
9781 : /* Also evaluate the cleanup. */
9782 805222 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9783 : non_constant_p, overflow_p,
9784 : &jmp_target);
9785 805222 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9786 : jmp_target);
9787 805222 : }
9788 : break;
9789 :
9790 : /* These differ from cxx_eval_unary_expression in that this doesn't
9791 : check for a constant operand or result; an address can be
9792 : constant without its operand being, and vice versa. */
9793 63688253 : case MEM_REF:
9794 63688253 : case INDIRECT_REF:
9795 63688253 : r = cxx_eval_indirect_ref (ctx, t, lval,
9796 : non_constant_p, overflow_p,
9797 : jump_target);
9798 63688253 : break;
9799 :
9800 73342340 : case ADDR_EXPR:
9801 73342340 : {
9802 73342340 : tree oldop = TREE_OPERAND (t, 0);
9803 73342340 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9804 : non_constant_p, overflow_p,
9805 : jump_target);
9806 73342340 : if (*jump_target)
9807 : return NULL_TREE;
9808 : /* Don't VERIFY_CONSTANT here. */
9809 73342336 : if (*non_constant_p)
9810 : return t;
9811 55159040 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9812 : /* This function does more aggressive folding than fold itself. */
9813 55159040 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9814 55159040 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9815 : {
9816 39949866 : ggc_free (r);
9817 39949866 : return t;
9818 : }
9819 : break;
9820 : }
9821 :
9822 473327 : case REALPART_EXPR:
9823 473327 : case IMAGPART_EXPR:
9824 473327 : if (lval)
9825 : {
9826 614 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9827 : non_constant_p, overflow_p,
9828 : jump_target);
9829 614 : if (*jump_target)
9830 : return NULL_TREE;
9831 614 : if (r == error_mark_node)
9832 : ;
9833 614 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9834 : r = t;
9835 : else
9836 568 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9837 : break;
9838 : }
9839 : /* FALLTHRU */
9840 21362241 : case CONJ_EXPR:
9841 21362241 : case FIX_TRUNC_EXPR:
9842 21362241 : case FLOAT_EXPR:
9843 21362241 : case NEGATE_EXPR:
9844 21362241 : case ABS_EXPR:
9845 21362241 : case ABSU_EXPR:
9846 21362241 : case BIT_NOT_EXPR:
9847 21362241 : case TRUTH_NOT_EXPR:
9848 21362241 : case FIXED_CONVERT_EXPR:
9849 21362241 : case VEC_DUPLICATE_EXPR:
9850 21362241 : r = cxx_eval_unary_expression (ctx, t, lval,
9851 : non_constant_p, overflow_p,
9852 : jump_target);
9853 21362241 : break;
9854 :
9855 8612772 : case SIZEOF_EXPR:
9856 8612772 : r = fold_sizeof_expr (t);
9857 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9858 : which could lead to an infinite recursion. */
9859 8612772 : if (TREE_CODE (r) != SIZEOF_EXPR)
9860 8612772 : r = cxx_eval_constant_expression (ctx, r, lval,
9861 : non_constant_p, overflow_p,
9862 : jump_target);
9863 : else
9864 : {
9865 0 : *non_constant_p = true;
9866 0 : gcc_assert (ctx->quiet);
9867 : }
9868 :
9869 : break;
9870 :
9871 7156702 : case COMPOUND_EXPR:
9872 7156702 : {
9873 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9874 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9875 : introduced by build_call_a. */
9876 7156702 : tree op0 = TREE_OPERAND (t, 0);
9877 7156702 : tree op1 = TREE_OPERAND (t, 1);
9878 7156702 : STRIP_NOPS (op1);
9879 2356548 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9880 8226754 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9881 4148477 : r = cxx_eval_constant_expression (ctx, op0,
9882 : lval, non_constant_p, overflow_p,
9883 : jump_target);
9884 : else
9885 : {
9886 : /* Check that the LHS is constant and then discard it. */
9887 3008225 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9888 : non_constant_p, overflow_p,
9889 : jump_target);
9890 3008225 : if (*jump_target)
9891 : return NULL_TREE;
9892 3004751 : if (*non_constant_p)
9893 : return t;
9894 2752538 : op1 = TREE_OPERAND (t, 1);
9895 2752538 : r = cxx_eval_constant_expression (ctx, op1,
9896 : lval, non_constant_p, overflow_p,
9897 : jump_target);
9898 : }
9899 : }
9900 : break;
9901 :
9902 78328437 : case POINTER_PLUS_EXPR:
9903 78328437 : case POINTER_DIFF_EXPR:
9904 78328437 : case PLUS_EXPR:
9905 78328437 : case MINUS_EXPR:
9906 78328437 : case MULT_EXPR:
9907 78328437 : case TRUNC_DIV_EXPR:
9908 78328437 : case CEIL_DIV_EXPR:
9909 78328437 : case FLOOR_DIV_EXPR:
9910 78328437 : case ROUND_DIV_EXPR:
9911 78328437 : case TRUNC_MOD_EXPR:
9912 78328437 : case CEIL_MOD_EXPR:
9913 78328437 : case ROUND_MOD_EXPR:
9914 78328437 : case RDIV_EXPR:
9915 78328437 : case EXACT_DIV_EXPR:
9916 78328437 : case MIN_EXPR:
9917 78328437 : case MAX_EXPR:
9918 78328437 : case LSHIFT_EXPR:
9919 78328437 : case RSHIFT_EXPR:
9920 78328437 : case LROTATE_EXPR:
9921 78328437 : case RROTATE_EXPR:
9922 78328437 : case BIT_IOR_EXPR:
9923 78328437 : case BIT_XOR_EXPR:
9924 78328437 : case BIT_AND_EXPR:
9925 78328437 : case TRUTH_XOR_EXPR:
9926 78328437 : case LT_EXPR:
9927 78328437 : case LE_EXPR:
9928 78328437 : case GT_EXPR:
9929 78328437 : case GE_EXPR:
9930 78328437 : case EQ_EXPR:
9931 78328437 : case NE_EXPR:
9932 78328437 : case SPACESHIP_EXPR:
9933 78328437 : case UNORDERED_EXPR:
9934 78328437 : case ORDERED_EXPR:
9935 78328437 : case UNLT_EXPR:
9936 78328437 : case UNLE_EXPR:
9937 78328437 : case UNGT_EXPR:
9938 78328437 : case UNGE_EXPR:
9939 78328437 : case UNEQ_EXPR:
9940 78328437 : case LTGT_EXPR:
9941 78328437 : case RANGE_EXPR:
9942 78328437 : case COMPLEX_EXPR:
9943 78328437 : r = cxx_eval_binary_expression (ctx, t, lval,
9944 : non_constant_p, overflow_p,
9945 : jump_target);
9946 78328437 : break;
9947 :
9948 : /* fold can introduce non-IF versions of these; still treat them as
9949 : short-circuiting. */
9950 8789237 : case TRUTH_AND_EXPR:
9951 8789237 : case TRUTH_ANDIF_EXPR:
9952 8789237 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
9953 : boolean_true_node,
9954 : non_constant_p, overflow_p,
9955 : jump_target);
9956 8789237 : break;
9957 :
9958 1979239 : case TRUTH_OR_EXPR:
9959 1979239 : case TRUTH_ORIF_EXPR:
9960 1979239 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
9961 : boolean_false_node,
9962 : non_constant_p, overflow_p,
9963 : jump_target);
9964 1979239 : break;
9965 :
9966 8370362 : case ARRAY_REF:
9967 8370362 : r = cxx_eval_array_reference (ctx, t, lval,
9968 : non_constant_p, overflow_p,
9969 : jump_target);
9970 8370362 : break;
9971 :
9972 69567496 : case COMPONENT_REF:
9973 69567496 : if (is_overloaded_fn (t))
9974 : {
9975 : /* We can only get here in checking mode via
9976 : build_non_dependent_expr, because any expression that
9977 : calls or takes the address of the function will have
9978 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9979 6 : gcc_checking_assert (ctx->quiet || errorcount);
9980 6 : *non_constant_p = true;
9981 6 : return t;
9982 : }
9983 69567490 : r = cxx_eval_component_reference (ctx, t, lval,
9984 : non_constant_p, overflow_p,
9985 : jump_target);
9986 69567490 : break;
9987 :
9988 16029 : case BIT_FIELD_REF:
9989 16029 : r = cxx_eval_bit_field_ref (ctx, t, lval,
9990 : non_constant_p, overflow_p,
9991 : jump_target);
9992 16029 : break;
9993 :
9994 19318152 : case COND_EXPR:
9995 19318152 : case IF_STMT:
9996 19318152 : if (*jump_target)
9997 : {
9998 151144 : tree orig_jump = *jump_target;
9999 151144 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
10000 302288 : ? TREE_OPERAND (t, 1) : void_node);
10001 : /* When jumping to a label, the label might be either in the
10002 : then or else blocks, so process then block first in skipping
10003 : mode first, and if we are still in the skipping mode at its end,
10004 : process the else block too. */
10005 151144 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10006 : overflow_p, jump_target);
10007 : /* It's possible that we found the label in the then block. But
10008 : it could have been followed by another jumping statement, e.g.
10009 : say we're looking for case 1:
10010 : if (cond)
10011 : {
10012 : // skipped statements
10013 : case 1:; // clears up *jump_target
10014 : return 1; // and sets it to a RETURN_EXPR
10015 : }
10016 : else { ... }
10017 : in which case we need not go looking to the else block.
10018 : (goto is not allowed in a constexpr function.) */
10019 151144 : if (*jump_target == orig_jump)
10020 : {
10021 151192 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
10022 151192 : ? TREE_OPERAND (t, 2) : void_node);
10023 151110 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10024 : overflow_p, jump_target);
10025 : }
10026 : break;
10027 : }
10028 19167008 : r = cxx_eval_conditional_expression (ctx, t, lval,
10029 : non_constant_p, overflow_p,
10030 : jump_target);
10031 19167008 : break;
10032 4788 : case VEC_COND_EXPR:
10033 4788 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10034 : overflow_p, jump_target);
10035 4788 : break;
10036 :
10037 21785731 : case CONSTRUCTOR:
10038 21785731 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10039 : {
10040 : /* Don't re-process a constant CONSTRUCTOR. */
10041 20162179 : verify_constructor_flags (t);
10042 20162179 : if (TREE_CONSTANT (t))
10043 : return t;
10044 : }
10045 1623552 : if (TREE_CLOBBER_P (t))
10046 : {
10047 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10048 : a clobber by itself isn't a constant-expression. */
10049 0 : gcc_assert (ctx->quiet);
10050 0 : *non_constant_p = true;
10051 0 : break;
10052 : }
10053 1623552 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10054 : non_constant_p, overflow_p, jump_target);
10055 1623552 : break;
10056 :
10057 524 : case VEC_INIT_EXPR:
10058 : /* We can get this in a defaulted constructor for a class with a
10059 : non-static data member of array type. Either the initializer will
10060 : be NULL, meaning default-initialization, or it will be an lvalue
10061 : or xvalue of the same type, meaning direct-initialization from the
10062 : corresponding member. */
10063 524 : r = cxx_eval_vec_init (ctx, t, lval,
10064 : non_constant_p, overflow_p, jump_target);
10065 524 : break;
10066 :
10067 15128 : case VEC_PERM_EXPR:
10068 15128 : r = cxx_eval_trinary_expression (ctx, t, lval,
10069 : non_constant_p, overflow_p,
10070 : jump_target);
10071 15128 : break;
10072 :
10073 4 : case PAREN_EXPR:
10074 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
10075 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10076 : constant expressions since it's unaffected by -fassociative-math. */
10077 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10078 : non_constant_p, overflow_p,
10079 : jump_target);
10080 4 : break;
10081 :
10082 298952436 : case NOP_EXPR:
10083 298952436 : if (REINTERPRET_CAST_P (t))
10084 : {
10085 73 : if (!ctx->quiet)
10086 6 : error_at (loc,
10087 : "%<reinterpret_cast%> is not a constant expression");
10088 73 : *non_constant_p = true;
10089 73 : return t;
10090 : }
10091 : /* FALLTHROUGH. */
10092 358965532 : case CONVERT_EXPR:
10093 358965532 : case VIEW_CONVERT_EXPR:
10094 358965532 : case UNARY_PLUS_EXPR:
10095 358965532 : {
10096 358965532 : tree oldop = TREE_OPERAND (t, 0);
10097 :
10098 358965532 : tree op = cxx_eval_constant_expression (ctx, oldop,
10099 358965532 : VOID_TYPE_P (TREE_TYPE (t))
10100 : ? vc_discard
10101 : : tcode == VIEW_CONVERT_EXPR
10102 337368076 : ? lval : vc_prvalue,
10103 : non_constant_p, overflow_p,
10104 : jump_target);
10105 358962834 : if (*jump_target)
10106 : return NULL_TREE;
10107 358960803 : if (*non_constant_p)
10108 : return t;
10109 263069512 : tree type = TREE_TYPE (t);
10110 :
10111 263069512 : if (VOID_TYPE_P (type))
10112 20275662 : return void_node;
10113 :
10114 242793850 : if (TREE_CODE (t) == CONVERT_EXPR
10115 21506742 : && ARITHMETIC_TYPE_P (type)
10116 4127649 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10117 242817308 : && ctx->strict)
10118 : {
10119 23366 : if (!ctx->quiet)
10120 54 : error_at (loc,
10121 : "conversion from pointer type %qT to arithmetic type "
10122 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10123 23366 : *non_constant_p = true;
10124 23366 : return t;
10125 : }
10126 :
10127 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10128 : type cannot be part of a core constant expression as a resolution to
10129 : DR 1312. */
10130 77383541 : if (TYPE_PTROB_P (type)
10131 75250834 : && TYPE_PTR_P (TREE_TYPE (op))
10132 56783006 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10133 : /* Inside a call to std::construct_at,
10134 : std::allocator<T>::{,de}allocate, or
10135 : std::source_location::current, we permit casting from void*
10136 : because that is compiler-generated code. */
10137 750514 : && !is_std_construct_at (ctx->call)
10138 82662 : && !is_std_allocator_allocate (ctx->call)
10139 242812593 : && !is_std_source_location_current (ctx->call))
10140 : {
10141 : /* Likewise, don't error when casting from void* when OP is
10142 : &heap uninit and similar. */
10143 41950 : tree sop = tree_strip_nop_conversions (op);
10144 41950 : tree decl = NULL_TREE;
10145 41950 : if (TREE_CODE (sop) == ADDR_EXPR)
10146 32538 : decl = TREE_OPERAND (sop, 0);
10147 32538 : if (decl
10148 32538 : && VAR_P (decl)
10149 32183 : && DECL_ARTIFICIAL (decl)
10150 64682 : && (DECL_NAME (decl) == heap_identifier
10151 25084 : || DECL_NAME (decl) == heap_uninit_identifier
10152 2903 : || DECL_NAME (decl) == heap_vec_identifier
10153 1559 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
10154 : /* OK */;
10155 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10156 : cv void" to a pointer-to-object type T unless P is a null
10157 : pointer value or points to an object whose type is similar to
10158 : T. */
10159 9822 : else if (cxx_dialect > cxx23)
10160 : {
10161 9670 : if (integer_zerop (sop))
10162 28 : return build_int_cst (type, 0);
10163 9642 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10164 : NULL, jump_target);
10165 9642 : if (*jump_target)
10166 : return NULL_TREE;
10167 9642 : if (r)
10168 : {
10169 9607 : r = build1 (ADDR_EXPR, type, r);
10170 9607 : break;
10171 : }
10172 35 : if (!ctx->quiet)
10173 : {
10174 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10175 3 : auto_diagnostic_group d;
10176 3 : error_at (loc, "cast from %qT is not allowed in a "
10177 : "constant expression because "
10178 : "pointed-to type %qT is not similar to %qT",
10179 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10180 3 : TREE_TYPE (type));
10181 3 : tree obj = build_fold_indirect_ref (sop);
10182 3 : if (TREE_CODE (obj) == COMPONENT_REF)
10183 2 : obj = TREE_OPERAND (obj, 1);
10184 3 : if (DECL_P (obj))
10185 3 : inform (DECL_SOURCE_LOCATION (obj),
10186 : "pointed-to object declared here");
10187 3 : }
10188 35 : *non_constant_p = true;
10189 35 : return t;
10190 : }
10191 : else
10192 : {
10193 152 : if (!ctx->quiet)
10194 26 : error_at (loc, "cast from %qT is not allowed in a "
10195 : "constant expression before C++26",
10196 26 : TREE_TYPE (op));
10197 152 : *non_constant_p = true;
10198 152 : return t;
10199 : }
10200 : }
10201 :
10202 242760662 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
10203 : {
10204 1019 : op = cplus_expand_constant (op);
10205 1019 : if (TREE_CODE (op) == PTRMEM_CST)
10206 : {
10207 21 : if (!ctx->quiet)
10208 3 : error_at (loc, "%qE is not a constant expression when the "
10209 : "class %qT is still incomplete", op,
10210 3 : PTRMEM_CST_CLASS (op));
10211 21 : *non_constant_p = true;
10212 21 : return t;
10213 : }
10214 : }
10215 :
10216 242760641 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
10217 : {
10218 684 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
10219 684 : && !can_convert_qual (type, op))
10220 6 : op = cplus_expand_constant (op);
10221 684 : return cp_fold_convert (type, op);
10222 : }
10223 :
10224 242759957 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
10225 : {
10226 569199 : if (integer_zerop (op))
10227 : {
10228 514305 : if (TYPE_REF_P (type))
10229 : {
10230 38 : if (!ctx->quiet)
10231 4 : error_at (loc, "dereferencing a null pointer");
10232 38 : *non_constant_p = true;
10233 38 : return t;
10234 : }
10235 : }
10236 54894 : else if (TYPE_PTR_P (type)
10237 54894 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10238 : /* INTEGER_CST with pointer-to-method type is only used
10239 : for a virtual method in a pointer to member function.
10240 : Don't reject those. */
10241 : ;
10242 : else
10243 : {
10244 : /* This detects for example:
10245 : reinterpret_cast<void*>(sizeof 0)
10246 : */
10247 54891 : if (!ctx->quiet)
10248 15 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
10249 : "a constant expression",
10250 : type, op);
10251 54891 : *non_constant_p = true;
10252 54891 : return t;
10253 : }
10254 : }
10255 :
10256 242705028 : if (INDIRECT_TYPE_P (type)
10257 117769730 : && TREE_CODE (op) == NOP_EXPR
10258 59973225 : && TREE_TYPE (op) == ptr_type_node
10259 264013 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
10260 260921 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
10261 242911357 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10262 206329 : 0)) == heap_uninit_identifier
10263 143595 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10264 143595 : 0)) == heap_vec_uninit_identifier))
10265 : {
10266 64277 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
10267 64277 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
10268 64277 : tree elt_type = TREE_TYPE (type);
10269 64277 : tree cookie_size = NULL_TREE;
10270 64277 : tree arg_size = NULL_TREE;
10271 64277 : if (TREE_CODE (elt_type) == RECORD_TYPE
10272 64277 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
10273 : {
10274 9 : tree fld1 = TYPE_FIELDS (elt_type);
10275 9 : tree fld2 = DECL_CHAIN (fld1);
10276 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
10277 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
10278 : }
10279 64277 : DECL_NAME (var)
10280 64277 : = (DECL_NAME (var) == heap_uninit_identifier
10281 64277 : ? heap_identifier : heap_vec_identifier);
10282 : /* For zero sized elt_type, try to recover how many outer_nelts
10283 : it should have. */
10284 64277 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
10285 64268 : : integer_zerop (var_size))
10286 89 : && !int_size_in_bytes (elt_type)
10287 9 : && TREE_CODE (oldop) == CALL_EXPR
10288 64295 : && call_expr_nargs (oldop) >= 1)
10289 9 : if (tree fun = get_function_named_in_call (oldop))
10290 9 : if (cxx_replaceable_global_alloc_fn (fun)
10291 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
10292 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
10293 64277 : tree new_type
10294 64277 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
10295 : var_size, arg_size,
10296 : non_constant_p, overflow_p,
10297 : jump_target);
10298 64277 : if (*jump_target)
10299 : return NULL_TREE;
10300 64277 : TREE_TYPE (var) = new_type;
10301 64277 : TREE_TYPE (TREE_OPERAND (op, 0))
10302 128554 : = build_pointer_type (TREE_TYPE (var));
10303 : }
10304 :
10305 : /* This can happen for std::meta::info(^^int) where the cast has no
10306 : meaning. */
10307 242705028 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
10308 : {
10309 : r = op;
10310 : break;
10311 : }
10312 :
10313 242656830 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
10314 : /* We didn't fold at the top so we could check for ptr-int
10315 : conversion. */
10316 20103139 : return fold (t);
10317 :
10318 222553691 : tree sop;
10319 :
10320 : /* Handle an array's bounds having been deduced after we built
10321 : the wrapping expression. */
10322 222553691 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
10323 : r = op;
10324 71629928 : else if (sop = tree_strip_nop_conversions (op),
10325 101084792 : sop != op && (same_type_ignoring_tlq_and_bounds_p
10326 29454864 : (type, TREE_TYPE (sop))))
10327 : r = sop;
10328 61560249 : else if (tcode == UNARY_PLUS_EXPR)
10329 0 : r = fold_convert (TREE_TYPE (t), op);
10330 : else
10331 61560249 : r = fold_build1 (tcode, type, op);
10332 :
10333 : /* Conversion of an out-of-range value has implementation-defined
10334 : behavior; the language considers it different from arithmetic
10335 : overflow, which is undefined. */
10336 222553691 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10337 12230 : TREE_OVERFLOW (r) = false;
10338 : }
10339 : break;
10340 :
10341 6450 : case EXCESS_PRECISION_EXPR:
10342 6450 : {
10343 6450 : tree oldop = TREE_OPERAND (t, 0);
10344 :
10345 6450 : tree op = cxx_eval_constant_expression (ctx, oldop,
10346 : lval,
10347 : non_constant_p, overflow_p,
10348 : jump_target);
10349 6450 : if (*jump_target)
10350 : return NULL_TREE;
10351 6450 : if (*non_constant_p)
10352 : return t;
10353 6442 : r = fold_convert (TREE_TYPE (t), op);
10354 6442 : break;
10355 : }
10356 :
10357 38740 : case EMPTY_CLASS_EXPR:
10358 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10359 : it to an appropriate CONSTRUCTOR. */
10360 38740 : return build_constructor (TREE_TYPE (t), NULL);
10361 :
10362 32416724 : case STATEMENT_LIST:
10363 32416724 : new_ctx = *ctx;
10364 32416724 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10365 32416724 : return cxx_eval_statement_list (&new_ctx, t,
10366 32416722 : non_constant_p, overflow_p, jump_target);
10367 :
10368 19557717 : case BIND_EXPR:
10369 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10370 : map, so that when checking whether they're already destroyed later we
10371 : don't get confused by remnants of previous calls. */
10372 28380764 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10373 8823047 : ctx->global->clear_value (decl);
10374 19557717 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10375 : lval,
10376 : non_constant_p, overflow_p,
10377 : jump_target);
10378 28380762 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10379 8823046 : destroy_value_checked (ctx, decl, non_constant_p);
10380 : break;
10381 :
10382 5007340 : case PREINCREMENT_EXPR:
10383 5007340 : case POSTINCREMENT_EXPR:
10384 5007340 : case PREDECREMENT_EXPR:
10385 5007340 : case POSTDECREMENT_EXPR:
10386 5007340 : return cxx_eval_increment_expression (ctx, t,
10387 : lval, non_constant_p, overflow_p,
10388 5007340 : jump_target);
10389 :
10390 1153 : case THROW_EXPR:
10391 1153 : if (cxx_dialect >= cxx26)
10392 826 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10393 : non_constant_p, overflow_p,
10394 826 : jump_target);
10395 : /* FALLTHROUGH */
10396 335 : case LAMBDA_EXPR:
10397 335 : case NEW_EXPR:
10398 335 : case VEC_NEW_EXPR:
10399 335 : case DELETE_EXPR:
10400 335 : case VEC_DELETE_EXPR:
10401 335 : case MODOP_EXPR:
10402 : /* GCC internal stuff. */
10403 335 : case VA_ARG_EXPR:
10404 335 : case BASELINK:
10405 335 : case OFFSET_REF:
10406 335 : if (!ctx->quiet)
10407 86 : error_at (loc, "expression %qE is not a constant expression", t);
10408 335 : *non_constant_p = true;
10409 335 : break;
10410 :
10411 224307 : case OBJ_TYPE_REF:
10412 : /* Virtual function lookup. We don't need to do anything fancy. */
10413 224307 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10414 : lval, non_constant_p, overflow_p,
10415 224307 : jump_target);
10416 :
10417 16116 : case PLACEHOLDER_EXPR:
10418 : /* Use of the value or address of the current object. */
10419 16116 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10420 : {
10421 15885 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10422 : return ctor;
10423 : else
10424 15585 : return cxx_eval_constant_expression (ctx, ctor, lval,
10425 : non_constant_p, overflow_p,
10426 15585 : jump_target);
10427 : }
10428 : /* A placeholder without a referent. We can get here when
10429 : checking whether NSDMIs are noexcept, or in massage_init_elt;
10430 : just say it's non-constant for now. */
10431 231 : gcc_assert (ctx->quiet);
10432 231 : *non_constant_p = true;
10433 231 : break;
10434 :
10435 3319 : case EXIT_EXPR:
10436 3319 : {
10437 3319 : tree cond = TREE_OPERAND (t, 0);
10438 3319 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10439 : non_constant_p, overflow_p,
10440 : jump_target);
10441 3319 : if (*jump_target)
10442 : return NULL_TREE;
10443 3319 : VERIFY_CONSTANT (cond);
10444 3319 : if (integer_nonzerop (cond))
10445 1600 : *jump_target = t;
10446 : }
10447 : break;
10448 :
10449 39 : case GOTO_EXPR:
10450 39 : if (breaks (&TREE_OPERAND (t, 0))
10451 39 : || continues (&TREE_OPERAND (t, 0)))
10452 18 : *jump_target = TREE_OPERAND (t, 0);
10453 : else
10454 : {
10455 21 : if (!ctx->quiet)
10456 1 : error_at (loc, "%<goto%> is not a constant expression");
10457 21 : *non_constant_p = true;
10458 : }
10459 : break;
10460 :
10461 3332441 : case LOOP_EXPR:
10462 3332441 : case DO_STMT:
10463 3332441 : case WHILE_STMT:
10464 3332441 : case FOR_STMT:
10465 3332441 : cxx_eval_loop_expr (ctx, t,
10466 : non_constant_p, overflow_p, jump_target);
10467 3332441 : break;
10468 :
10469 34540 : case SWITCH_EXPR:
10470 34540 : case SWITCH_STMT:
10471 34540 : cxx_eval_switch_expr (ctx, t,
10472 : non_constant_p, overflow_p, jump_target);
10473 34540 : break;
10474 :
10475 120 : case REQUIRES_EXPR:
10476 : /* It's possible to get a requires-expression in a constant
10477 : expression. For example:
10478 :
10479 : template<typename T> concept bool C() {
10480 : return requires (T t) { t; };
10481 : }
10482 :
10483 : template<typename T> requires !C<T>() void f(T);
10484 :
10485 : Normalization leaves f with the associated constraint
10486 : '!requires (T t) { ... }' which is not transformed into
10487 : a constraint. */
10488 120 : if (!processing_template_decl)
10489 120 : return evaluate_requires_expr (t);
10490 : else
10491 0 : *non_constant_p = true;
10492 0 : return t;
10493 :
10494 16318 : case ANNOTATE_EXPR:
10495 16318 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10496 : lval,
10497 : non_constant_p, overflow_p,
10498 : jump_target);
10499 16318 : break;
10500 :
10501 13721 : case USING_STMT:
10502 13721 : r = void_node;
10503 13721 : break;
10504 :
10505 12 : case ASSERTION_STMT:
10506 12 : case PRECONDITION_STMT:
10507 12 : case POSTCONDITION_STMT:
10508 12 : {
10509 12 : r = void_node;
10510 : /* Only record the first fail, and do not go further is the semantic
10511 : is 'ignore'. */
10512 12 : if (*non_constant_p || ctx->global->contract_statement
10513 24 : || contract_ignored_p (t))
10514 : break;
10515 :
10516 12 : tree cond = CONTRACT_CONDITION (t);
10517 12 : if (!potential_rvalue_constant_expression (cond))
10518 : {
10519 4 : ctx->global->contract_statement = t;
10520 4 : ctx->global->contract_condition_non_const = true;
10521 4 : break;
10522 : }
10523 :
10524 : /* We need to evaluate and stash the result of this here, since whether
10525 : it needs to be reported (and how) depends on whether the containing
10526 : expression is otherwise const. */
10527 8 : bool ctrct_non_const_p = false;
10528 8 : bool ctrct_overflow_p = false;
10529 8 : tree jmp_target = NULL_TREE;
10530 8 : constexpr_ctx new_ctx = *ctx;
10531 8 : new_ctx.quiet = true;
10532 : /* Avoid modification of existing values. */
10533 8 : modifiable_tracker ms (new_ctx.global);
10534 8 : tree eval =
10535 8 : cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
10536 : &ctrct_non_const_p,
10537 : &ctrct_overflow_p, &jmp_target);
10538 : /* Not a constant. */
10539 8 : if (ctrct_non_const_p)
10540 : {
10541 0 : ctx->global->contract_statement = t;
10542 0 : ctx->global->contract_condition_non_const = true;
10543 0 : break;
10544 : }
10545 : /* Constant, but check failed. */
10546 8 : if (integer_zerop (eval))
10547 8 : ctx->global->contract_statement = t;
10548 8 : }
10549 8 : break;
10550 :
10551 2423878 : case TEMPLATE_ID_EXPR:
10552 2423878 : {
10553 : /* We can evaluate template-id that refers to a concept only if
10554 : the template arguments are non-dependent. */
10555 2423878 : gcc_assert (concept_check_p (t));
10556 :
10557 2423878 : if (!value_dependent_expression_p (t)
10558 2423878 : && !uid_sensitive_constexpr_evaluation_p ())
10559 2423439 : r = evaluate_concept_check (t);
10560 : else
10561 439 : *non_constant_p = true;
10562 :
10563 : break;
10564 : }
10565 :
10566 51 : case ASM_EXPR:
10567 51 : if (!ctx->quiet)
10568 29 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10569 51 : *non_constant_p = true;
10570 51 : return t;
10571 :
10572 1238 : case BIT_CAST_EXPR:
10573 1238 : if (lval)
10574 : {
10575 0 : if (!ctx->quiet)
10576 0 : error_at (EXPR_LOCATION (t),
10577 : "address of a call to %qs is not a constant expression",
10578 : "__builtin_bit_cast");
10579 0 : *non_constant_p = true;
10580 0 : return t;
10581 : }
10582 1238 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10583 1238 : break;
10584 :
10585 0 : case OMP_PARALLEL:
10586 0 : case OMP_TASK:
10587 0 : case OMP_FOR:
10588 0 : case OMP_SIMD:
10589 0 : case OMP_DISTRIBUTE:
10590 0 : case OMP_TASKLOOP:
10591 0 : case OMP_LOOP:
10592 0 : case OMP_TEAMS:
10593 0 : case OMP_TARGET_DATA:
10594 0 : case OMP_TARGET:
10595 0 : case OMP_SECTIONS:
10596 0 : case OMP_ORDERED:
10597 0 : case OMP_CRITICAL:
10598 0 : case OMP_SINGLE:
10599 0 : case OMP_SCAN:
10600 0 : case OMP_SCOPE:
10601 0 : case OMP_SECTION:
10602 0 : case OMP_STRUCTURED_BLOCK:
10603 0 : case OMP_MASTER:
10604 0 : case OMP_MASKED:
10605 0 : case OMP_TASKGROUP:
10606 0 : case OMP_TARGET_UPDATE:
10607 0 : case OMP_TARGET_ENTER_DATA:
10608 0 : case OMP_TARGET_EXIT_DATA:
10609 0 : case OMP_ATOMIC:
10610 0 : case OMP_ATOMIC_READ:
10611 0 : case OMP_ATOMIC_CAPTURE_OLD:
10612 0 : case OMP_ATOMIC_CAPTURE_NEW:
10613 0 : case OMP_DEPOBJ:
10614 0 : case OACC_PARALLEL:
10615 0 : case OACC_KERNELS:
10616 0 : case OACC_SERIAL:
10617 0 : case OACC_DATA:
10618 0 : case OACC_HOST_DATA:
10619 0 : case OACC_LOOP:
10620 0 : case OACC_CACHE:
10621 0 : case OACC_DECLARE:
10622 0 : case OACC_ENTER_DATA:
10623 0 : case OACC_EXIT_DATA:
10624 0 : case OACC_UPDATE:
10625 0 : if (!ctx->quiet)
10626 0 : error_at (EXPR_LOCATION (t),
10627 : "statement is not a constant expression");
10628 0 : *non_constant_p = true;
10629 0 : break;
10630 :
10631 0 : default:
10632 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10633 : {
10634 : /* This function doesn't know how to deal with pre-genericize
10635 : statements; this can only happen with statement-expressions,
10636 : so for now just fail. */
10637 0 : if (!ctx->quiet)
10638 0 : error_at (EXPR_LOCATION (t),
10639 : "statement is not a constant expression");
10640 : }
10641 0 : else if (flag_checking)
10642 0 : internal_error ("unexpected expression %qE of kind %s", t,
10643 : get_tree_code_name (TREE_CODE (t)));
10644 0 : *non_constant_p = true;
10645 0 : break;
10646 : }
10647 :
10648 1287927290 : if (r == error_mark_node)
10649 13830633 : *non_constant_p = true;
10650 :
10651 76168167 : if (r == void_node && lval != vc_discard && !*jump_target
10652 1287930870 : && !VOID_TYPE_P (TREE_TYPE (t)))
10653 : {
10654 : /* For diagnostic quality we should have handled this sooner, where we
10655 : can be more specific about the out-of-lifetime object. But here we
10656 : can still be correct. */
10657 1 : gcc_checking_assert (false);
10658 : if (!ctx->quiet)
10659 : error_at (EXPR_LOCATION (t),
10660 : "%qE accesses an object outside its lifetime", t);
10661 : *non_constant_p = true;
10662 : }
10663 :
10664 1287927289 : if (*non_constant_p)
10665 365785780 : return t;
10666 : else
10667 : return r;
10668 : }
10669 :
10670 : /* P0859: A function is needed for constant evaluation if it is a constexpr
10671 : function that is named by an expression ([basic.def.odr]) that is
10672 : potentially constant evaluated.
10673 :
10674 : So we need to instantiate any constexpr functions mentioned by the
10675 : expression even if the definition isn't needed for evaluating the
10676 : expression. */
10677 :
10678 : static tree
10679 549767645 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10680 : {
10681 549767645 : if (TREE_CODE (*tp) == FUNCTION_DECL
10682 11181933 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10683 11058677 : && !DECL_INITIAL (*tp)
10684 2884332 : && !trivial_fn_p (*tp)
10685 2884312 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10686 549767645 : && !uid_sensitive_constexpr_evaluation_p ())
10687 : {
10688 2870157 : ++function_depth;
10689 2870157 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10690 2870149 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10691 : else
10692 8 : synthesize_method (*tp);
10693 2870157 : --function_depth;
10694 : }
10695 546897488 : else if (TREE_CODE (*tp) == CALL_EXPR
10696 535842759 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10697 : {
10698 11244404 : if (EXPR_HAS_LOCATION (*tp))
10699 11241092 : input_location = EXPR_LOCATION (*tp);
10700 : }
10701 :
10702 549767645 : if (!EXPR_P (*tp))
10703 311048393 : *walk_subtrees = 0;
10704 :
10705 549767645 : return NULL_TREE;
10706 : }
10707 :
10708 : static void
10709 265658276 : instantiate_constexpr_fns (tree t)
10710 : {
10711 265658276 : location_t loc = input_location;
10712 265658276 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10713 265658276 : input_location = loc;
10714 265658276 : }
10715 :
10716 : /* Look for heap variables in the expression *TP. */
10717 :
10718 : static tree
10719 617633 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10720 : {
10721 617633 : if (VAR_P (*tp)
10722 617633 : && (DECL_NAME (*tp) == heap_uninit_identifier
10723 7185 : || DECL_NAME (*tp) == heap_identifier
10724 3880 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10725 1773 : || DECL_NAME (*tp) == heap_vec_identifier
10726 559 : || DECL_NAME (*tp) == heap_deleted_identifier))
10727 : return *tp;
10728 :
10729 419544 : if (TYPE_P (*tp))
10730 81 : *walk_subtrees = 0;
10731 : return NULL_TREE;
10732 : }
10733 :
10734 : /* Find immediate function decls in *TP if any. */
10735 :
10736 : static tree
10737 576328587 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
10738 : {
10739 580040294 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10740 : return *tp;
10741 576323767 : if (TREE_CODE (*tp) == PTRMEM_CST
10742 56133 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10743 576378997 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10744 : return PTRMEM_CST_MEMBER (*tp);
10745 576323662 : if (REFLECT_EXPR_P (*tp))
10746 49345 : *walk_subtrees = 0;
10747 : return NULL_TREE;
10748 : }
10749 :
10750 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10751 : expression. Return a version of T that has TREE_CONSTANT cleared. */
10752 :
10753 : static tree
10754 142219 : mark_non_constant (tree t)
10755 : {
10756 142219 : gcc_checking_assert (TREE_CONSTANT (t));
10757 :
10758 : /* This isn't actually constant, so unset TREE_CONSTANT.
10759 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10760 : it to be set if it is invariant address, even when it is not
10761 : a valid C++ constant expression. Wrap it with a NOP_EXPR
10762 : instead. */
10763 142219 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10764 140582 : t = copy_node (t);
10765 1637 : else if (TREE_CODE (t) == CONSTRUCTOR)
10766 236 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10767 : else
10768 1401 : t = build_nop (TREE_TYPE (t), t);
10769 142219 : TREE_CONSTANT (t) = false;
10770 142219 : return t;
10771 : }
10772 :
10773 : /* If we have a successful constant evaluation, now check whether there is
10774 : a failed or non-constant contract that would invalidate this. */
10775 :
10776 : static bool
10777 516379682 : check_for_failed_contracts (constexpr_ctx *ctx)
10778 : {
10779 516379682 : constexpr_global_ctx *const global_ctx = ctx->global;
10780 516379682 : if (!flag_contracts || !global_ctx->contract_statement)
10781 : return false;
10782 :
10783 12 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
10784 12 : enum diagnostics::kind kind;
10785 12 : bool error = false;
10786 : /* [intro.compliance.general]/2.3.4. */
10787 : /* [basic.contract.eval]/8. */
10788 12 : if (ctx->manifestly_const_eval != mce_true)
10789 : {
10790 : /* When !MCE, silently return not constant. */
10791 : return true;
10792 : }
10793 12 : else if (contract_terminating_p (global_ctx->contract_statement))
10794 : {
10795 : kind = diagnostics::kind::error;
10796 : error = true;
10797 : }
10798 : else
10799 4 : kind = diagnostics::kind::warning;
10800 :
10801 : /* [basic.contract.eval]/7.3 */
10802 12 : if (global_ctx->contract_condition_non_const)
10803 : {
10804 4 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
10805 4 : return error;
10806 : }
10807 :
10808 : /* Otherwise, the evaluation was const, but determined to be false. */
10809 8 : emit_diagnostic (kind, loc, 0,
10810 : "contract predicate is false in constant expression");
10811 8 : return error;
10812 : }
10813 :
10814 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10815 : STRICT has the same sense as for constant_value_1: true if we only allow
10816 : conforming C++ constant expressions, or false if we want a constant value
10817 : even if it doesn't conform.
10818 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10819 : per P0595 even when ALLOW_NON_CONSTANT is true.
10820 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10821 : OBJECT must be non-NULL in that case. */
10822 :
10823 : static tree
10824 525450464 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10825 : bool strict = true,
10826 : mce_value manifestly_const_eval = mce_unknown,
10827 : bool constexpr_dtor = false,
10828 : tree object = NULL_TREE)
10829 : {
10830 525450464 : auto_timevar time (TV_CONSTEXPR);
10831 :
10832 525450464 : bool non_constant_p = false;
10833 525450464 : bool overflow_p = false;
10834 :
10835 525450464 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10836 : {
10837 0 : gcc_checking_assert (allow_non_constant);
10838 : return t;
10839 : }
10840 :
10841 525450464 : constexpr_global_ctx global_ctx;
10842 525450464 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10843 : allow_non_constant, strict,
10844 525450464 : !allow_non_constant ? mce_true : manifestly_const_eval };
10845 :
10846 : /* Turn off -frounding-math for manifestly constant evaluation. */
10847 525450464 : warning_sentinel rm (flag_rounding_math,
10848 525450464 : ctx.manifestly_const_eval == mce_true);
10849 525450464 : tree type = (object
10850 525450464 : ? cv_unqualified (TREE_TYPE (object))
10851 470878395 : : initialized_type (t));
10852 525450464 : tree r = t;
10853 525450464 : bool is_consteval = false;
10854 525450464 : if (VOID_TYPE_P (type))
10855 : {
10856 9064306 : if (!constexpr_dtor)
10857 : {
10858 9064306 : if (cxx_dialect < cxx20)
10859 : return t;
10860 : /* We could have a COMPOUND_EXPR here coming from
10861 : keep_unused_object_arg. */
10862 9053350 : tree x = extract_call_expr (t);
10863 9053350 : if (x == NULL_TREE || x == error_mark_node)
10864 : return t;
10865 : /* Calls to immediate functions returning void need to be
10866 : evaluated. */
10867 9053310 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10868 18106620 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10869 : return t;
10870 : else
10871 576 : is_consteval = true;
10872 576 : tree lam;
10873 576 : if (manifestly_const_eval == mce_true
10874 834 : && LAMBDA_FUNCTION_P (fndecl)
10875 252 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
10876 828 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
10877 242 : global_ctx.consteval_block = fndecl;
10878 : }
10879 : }
10880 516386158 : else if (cxx_dialect >= cxx20
10881 508197434 : && (TREE_CODE (t) == CALL_EXPR
10882 437477373 : || TREE_CODE (t) == AGGR_INIT_EXPR
10883 430719196 : || TREE_CODE (t) == TARGET_EXPR))
10884 : {
10885 82221543 : tree x = t;
10886 82221543 : if (TREE_CODE (x) == TARGET_EXPR)
10887 4743305 : x = TARGET_EXPR_INITIAL (x);
10888 82221543 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10889 162143802 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10890 : is_consteval = true;
10891 : /* Don't try to evaluate a std::vector constructor taking an integer, it
10892 : will fail in the 'if (heap_var)' block below after doing all the work
10893 : (c++/113835). This will need adjustment if P3554 is accepted. Note
10894 : that evaluation of e.g. the vector default constructor can succeed, so
10895 : we don't shortcut all vector constructors. */
10896 159844518 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10897 12564476 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10898 82243383 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10899 : return t;
10900 : }
10901 516385323 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10902 : {
10903 : /* In C++14 an NSDMI can participate in aggregate initialization,
10904 : and can refer to the address of the object being initialized, so
10905 : we need to pass in the relevant VAR_DECL if we want to do the
10906 : evaluation in a single pass. The evaluation will dynamically
10907 : update ctx.values for the VAR_DECL. We use the same strategy
10908 : for C++11 constexpr constructors that refer to the object being
10909 : initialized. */
10910 50913158 : if (constexpr_dtor)
10911 : {
10912 161 : gcc_assert (object && VAR_P (object));
10913 161 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10914 161 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10915 161 : if (error_operand_p (DECL_INITIAL (object)))
10916 : return t;
10917 149 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10918 149 : TREE_READONLY (ctx.ctor) = false;
10919 : /* Temporarily force decl_really_constant_value to return false
10920 : for it, we want to use ctx.ctor for the current value instead. */
10921 149 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10922 : }
10923 : else
10924 : {
10925 50912997 : ctx.ctor = build_constructor (type, NULL);
10926 50912997 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10927 : }
10928 50913146 : if (!object)
10929 : {
10930 19246627 : if (TREE_CODE (t) == CALL_EXPR)
10931 : {
10932 : /* If T is calling a constructor to initialize an object, reframe
10933 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10934 : from outside the constant evaluation, which will fail even if
10935 : the value is actually constant (is_constant_evaluated3.C). */
10936 8881534 : tree fn = cp_get_callee_fndecl_nofold (t);
10937 17762972 : if (fn && DECL_CONSTRUCTOR_P (fn))
10938 : {
10939 3763450 : object = CALL_EXPR_ARG (t, 0);
10940 3763450 : object = build_fold_indirect_ref (object);
10941 3763450 : r = build_aggr_init_expr (type, r);
10942 : }
10943 : }
10944 10365093 : else if (TREE_CODE (t) == TARGET_EXPR)
10945 4654394 : object = TARGET_EXPR_SLOT (t);
10946 5710699 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10947 327852 : object = AGGR_INIT_EXPR_SLOT (t);
10948 : }
10949 50913146 : ctx.object = object;
10950 50913146 : if (object)
10951 40412215 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10952 : (type, TREE_TYPE (object)));
10953 40412215 : if (object && DECL_P (object))
10954 36311574 : global_ctx.put_value (object, ctx.ctor);
10955 50913146 : if (TREE_CODE (r) == TARGET_EXPR)
10956 : /* Avoid creating another CONSTRUCTOR when we expand the
10957 : TARGET_EXPR. */
10958 4779185 : r = TARGET_EXPR_INITIAL (r);
10959 : }
10960 :
10961 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
10962 : constexpr evaluation to avoid unnecessary template instantiation, and is
10963 : always done with mce_unknown. But due to gaps in the restriction logic
10964 : we may still end up taking an evaluation path that in turn requires
10965 : manifestly constant evaluation, and such evaluation must not be
10966 : restricted since it likely has semantic consequences.
10967 : TODO: Remove/replace the mechanism in GCC 17. */
10968 516385311 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
10969 516385311 : if (ctx.manifestly_const_eval == mce_true)
10970 265658276 : uid_sensitive_constexpr_evaluation_value = false;
10971 :
10972 516385311 : auto_vec<tree, 16> cleanups;
10973 516385311 : global_ctx.cleanups = &cleanups;
10974 :
10975 516385311 : if (manifestly_const_eval == mce_true)
10976 265658276 : instantiate_constexpr_fns (r);
10977 516385311 : tree jmp_target = NULL_TREE;
10978 516385311 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
10979 : &non_constant_p, &overflow_p,
10980 : &jmp_target);
10981 516383379 : if (throws (&jmp_target) && !non_constant_p)
10982 : {
10983 766 : if (!ctx.quiet)
10984 214 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10985 766 : non_constant_p = true;
10986 766 : jmp_target = NULL_TREE;
10987 766 : r = t;
10988 : }
10989 516381847 : else if (!non_constant_p && jmp_target)
10990 : {
10991 24 : non_constant_p = true;
10992 24 : if (!ctx.quiet)
10993 : {
10994 3 : if (breaks (&jmp_target))
10995 3 : error ("%<break%> outside of a loop or %<switch%>");
10996 0 : else if (continues (&jmp_target))
10997 0 : error ("%<continue%> outside of a loop");
10998 0 : else if (returns (&jmp_target))
10999 0 : error ("%<return%> in a statement expression");
11000 : else
11001 0 : gcc_unreachable ();
11002 : }
11003 24 : r = t;
11004 : }
11005 :
11006 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
11007 : of statements, and the result ought to be stored in ctx.ctor. */
11008 516382613 : if (r == void_node && !constexpr_dtor && ctx.ctor)
11009 0 : r = ctx.ctor;
11010 :
11011 516382613 : unsigned int i;
11012 516382613 : tree cleanup;
11013 516382613 : jmp_target = NULL_TREE;
11014 : /* Evaluate the cleanups. */
11015 1032894910 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
11016 129684 : if (cleanup == NULL_TREE)
11017 : /* NULL_TREE cleanup is a marker that before it is
11018 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
11019 23 : --i;
11020 : else
11021 129661 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
11022 : &non_constant_p, &overflow_p,
11023 : &jmp_target);
11024 516382613 : if (throws (&jmp_target) && !non_constant_p)
11025 : {
11026 0 : if (!ctx.quiet)
11027 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11028 0 : non_constant_p = true;
11029 0 : r = t;
11030 : }
11031 :
11032 : /* Mutable logic is a bit tricky: we want to allow initialization of
11033 : constexpr variables with mutable members, but we can't copy those
11034 : members to another constexpr variable. */
11035 516382613 : if (!non_constant_p
11036 386102807 : && TREE_CODE (r) == CONSTRUCTOR
11037 534459575 : && CONSTRUCTOR_MUTABLE_POISON (r))
11038 : {
11039 92 : if (!allow_non_constant)
11040 6 : error ("%qE is not a constant expression because it refers to "
11041 : "mutable subobjects of %qT", t, type);
11042 92 : non_constant_p = true;
11043 : }
11044 :
11045 386102715 : if (!non_constant_p && cxx_dialect >= cxx20
11046 902485328 : && !global_ctx.heap_vars.is_empty ())
11047 : {
11048 203444 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11049 : NULL);
11050 203444 : unsigned int i;
11051 203444 : if (heap_var)
11052 : {
11053 198089 : if (!allow_non_constant && !non_constant_p)
11054 : {
11055 12 : if (DECL_LANG_SPECIFIC (heap_var))
11056 2 : error ("%qE is not a constant expression because it refers to "
11057 : "exception object allocated with "
11058 : "%<__cxa_allocate_exception%>", t);
11059 : else
11060 10 : error ("%qE is not a constant expression because it refers to "
11061 : "a result of %<operator new%>", t);
11062 12 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11063 : }
11064 198089 : r = t;
11065 198089 : non_constant_p = true;
11066 : }
11067 443230 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11068 : {
11069 239786 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11070 : {
11071 198175 : if (!allow_non_constant && !non_constant_p)
11072 : {
11073 16 : error ("%qE is not a constant expression because allocated "
11074 : "storage has not been deallocated", t);
11075 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11076 : }
11077 198175 : r = t;
11078 198175 : non_constant_p = true;
11079 : }
11080 : }
11081 : }
11082 :
11083 : /* Check that immediate invocation does not return an expression referencing
11084 : any immediate function decls. */
11085 516382613 : if (!non_constant_p && cxx_dialect >= cxx20)
11086 760519162 : if (tree immediate_fndecl
11087 380259581 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11088 : NULL))
11089 : {
11090 4925 : if (!allow_non_constant && !non_constant_p)
11091 : {
11092 73 : if (is_consteval)
11093 36 : error_at (cp_expr_loc_or_input_loc (t),
11094 : "immediate evaluation returns address of immediate "
11095 : "function %qD", immediate_fndecl);
11096 : else
11097 56 : error_at (cp_expr_loc_or_input_loc (t),
11098 : "constant evaluation returns address of immediate "
11099 : "function %qD", immediate_fndecl);
11100 : }
11101 4925 : r = t;
11102 4925 : non_constant_p = true;
11103 : }
11104 :
11105 : /* Detect consteval-only smuggling: turning a consteval-only object
11106 : into one that is not. For instance, in
11107 : struct B { };
11108 : struct D : B { info r; };
11109 : constexpr D d{^^::};
11110 : constexpr const B &b = d; // #1
11111 : #1 is wrong because D is a consteval-only type but B is not. */
11112 516382613 : if (flag_reflection
11113 4875581 : && !non_constant_p
11114 3943210 : && object
11115 455089 : && POINTER_TYPE_P (TREE_TYPE (object))
11116 787 : && !consteval_only_p (object)
11117 516383343 : && check_out_of_consteval_use (r, /*complain=*/false))
11118 : {
11119 15 : if (!allow_non_constant)
11120 : {
11121 5 : if (TYPE_REF_P (TREE_TYPE (object)))
11122 6 : error_at (cp_expr_loc_or_input_loc (t),
11123 : "reference into an object of consteval-only type is "
11124 : "not a constant expression unless it also has "
11125 : "consteval-only type");
11126 : else
11127 2 : error_at (cp_expr_loc_or_input_loc (t),
11128 : "pointer into an object of consteval-only type is "
11129 : "not a constant expression unless it also has "
11130 : "consteval-only type");
11131 : }
11132 15 : r = t;
11133 15 : non_constant_p = true;
11134 : }
11135 :
11136 516382613 : if (!non_constant_p && !constexpr_dtor)
11137 385899460 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11138 :
11139 : /* After verify_constant because reduced_constant_expression_p can unset
11140 : CONSTRUCTOR_NO_CLEARING. */
11141 516382613 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11142 : {
11143 107444 : if (!allow_non_constant)
11144 41 : error ("%qE is not a constant expression because it refers to "
11145 : "an incompletely initialized variable", t);
11146 107444 : TREE_CONSTANT (r) = false;
11147 107444 : non_constant_p = true;
11148 : }
11149 :
11150 516382613 : if (non_constant_p)
11151 : /* If we saw something bad, go back to our argument. The wrapping below is
11152 : only for the cases of TREE_CONSTANT argument or overflow. */
11153 132614999 : r = t;
11154 :
11155 516382613 : if (!non_constant_p && overflow_p)
11156 215 : non_constant_p = true;
11157 :
11158 : /* Unshare the result. */
11159 516382613 : bool should_unshare = true;
11160 516382613 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
11161 1357511 : && TARGET_EXPR_INITIAL (t) == r))
11162 : should_unshare = false;
11163 :
11164 516382613 : if (non_constant_p && !allow_non_constant)
11165 2931 : return error_mark_node;
11166 516379682 : else if (check_for_failed_contracts (&ctx))
11167 : {
11168 8 : if (manifestly_const_eval == mce_true)
11169 : /* If MCE, we gave a hard error and return error_mark_node. */
11170 8 : return error_mark_node;
11171 : else
11172 : {
11173 : /* Otherwise treat it as non-constant so the violation is still
11174 : detectable at run-time. */
11175 0 : gcc_checking_assert (allow_non_constant);
11176 : return t;
11177 : }
11178 : }
11179 516379674 : else if (non_constant_p && TREE_CONSTANT (r))
11180 137729 : r = mark_non_constant (r);
11181 516241945 : else if (non_constant_p)
11182 : return t;
11183 :
11184 383905120 : if (constexpr_dtor)
11185 : {
11186 131 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
11187 131 : return r;
11188 : }
11189 :
11190 : /* Check we are not trying to return the wrong type. */
11191 383904989 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
11192 : {
11193 : /* If so, this is not a constant expression. */
11194 151 : if (!allow_non_constant)
11195 2 : error ("%qE is not a constant expression because it initializes "
11196 2 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
11197 151 : return t;
11198 : }
11199 :
11200 383904838 : if (should_unshare)
11201 219444278 : r = unshare_expr (r);
11202 :
11203 383904838 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
11204 : {
11205 17143735 : r = adjust_temp_type (type, r);
11206 17143735 : if (TREE_CODE (t) == TARGET_EXPR
11207 17143735 : && TARGET_EXPR_INITIAL (t) == r)
11208 : return t;
11209 16241665 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
11210 2330108 : || TREE_CODE (t) == AGGR_INIT_EXPR)
11211 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
11212 509906 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
11213 489 : r = get_target_expr (r);
11214 : else
11215 : {
11216 509417 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
11217 509417 : TREE_CONSTANT (r) = true;
11218 : }
11219 : }
11220 :
11221 383002768 : if (TREE_CODE (t) == TARGET_EXPR
11222 455441 : && TREE_CODE (r) == TARGET_EXPR)
11223 : {
11224 : /* Preserve this flag for potential_constant_expression, and the others
11225 : for good measure. */
11226 455340 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
11227 455340 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
11228 455340 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
11229 455340 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
11230 : }
11231 :
11232 : /* Remember the original location if that wouldn't need a wrapper. */
11233 383002768 : if (location_t loc = EXPR_LOCATION (t))
11234 177375355 : protected_set_expr_location (r, loc);
11235 :
11236 383002768 : return r;
11237 525447766 : }
11238 :
11239 : /* If T represents a constant expression returns its reduced value.
11240 : Otherwise return error_mark_node. */
11241 :
11242 : tree
11243 140905858 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
11244 : tsubst_flags_t complain /* = tf_error */)
11245 : {
11246 140905858 : bool sfinae = !(complain & tf_error);
11247 140905858 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
11248 140905858 : if (sfinae && !TREE_CONSTANT (r))
11249 1654 : r = error_mark_node;
11250 140905858 : return r;
11251 : }
11252 :
11253 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
11254 : of constexpr variables. The actual initializer of DECL is not modified. */
11255 :
11256 : void
11257 161 : cxx_constant_dtor (tree t, tree decl)
11258 : {
11259 161 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
11260 161 : }
11261 :
11262 : /* Helper routine for fold_simple function. Either return simplified
11263 : expression T, otherwise NULL_TREE.
11264 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
11265 : even if we are within template-declaration. So be careful on call, as in
11266 : such case types can be undefined. */
11267 :
11268 : static tree
11269 156413766 : fold_simple_1 (tree t)
11270 : {
11271 156413766 : tree op1;
11272 156413766 : enum tree_code code = TREE_CODE (t);
11273 :
11274 156413766 : switch (code)
11275 : {
11276 : case INTEGER_CST:
11277 : case REAL_CST:
11278 : case VECTOR_CST:
11279 : case FIXED_CST:
11280 : case COMPLEX_CST:
11281 : return t;
11282 :
11283 1355400 : case SIZEOF_EXPR:
11284 1355400 : return fold_sizeof_expr (t);
11285 :
11286 35618585 : case ABS_EXPR:
11287 35618585 : case ABSU_EXPR:
11288 35618585 : case CONJ_EXPR:
11289 35618585 : case REALPART_EXPR:
11290 35618585 : case IMAGPART_EXPR:
11291 35618585 : case NEGATE_EXPR:
11292 35618585 : case BIT_NOT_EXPR:
11293 35618585 : case TRUTH_NOT_EXPR:
11294 35618585 : case VIEW_CONVERT_EXPR:
11295 35618585 : CASE_CONVERT:
11296 35618585 : case FLOAT_EXPR:
11297 35618585 : case FIX_TRUNC_EXPR:
11298 35618585 : case FIXED_CONVERT_EXPR:
11299 35618585 : case ADDR_SPACE_CONVERT_EXPR:
11300 :
11301 35618585 : op1 = TREE_OPERAND (t, 0);
11302 :
11303 35618585 : t = const_unop (code, TREE_TYPE (t), op1);
11304 35618585 : if (!t)
11305 : return NULL_TREE;
11306 :
11307 3970806 : if (CONVERT_EXPR_CODE_P (code)
11308 3970806 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
11309 0 : TREE_OVERFLOW (t) = false;
11310 : return t;
11311 :
11312 : default:
11313 : return NULL_TREE;
11314 : }
11315 : }
11316 :
11317 : /* If T is a simple constant expression, returns its simplified value.
11318 : Otherwise returns T. In contrast to maybe_constant_value we
11319 : simplify only few operations on constant-expressions, and we don't
11320 : try to simplify constexpressions. */
11321 :
11322 : tree
11323 157086641 : fold_simple (tree t)
11324 : {
11325 157086641 : if (processing_template_decl)
11326 : return t;
11327 :
11328 156413766 : tree r = fold_simple_1 (t);
11329 156413766 : if (r)
11330 : return r;
11331 :
11332 : return t;
11333 : }
11334 :
11335 : /* Try folding the expression T to a simple constant.
11336 : Returns that constant, otherwise returns T. */
11337 :
11338 : tree
11339 1555098 : fold_to_constant (tree t)
11340 : {
11341 1555098 : if (processing_template_decl)
11342 : return t;
11343 :
11344 1461393 : tree r = fold (t);
11345 1461393 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
11346 : return r;
11347 : else
11348 : return t;
11349 : }
11350 :
11351 : /* If T is a constant expression, returns its reduced value.
11352 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
11353 : Otherwise, returns a version of T without TREE_CONSTANT.
11354 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
11355 : as per P0595. */
11356 :
11357 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
11358 :
11359 : tree
11360 808887906 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
11361 : mce_value manifestly_const_eval /* = mce_unknown */)
11362 : {
11363 808887906 : tree orig_t = t;
11364 808887906 : tree r;
11365 :
11366 808887906 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
11367 : {
11368 : /* Look up each operand in the cv_cache first to see if we've already
11369 : reduced it, and reuse that result to avoid quadratic behavior if
11370 : we're called when building up a large expression. */
11371 233090371 : int n = cp_tree_operand_length (t);
11372 233090371 : tree *ops = XALLOCAVEC (tree, n);
11373 233090371 : bool rebuild = false;
11374 654529769 : for (int i = 0; i < n; ++i)
11375 : {
11376 421439398 : ops[i] = TREE_OPERAND (t, i);
11377 1263734065 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
11378 28960388 : if (*cached != ops[i])
11379 : {
11380 9004598 : ops[i] = *cached;
11381 9004598 : rebuild = true;
11382 : }
11383 : }
11384 233090371 : if (rebuild)
11385 : {
11386 7587685 : t = copy_node (t);
11387 24648886 : for (int i = 0; i < n; ++i)
11388 17061201 : TREE_OPERAND (t, i) = ops[i];
11389 : }
11390 : }
11391 :
11392 808887906 : if (!is_nondependent_constant_expression (t))
11393 : {
11394 0 : if (TREE_OVERFLOW_P (t)
11395 121272567 : || (!processing_template_decl && TREE_CONSTANT (t)))
11396 4490 : t = mark_non_constant (t);
11397 121272567 : return t;
11398 : }
11399 687615339 : else if (CONSTANT_CLASS_P (t))
11400 : /* No caching or evaluation needed. */
11401 : return t;
11402 :
11403 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11404 : but at least try folding it to a simple constant. */
11405 314690008 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11406 492255 : return fold_to_constant (t);
11407 :
11408 298792372 : if (manifestly_const_eval != mce_unknown)
11409 : /* TODO: Extend the cache to be mce_value aware. And if we have a
11410 : previously cached mce_unknown result that's TREE_CONSTANT, it means
11411 : the reduced value is independent of mce_value and so we should
11412 : be able to reuse it in the mce_true/false case. */
11413 164227324 : return cxx_eval_outermost_constant_expr (t, true, true,
11414 164224626 : manifestly_const_eval, false, decl);
11415 :
11416 149970429 : if (cv_cache == NULL)
11417 140894 : cv_cache = hash_map<tree, tree>::create_ggc (101);
11418 149970429 : if (tree *cached = cv_cache->get (t))
11419 : {
11420 12733180 : r = *cached;
11421 12733180 : if (r != t)
11422 : {
11423 : /* Clear processing_template_decl for sake of break_out_target_exprs;
11424 : entries in the cv_cache are non-templated. */
11425 4304971 : processing_template_decl_sentinel ptds;
11426 :
11427 4304971 : r = break_out_target_exprs (r, /*clear_loc*/true);
11428 4304971 : protected_set_expr_location (r, EXPR_LOCATION (t));
11429 4304971 : }
11430 12733180 : return r;
11431 : }
11432 :
11433 137237249 : uid_sensitive_constexpr_evaluation_checker c;
11434 137237249 : r = cxx_eval_outermost_constant_expr (t, true, true,
11435 : manifestly_const_eval, false, decl);
11436 137237249 : gcc_checking_assert (r == t
11437 : || CONVERT_EXPR_P (t)
11438 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11439 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11440 : || !cp_tree_equal (r, t));
11441 137237249 : if (!c.evaluation_restricted_p ())
11442 136984396 : cv_cache->put (orig_t, r);
11443 : return r;
11444 : }
11445 :
11446 : /* Dispose of the whole CV_CACHE. */
11447 :
11448 : static void
11449 38559068 : clear_cv_cache (void)
11450 : {
11451 38559068 : if (cv_cache != NULL)
11452 38318357 : cv_cache->empty ();
11453 38559068 : }
11454 :
11455 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
11456 :
11457 : void
11458 38559068 : clear_cv_and_fold_caches ()
11459 : {
11460 38559068 : clear_cv_cache ();
11461 38559068 : clear_fold_cache ();
11462 38559068 : }
11463 :
11464 : /* Internal function handling expressions in templates for
11465 : fold_non_dependent_expr and fold_non_dependent_init.
11466 :
11467 : If we're in a template, but T isn't value dependent, simplify
11468 : it. We're supposed to treat:
11469 :
11470 : template <typename T> void f(T[1 + 1]);
11471 : template <typename T> void f(T[2]);
11472 :
11473 : as two declarations of the same function, for example. */
11474 :
11475 : static tree
11476 29536025 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11477 : bool manifestly_const_eval,
11478 : tree object)
11479 : {
11480 29536025 : gcc_assert (processing_template_decl);
11481 :
11482 29536025 : if (is_nondependent_constant_expression (t))
11483 : {
11484 16263595 : processing_template_decl_sentinel s;
11485 16263595 : t = instantiate_non_dependent_expr_internal (t, complain);
11486 :
11487 16263595 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11488 : {
11489 0 : if (TREE_OVERFLOW_P (t))
11490 : {
11491 0 : t = build_nop (TREE_TYPE (t), t);
11492 0 : TREE_CONSTANT (t) = false;
11493 : }
11494 0 : return t;
11495 : }
11496 16263595 : else if (CONSTANT_CLASS_P (t))
11497 : /* No evaluation needed. */
11498 : return t;
11499 :
11500 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11501 : but at least try folding it to a simple constant. */
11502 4233524 : if (cp_unevaluated_operand && !manifestly_const_eval)
11503 89 : return fold_to_constant (t);
11504 :
11505 4233435 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11506 : mce_value (manifestly_const_eval),
11507 : false, object);
11508 : /* cp_tree_equal looks through NOPs, so allow them. */
11509 4233435 : gcc_checking_assert (r == t
11510 : || CONVERT_EXPR_P (t)
11511 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11512 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11513 : || !cp_tree_equal (r, t));
11514 4233435 : return r;
11515 16263595 : }
11516 13272430 : else if (TREE_OVERFLOW_P (t))
11517 : {
11518 0 : t = build_nop (TREE_TYPE (t), t);
11519 0 : TREE_CONSTANT (t) = false;
11520 : }
11521 :
11522 : return t;
11523 : }
11524 :
11525 : /* Like maybe_constant_value but first fully instantiate the argument.
11526 :
11527 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11528 : followed by maybe_constant_value but is more efficient,
11529 : because it calls instantiation_dependent_expression_p and
11530 : potential_constant_expression at most once.
11531 : The manifestly_const_eval argument is passed to maybe_constant_value.
11532 :
11533 : Callers should generally pass their active complain, or if they are in a
11534 : non-template, diagnosing context, they can use the default of
11535 : tf_warning_or_error. Callers that might be within a template context, don't
11536 : have a complain parameter, and aren't going to remember the result for long
11537 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11538 : appropriately. */
11539 :
11540 : tree
11541 247329237 : fold_non_dependent_expr (tree t,
11542 : tsubst_flags_t complain /* = tf_warning_or_error */,
11543 : bool manifestly_const_eval /* = false */,
11544 : tree object /* = NULL_TREE */)
11545 : {
11546 247329237 : if (t == NULL_TREE)
11547 : return NULL_TREE;
11548 :
11549 246272844 : if (processing_template_decl)
11550 28500360 : return fold_non_dependent_expr_template (t, complain,
11551 28500360 : manifestly_const_eval, object);
11552 :
11553 217772484 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11554 : }
11555 :
11556 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11557 : return the original expression. */
11558 :
11559 : tree
11560 2237859 : maybe_fold_non_dependent_expr (tree expr,
11561 : tsubst_flags_t complain/*=tf_warning_or_error*/)
11562 : {
11563 2237859 : tree t = fold_non_dependent_expr (expr, complain);
11564 2237859 : if (t && TREE_CONSTANT (t))
11565 1056111 : return t;
11566 :
11567 : return expr;
11568 : }
11569 :
11570 : /* Like maybe_constant_init but first fully instantiate the argument. */
11571 :
11572 : tree
11573 59049623 : fold_non_dependent_init (tree t,
11574 : tsubst_flags_t complain /*=tf_warning_or_error*/,
11575 : bool manifestly_const_eval /*=false*/,
11576 : tree object /* = NULL_TREE */)
11577 : {
11578 59049623 : if (t == NULL_TREE)
11579 : return NULL_TREE;
11580 :
11581 59049623 : if (processing_template_decl)
11582 : {
11583 1035665 : t = fold_non_dependent_expr_template (t, complain,
11584 : manifestly_const_eval, object);
11585 : /* maybe_constant_init does this stripping, so do it here too. */
11586 1035665 : if (TREE_CODE (t) == TARGET_EXPR)
11587 : {
11588 934 : tree init = TARGET_EXPR_INITIAL (t);
11589 934 : if (TREE_CODE (init) == CONSTRUCTOR)
11590 1035665 : t = init;
11591 : }
11592 1035665 : return t;
11593 : }
11594 :
11595 58013958 : return maybe_constant_init (t, object, manifestly_const_eval);
11596 : }
11597 :
11598 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11599 : than wrapped in a TARGET_EXPR.
11600 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11601 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11602 : per P0595 even when ALLOW_NON_CONSTANT is true. */
11603 :
11604 : static tree
11605 134279870 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11606 : mce_value manifestly_const_eval)
11607 : {
11608 134279870 : if (!t)
11609 : return t;
11610 134279870 : if (TREE_CODE (t) == EXPR_STMT)
11611 24448 : t = TREE_OPERAND (t, 0);
11612 134279870 : if (TREE_CODE (t) == CONVERT_EXPR
11613 134279870 : && VOID_TYPE_P (TREE_TYPE (t)))
11614 30195 : t = TREE_OPERAND (t, 0);
11615 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11616 : DECL and losing that information would cause mischief later. */
11617 134279870 : if (TREE_CODE (t) == INIT_EXPR
11618 134279870 : && (!decl
11619 5763 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11620 5763 : TREE_TYPE (t))))
11621 25693 : t = TREE_OPERAND (t, 1);
11622 134279870 : if (TREE_CODE (t) == TARGET_EXPR)
11623 507766 : t = TARGET_EXPR_INITIAL (t);
11624 134279870 : if (!is_nondependent_static_init_expression (t))
11625 : /* Don't try to evaluate it. */;
11626 119901244 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11627 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11628 : else
11629 : {
11630 : /* [basic.start.static] allows constant-initialization of variables with
11631 : static or thread storage duration even if it isn't required, but we
11632 : shouldn't bend the rules the same way for automatic variables.
11633 :
11634 : But still enforce the requirements of constexpr/constinit.
11635 : [dcl.constinit] "If a variable declared with the constinit specifier
11636 : has dynamic initialization, the program is ill-formed, even if the
11637 : implementation would perform that initialization as a static
11638 : initialization." */
11639 32707700 : bool is_static = (decl && DECL_P (decl)
11640 89015405 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11641 3253459 : bool strict = (!is_static
11642 : || (decl && DECL_P (decl)
11643 3253459 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11644 945435 : || DECL_DECLARED_CONSTINIT_P (decl))));
11645 : if (is_static)
11646 : manifestly_const_eval = mce_true;
11647 :
11648 57004374 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11649 51449 : return fold_to_constant (t);
11650 :
11651 56952925 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11652 : manifestly_const_eval,
11653 : false, decl);
11654 : }
11655 134228421 : if (TREE_CODE (t) == TARGET_EXPR)
11656 : {
11657 54096 : tree init = TARGET_EXPR_INITIAL (t);
11658 54096 : if (TREE_CODE (init) == CONSTRUCTOR)
11659 134279870 : t = init;
11660 : }
11661 : return t;
11662 : }
11663 :
11664 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11665 :
11666 : tree
11667 93619364 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11668 : {
11669 93619364 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11670 : }
11671 :
11672 : tree
11673 40658955 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11674 : {
11675 40658955 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11676 : }
11677 :
11678 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11679 :
11680 : tree
11681 1551 : cxx_constant_init (tree t, tree decl)
11682 : {
11683 1551 : return maybe_constant_init_1 (t, decl, false, mce_true);
11684 : }
11685 :
11686 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11687 :
11688 : static bool
11689 205138266 : callee_might_throw (tree t)
11690 : {
11691 205138266 : if (cxx_dialect < cxx26 || !flag_exceptions)
11692 : return false;
11693 19702284 : tree callee = cp_get_callee (t);
11694 19702284 : if (callee == NULL_TREE)
11695 : return false;
11696 19667614 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11697 19667614 : return (!flag_enforce_eh_specs
11698 19667614 : || type_dependent_expression_p (callee)
11699 17781102 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11700 36184774 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11701 7009676 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11702 : }
11703 :
11704 : #if 0
11705 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11706 : /* Return true if the object referred to by REF has automatic or thread
11707 : local storage. */
11708 :
11709 : enum { ck_ok, ck_bad, ck_unknown };
11710 : static int
11711 : check_automatic_or_tls (tree ref)
11712 : {
11713 : machine_mode mode;
11714 : poly_int64 bitsize, bitpos;
11715 : tree offset;
11716 : int volatilep = 0, unsignedp = 0;
11717 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11718 : &mode, &unsignedp, &volatilep, false);
11719 : duration_kind dk;
11720 :
11721 : /* If there isn't a decl in the middle, we don't know the linkage here,
11722 : and this isn't a constant expression anyway. */
11723 : if (!DECL_P (decl))
11724 : return ck_unknown;
11725 : dk = decl_storage_duration (decl);
11726 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11727 : }
11728 : #endif
11729 :
11730 : /* Data structure for passing data from potential_constant_expression_1
11731 : to check_for_return_continue via cp_walk_tree. */
11732 : struct check_for_return_continue_data {
11733 : hash_set<tree> *pset;
11734 : tree continue_stmt;
11735 : tree break_stmt;
11736 : bool could_throw;
11737 : };
11738 :
11739 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11740 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11741 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11742 : For C++26 also note presence of possibly throwing calls. */
11743 : static tree
11744 46030124 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11745 : {
11746 46030124 : tree t = *tp, s, b;
11747 46030124 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11748 46030124 : switch (TREE_CODE (t))
11749 : {
11750 : case RETURN_EXPR:
11751 : return t;
11752 :
11753 28 : case CONTINUE_STMT:
11754 28 : if (d->continue_stmt == NULL_TREE)
11755 28 : d->continue_stmt = t;
11756 : break;
11757 :
11758 3004 : case BREAK_STMT:
11759 3004 : if (d->break_stmt == NULL_TREE)
11760 1289 : d->break_stmt = t;
11761 : break;
11762 :
11763 : #define RECUR(x) \
11764 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11765 : d->pset)) \
11766 : return r
11767 :
11768 : /* For loops, walk subtrees manually, so that continue stmts found
11769 : inside of the bodies of the loops are ignored. */
11770 39420 : case DO_STMT:
11771 39420 : *walk_subtrees = 0;
11772 39420 : RECUR (DO_COND (t));
11773 39420 : s = d->continue_stmt;
11774 39420 : b = d->break_stmt;
11775 39420 : RECUR (DO_BODY (t));
11776 39420 : d->continue_stmt = s;
11777 39420 : d->break_stmt = b;
11778 39420 : break;
11779 :
11780 117 : case WHILE_STMT:
11781 117 : *walk_subtrees = 0;
11782 117 : RECUR (WHILE_COND_PREP (t));
11783 117 : RECUR (WHILE_COND (t));
11784 117 : s = d->continue_stmt;
11785 117 : b = d->break_stmt;
11786 117 : RECUR (WHILE_BODY (t));
11787 105 : d->continue_stmt = s;
11788 105 : d->break_stmt = b;
11789 105 : break;
11790 :
11791 277 : case FOR_STMT:
11792 277 : *walk_subtrees = 0;
11793 277 : RECUR (FOR_INIT_STMT (t));
11794 277 : RECUR (FOR_COND_PREP (t));
11795 277 : RECUR (FOR_COND (t));
11796 277 : RECUR (FOR_EXPR (t));
11797 277 : s = d->continue_stmt;
11798 277 : b = d->break_stmt;
11799 277 : RECUR (FOR_BODY (t));
11800 247 : d->continue_stmt = s;
11801 247 : d->break_stmt = b;
11802 247 : break;
11803 :
11804 0 : case RANGE_FOR_STMT:
11805 0 : *walk_subtrees = 0;
11806 0 : RECUR (RANGE_FOR_EXPR (t));
11807 0 : s = d->continue_stmt;
11808 0 : b = d->break_stmt;
11809 0 : RECUR (RANGE_FOR_BODY (t));
11810 0 : d->continue_stmt = s;
11811 0 : d->break_stmt = b;
11812 0 : break;
11813 :
11814 207 : case SWITCH_STMT:
11815 207 : *walk_subtrees = 0;
11816 207 : RECUR (SWITCH_STMT_COND (t));
11817 207 : b = d->break_stmt;
11818 207 : RECUR (SWITCH_STMT_BODY (t));
11819 187 : d->break_stmt = b;
11820 187 : break;
11821 : #undef RECUR
11822 :
11823 : case STATEMENT_LIST:
11824 : case CONSTRUCTOR:
11825 : break;
11826 :
11827 2375255 : case AGGR_INIT_EXPR:
11828 2375255 : case CALL_EXPR:
11829 : /* In C++26 a function could throw. */
11830 2375255 : if (callee_might_throw (t))
11831 51874 : d->could_throw = true;
11832 : break;
11833 :
11834 42263186 : default:
11835 42263186 : if (!EXPR_P (t))
11836 13189948 : *walk_subtrees = 0;
11837 : break;
11838 : }
11839 :
11840 : return NULL_TREE;
11841 : }
11842 :
11843 : /* Return true if T denotes a potentially constant expression. Issue
11844 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11845 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11846 : consider the expression in the current context, independent of constexpr
11847 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11848 : and hard errors should not be reported by constexpr_error.
11849 :
11850 : C++0x [expr.const] used to say
11851 :
11852 : 6 An expression is a potential constant expression if it is
11853 : a constant expression where all occurrences of function
11854 : parameters are replaced by arbitrary constant expressions
11855 : of the appropriate type.
11856 :
11857 : 2 A conditional expression is a constant expression unless it
11858 : involves one of the following as a potentially evaluated
11859 : subexpression (3.2), but subexpressions of logical AND (5.14),
11860 : logical OR (5.15), and conditional (5.16) operations that are
11861 : not evaluated are not considered. */
11862 :
11863 : static bool
11864 4495466506 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11865 : bool fundef_p, tsubst_flags_t flags,
11866 : tree *jump_target)
11867 : {
11868 : #define RECUR(T,RV) \
11869 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11870 : jump_target)
11871 :
11872 4495466506 : enum { any = false, rval = true };
11873 4495466506 : int i;
11874 4495466506 : tree tmp;
11875 :
11876 4495466506 : if (t == error_mark_node)
11877 : return false;
11878 4495455140 : if (t == NULL_TREE)
11879 : return true;
11880 4488325938 : location_t loc = cp_expr_loc_or_input_loc (t);
11881 4488325938 : iloc_sentinel ils = loc;
11882 :
11883 4488325938 : if (*jump_target)
11884 : /* If we are jumping, ignore everything. This is simpler than the
11885 : cxx_eval_constant_expression handling because we only need to be
11886 : conservatively correct, and we don't necessarily have a constant value
11887 : available, so we don't bother with switch tracking. */
11888 : return true;
11889 :
11890 1609188 : if (TREE_THIS_VOLATILE (t) && want_rval
11891 1043702 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11892 4480891320 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11893 : {
11894 77935 : if (TREE_CLOBBER_P (t))
11895 : {
11896 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
11897 0 : gcc_checking_assert (false);
11898 : return true;
11899 : }
11900 :
11901 77935 : if (flags & tf_error)
11902 22 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11903 : "a volatile lvalue %qE with type %qT", t,
11904 22 : TREE_TYPE (t));
11905 77935 : return false;
11906 : }
11907 4480735424 : if (CONSTANT_CLASS_P (t))
11908 : return true;
11909 3240011024 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11910 3240011024 : && TREE_TYPE (t) == error_mark_node)
11911 : return false;
11912 :
11913 3240010987 : switch (TREE_CODE (t))
11914 : {
11915 : case FUNCTION_DECL:
11916 : case BASELINK:
11917 : case TEMPLATE_DECL:
11918 : case OVERLOAD:
11919 : case TEMPLATE_ID_EXPR:
11920 : case LABEL_DECL:
11921 : case CASE_LABEL_EXPR:
11922 : case PREDICT_EXPR:
11923 : case CONST_DECL:
11924 : case SIZEOF_EXPR:
11925 : case ALIGNOF_EXPR:
11926 : case OFFSETOF_EXPR:
11927 : case NOEXCEPT_EXPR:
11928 : case TEMPLATE_PARM_INDEX:
11929 : case TRAIT_EXPR:
11930 : case IDENTIFIER_NODE:
11931 : case USERDEF_LITERAL:
11932 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11933 : case FIELD_DECL:
11934 : case RESULT_DECL:
11935 : case USING_DECL:
11936 : case USING_STMT:
11937 : case PLACEHOLDER_EXPR:
11938 : case REQUIRES_EXPR:
11939 : case STATIC_ASSERT:
11940 : case DEBUG_BEGIN_STMT:
11941 : case REFLECT_EXPR:
11942 : return true;
11943 :
11944 23213703 : case RETURN_EXPR:
11945 23213703 : if (!RECUR (TREE_OPERAND (t, 0), any))
11946 : return false;
11947 : /* FALLTHROUGH */
11948 :
11949 23072210 : case BREAK_STMT:
11950 23072210 : case CONTINUE_STMT:
11951 23072210 : *jump_target = t;
11952 23072210 : return true;
11953 :
11954 341060886 : case PARM_DECL:
11955 341060886 : if (now && want_rval)
11956 : {
11957 85405427 : tree type = TREE_TYPE (t);
11958 85405427 : if (dependent_type_p (type)
11959 57821669 : || !COMPLETE_TYPE_P (processing_template_decl
11960 : ? type : complete_type (type))
11961 143227096 : || is_really_empty_class (type, /*ignore_vptr*/false))
11962 : /* An empty class has no data to read. */
11963 27584284 : return true;
11964 57821143 : if (flags & tf_error)
11965 17 : constexpr_error (input_location, fundef_p,
11966 : "%qE is not a constant expression", t);
11967 57821143 : return false;
11968 : }
11969 : return true;
11970 :
11971 251222429 : case AGGR_INIT_EXPR:
11972 251222429 : case CALL_EXPR:
11973 : /* -- an invocation of a function other than a constexpr function
11974 : or a constexpr constructor. */
11975 251222429 : {
11976 251222429 : tree fun = get_function_named_in_call (t);
11977 251222429 : const int nargs = call_expr_nargs (t);
11978 251222429 : i = 0;
11979 :
11980 251222429 : if (fun == NULL_TREE)
11981 : {
11982 : /* Reset to allow the function to continue past the end
11983 : of the block below. Otherwise return early. */
11984 399773 : bool bail = true;
11985 :
11986 399773 : if (TREE_CODE (t) == CALL_EXPR
11987 399773 : && CALL_EXPR_FN (t) == NULL_TREE)
11988 399773 : switch (CALL_EXPR_IFN (t))
11989 : {
11990 : /* These should be ignored, they are optimized away from
11991 : constexpr functions. */
11992 : case IFN_UBSAN_NULL:
11993 : case IFN_UBSAN_BOUNDS:
11994 : case IFN_UBSAN_VPTR:
11995 : case IFN_FALLTHROUGH:
11996 : case IFN_ASSUME:
11997 : return true;
11998 :
11999 : case IFN_ADD_OVERFLOW:
12000 : case IFN_SUB_OVERFLOW:
12001 : case IFN_MUL_OVERFLOW:
12002 : case IFN_LAUNDER:
12003 : case IFN_VEC_CONVERT:
12004 : bail = false;
12005 : break;
12006 :
12007 : default:
12008 : break;
12009 : }
12010 :
12011 : if (bail)
12012 : {
12013 : /* fold_call_expr can't do anything with IFN calls. */
12014 66 : if (flags & tf_error)
12015 0 : constexpr_error (loc, fundef_p,
12016 : "call to internal function %qE", t);
12017 66 : return false;
12018 : }
12019 : }
12020 :
12021 250822656 : if (fun && is_overloaded_fn (fun))
12022 : {
12023 233195795 : if (!RECUR (fun, true))
12024 : return false;
12025 232769820 : fun = get_fns (fun);
12026 :
12027 232769820 : if (TREE_CODE (fun) == FUNCTION_DECL)
12028 : {
12029 215779863 : if (builtin_valid_in_constant_expr_p (fun))
12030 : return true;
12031 213942391 : if (!maybe_constexpr_fn (fun)
12032 : /* Allow any built-in function; if the expansion
12033 : isn't constant, we'll deal with that then. */
12034 43435671 : && !fndecl_built_in_p (fun)
12035 : /* In C++20, replaceable global allocation functions
12036 : are constant expressions. */
12037 31297948 : && (!cxx_replaceable_global_alloc_fn (fun)
12038 794929 : || TREE_CODE (t) != CALL_EXPR
12039 794929 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
12040 264937 : && (current_function_decl == NULL_TREE
12041 264937 : || !is_std_allocator_allocate
12042 264937 : (current_function_decl))))
12043 : /* Allow placement new in std::construct_at. */
12044 30507129 : && (!cxx_placement_new_fn (fun)
12045 643938 : || TREE_CODE (t) != CALL_EXPR
12046 643938 : || current_function_decl == NULL_TREE
12047 643926 : || !is_std_construct_at (current_function_decl))
12048 30107493 : && !cxx_dynamic_cast_fn_p (fun)
12049 244047081 : && !cxx_cxa_builtin_fn_p (fun))
12050 : {
12051 : /* In C++26 evaluation of the function arguments might
12052 : throw and in that case it is irrelevant whether
12053 : fun is constexpr or not. */
12054 30067001 : if (cxx_dialect >= cxx26)
12055 5771216 : for (; i < nargs; ++i)
12056 : {
12057 3535770 : tree x = get_nth_callarg (t, i);
12058 3535770 : bool rv = processing_template_decl ? any : rval;
12059 3535770 : bool sub_now = false;
12060 3535770 : if (!potential_constant_expression_1 (x, rv, strict,
12061 : sub_now,
12062 : fundef_p,
12063 : flags,
12064 : jump_target))
12065 : return false;
12066 3231394 : if (throws (jump_target))
12067 : return true;
12068 : }
12069 29708712 : if ((flags & tf_error)
12070 29708712 : && constexpr_error (loc, fundef_p,
12071 : "call to non-%<constexpr%> "
12072 : "function %qD", fun))
12073 250 : explain_invalid_constexpr_fn (fun);
12074 29708712 : return false;
12075 : }
12076 : }
12077 :
12078 200865347 : fun = OVL_FIRST (fun);
12079 : /* Skip initial arguments to base constructors. */
12080 200865347 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12081 3878352 : i = num_artificial_parms_for (fun);
12082 : }
12083 17626861 : else if (fun)
12084 : {
12085 17626861 : if (TREE_TYPE (fun)
12086 17626861 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12087 : want_rval = rval;
12088 : else
12089 : want_rval = any;
12090 17626861 : if (RECUR (fun, want_rval))
12091 : /* Might end up being a constant function pointer. But it
12092 : could also be a function object with constexpr op(), so
12093 : we pass 'any' so that the underlying VAR_DECL is deemed
12094 : as potentially-constant even though it wasn't declared
12095 : constexpr. */;
12096 : else
12097 : return false;
12098 : }
12099 482660548 : for (; i < nargs; ++i)
12100 : {
12101 279862649 : tree x = get_nth_callarg (t, i);
12102 : /* In a template, reference arguments haven't been converted to
12103 : REFERENCE_TYPE and we might not even know if the parameter
12104 : is a reference, so accept lvalue constants too. */
12105 279862649 : bool rv = processing_template_decl ? any : rval;
12106 : /* Don't require an immediately constant value, as constexpr
12107 : substitution might not use the value of the argument. */
12108 279862649 : bool sub_now = false;
12109 279862649 : if (!potential_constant_expression_1 (x, rv, strict,
12110 : sub_now, fundef_p, flags,
12111 : jump_target))
12112 : return false;
12113 2927130792 : if (throws (jump_target))
12114 : return true;
12115 : }
12116 : /* In C++26 a function could throw. */
12117 202797899 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12118 6133353 : *jump_target = void_node;
12119 : return true;
12120 : }
12121 :
12122 188989929 : case NON_LVALUE_EXPR:
12123 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
12124 : -- an lvalue of integral type that refers to a non-volatile
12125 : const variable or static data member initialized with
12126 : constant expressions, or
12127 :
12128 : -- an lvalue of literal type that refers to non-volatile
12129 : object defined with constexpr, or that refers to a
12130 : sub-object of such an object; */
12131 188989929 : return RECUR (TREE_OPERAND (t, 0), rval);
12132 :
12133 27007 : case EXCESS_PRECISION_EXPR:
12134 27007 : return RECUR (TREE_OPERAND (t, 0), rval);
12135 :
12136 291855664 : case VAR_DECL:
12137 291855664 : if (DECL_HAS_VALUE_EXPR_P (t))
12138 : {
12139 3895029 : if (now && is_normal_capture_proxy (t))
12140 : {
12141 : /* -- in a lambda-expression, a reference to this or to a
12142 : variable with automatic storage duration defined outside that
12143 : lambda-expression, where the reference would be an
12144 : odr-use. */
12145 :
12146 358646 : if (want_rval)
12147 : /* Since we're doing an lvalue-rvalue conversion, this might
12148 : not be an odr-use, so evaluate the variable directly. */
12149 357482 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
12150 :
12151 1164 : if (flags & tf_error)
12152 : {
12153 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
12154 3 : auto_diagnostic_group d;
12155 3 : if (constexpr_error (input_location, fundef_p,
12156 : "lambda capture of %qE is not a "
12157 : "constant expression", cap)
12158 3 : && decl_constant_var_p (cap))
12159 3 : inform (input_location, "because it is used as a glvalue");
12160 3 : }
12161 1164 : return false;
12162 : }
12163 3536383 : tree ve = DECL_VALUE_EXPR (t);
12164 : /* Treat __PRETTY_FUNCTION__ inside a template function as
12165 : potentially-constant. */
12166 3536383 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
12167 : return true;
12168 3536378 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
12169 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
12170 3533642 : return RECUR (ve, rval);
12171 : }
12172 287960635 : if (want_rval
12173 205278264 : && (now || !var_in_maybe_constexpr_fn (t))
12174 188200375 : && !type_dependent_expression_p (t)
12175 157718118 : && !decl_maybe_constant_var_p (t)
12176 50181393 : && !is_local_temp (t)
12177 49662917 : && (strict
12178 1989554 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
12179 655609 : || (DECL_INITIAL (t)
12180 650126 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
12181 49656243 : && COMPLETE_TYPE_P (TREE_TYPE (t))
12182 337616734 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
12183 : {
12184 49651334 : if (flags & tf_error)
12185 163 : non_const_var_error (loc, t, fundef_p);
12186 49651334 : return false;
12187 : }
12188 : return true;
12189 :
12190 401093523 : case NOP_EXPR:
12191 401093523 : if (REINTERPRET_CAST_P (t))
12192 : {
12193 157196 : if (flags & tf_error)
12194 46 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
12195 : "constant expression");
12196 157196 : return false;
12197 : }
12198 : /* FALLTHRU */
12199 822704580 : case CONVERT_EXPR:
12200 822704580 : case VIEW_CONVERT_EXPR:
12201 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
12202 : may change to something more specific to type-punning (DR 1312). */
12203 822704580 : {
12204 822704580 : tree from = TREE_OPERAND (t, 0);
12205 822704580 : if (location_wrapper_p (t))
12206 343441626 : return (RECUR (from, want_rval));
12207 479262954 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
12208 : {
12209 276436033 : STRIP_ANY_LOCATION_WRAPPER (from);
12210 276436033 : if (TREE_CODE (from) == INTEGER_CST
12211 276436033 : && !integer_zerop (from))
12212 : {
12213 1540 : if (flags & tf_error)
12214 25 : constexpr_error (loc, fundef_p,
12215 : "%<reinterpret_cast%> from integer to "
12216 : "pointer");
12217 1540 : return false;
12218 : }
12219 : }
12220 479261414 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
12221 : }
12222 :
12223 12003 : case ADDRESSOF_EXPR:
12224 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
12225 12003 : t = TREE_OPERAND (t, 0);
12226 12003 : goto handle_addr_expr;
12227 :
12228 89319036 : case ADDR_EXPR:
12229 : /* -- a unary operator & that is applied to an lvalue that
12230 : designates an object with thread or automatic storage
12231 : duration; */
12232 89319036 : t = TREE_OPERAND (t, 0);
12233 :
12234 89319036 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
12235 : /* A pointer-to-member constant. */
12236 : return true;
12237 :
12238 89330801 : handle_addr_expr:
12239 : #if 0
12240 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
12241 : any checking here, as we might dereference the pointer later. If
12242 : we remove this code, also remove check_automatic_or_tls. */
12243 : i = check_automatic_or_tls (t);
12244 : if (i == ck_ok)
12245 : return true;
12246 : if (i == ck_bad)
12247 : {
12248 : if (flags & tf_error)
12249 : error ("address-of an object %qE with thread local or "
12250 : "automatic storage is not a constant expression", t);
12251 : return false;
12252 : }
12253 : #endif
12254 89330801 : return RECUR (t, any);
12255 :
12256 93402478 : case COMPONENT_REF:
12257 93402478 : case ARROW_EXPR:
12258 93402478 : case OFFSET_REF:
12259 : /* -- a class member access unless its postfix-expression is
12260 : of literal type or of pointer to literal type. */
12261 : /* This test would be redundant, as it follows from the
12262 : postfix-expression being a potential constant expression. */
12263 93402478 : if (type_unknown_p (t))
12264 : return true;
12265 85657723 : if (is_overloaded_fn (t))
12266 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
12267 : which uses ob as an lvalue. */
12268 87211735 : want_rval = false;
12269 87211735 : gcc_fallthrough ();
12270 :
12271 87211735 : case REALPART_EXPR:
12272 87211735 : case IMAGPART_EXPR:
12273 87211735 : case BIT_FIELD_REF:
12274 87211735 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12275 :
12276 253613 : case EXPR_PACK_EXPANSION:
12277 253613 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
12278 :
12279 : case PACK_INDEX_EXPR:
12280 : return true;
12281 :
12282 99895371 : case INDIRECT_REF:
12283 99895371 : return RECUR (TREE_OPERAND (t, 0), rval);
12284 :
12285 23697065 : case STATEMENT_LIST:
12286 4581034865 : for (tree stmt : tsi_range (t))
12287 69535249 : if (!RECUR (stmt, any))
12288 262875779 : return false;
12289 : return true;
12290 :
12291 4209596 : case MODIFY_EXPR:
12292 4209596 : if (cxx_dialect < cxx14)
12293 1983 : goto fail;
12294 4207613 : if (!RECUR (TREE_OPERAND (t, 0), any))
12295 : return false;
12296 : /* Just ignore clobbers. */
12297 3781837 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12298 : return true;
12299 3336604 : if (!RECUR (TREE_OPERAND (t, 1), rval))
12300 : return false;
12301 : return true;
12302 :
12303 876290 : case MODOP_EXPR:
12304 876290 : if (cxx_dialect < cxx14)
12305 96 : goto fail;
12306 876194 : if (!RECUR (TREE_OPERAND (t, 0), rval))
12307 : return false;
12308 866252 : if (!RECUR (TREE_OPERAND (t, 2), rval))
12309 : return false;
12310 : return true;
12311 :
12312 514259 : case DO_STMT:
12313 514259 : if (!RECUR (DO_COND (t), rval))
12314 : return false;
12315 514259 : if (!RECUR (DO_BODY (t), any))
12316 : return false;
12317 513970 : if (breaks (jump_target) || continues (jump_target))
12318 2 : *jump_target = NULL_TREE;
12319 : return true;
12320 :
12321 417035 : case FOR_STMT:
12322 417035 : if (!RECUR (FOR_INIT_STMT (t), any))
12323 : return false;
12324 417035 : if (!RECUR (FOR_COND_PREP (t), any))
12325 : return false;
12326 417035 : tmp = FOR_COND (t);
12327 417035 : if (!RECUR (tmp, rval))
12328 : return false;
12329 416967 : if (tmp)
12330 : {
12331 371992 : if (!processing_template_decl)
12332 368844 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12333 : /* If we couldn't evaluate the condition, it might not ever be
12334 : true. */
12335 371992 : if (!integer_onep (tmp))
12336 : {
12337 : /* Before returning true, check if the for body can contain
12338 : a return. */
12339 371992 : hash_set<tree> pset;
12340 371992 : check_for_return_continue_data data = { &pset, NULL_TREE,
12341 371992 : NULL_TREE, false };
12342 371992 : if (tree ret_expr
12343 371992 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
12344 : &data, &pset))
12345 130642 : *jump_target = ret_expr;
12346 371992 : if (data.could_throw)
12347 9176 : *jump_target = void_node;
12348 371992 : return true;
12349 371992 : }
12350 : }
12351 44975 : if (!RECUR (FOR_EXPR (t), any))
12352 : return false;
12353 44975 : if (!RECUR (FOR_BODY (t), any))
12354 : return false;
12355 44975 : if (!RECUR (FOR_COND_CLEANUP (t), any))
12356 : return false;
12357 44975 : if (breaks (jump_target) || continues (jump_target))
12358 12 : *jump_target = NULL_TREE;
12359 : return true;
12360 :
12361 459 : case RANGE_FOR_STMT:
12362 459 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
12363 : return false;
12364 459 : if (!RECUR (RANGE_FOR_EXPR (t), any))
12365 : return false;
12366 459 : if (!RECUR (RANGE_FOR_BODY (t), any))
12367 : return false;
12368 457 : if (breaks (jump_target) || continues (jump_target))
12369 0 : *jump_target = NULL_TREE;
12370 : return true;
12371 :
12372 164781 : case WHILE_STMT:
12373 164781 : if (!RECUR (WHILE_COND_PREP (t), any))
12374 : return false;
12375 164781 : tmp = WHILE_COND (t);
12376 164781 : if (!RECUR (tmp, rval))
12377 : return false;
12378 164717 : if (!processing_template_decl)
12379 164693 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12380 : /* If we couldn't evaluate the condition, it might not ever be true. */
12381 164717 : if (!integer_onep (tmp))
12382 : {
12383 : /* Before returning true, check if the while body can contain
12384 : a return. */
12385 154983 : hash_set<tree> pset;
12386 154983 : check_for_return_continue_data data = { &pset, NULL_TREE,
12387 154983 : NULL_TREE, false };
12388 154983 : if (tree ret_expr
12389 154983 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
12390 : &data, &pset))
12391 417 : *jump_target = ret_expr;
12392 154983 : if (data.could_throw)
12393 3108 : *jump_target = void_node;
12394 154983 : return true;
12395 154983 : }
12396 9734 : if (!RECUR (WHILE_BODY (t), any))
12397 : return false;
12398 9723 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
12399 : return false;
12400 9723 : if (breaks (jump_target) || continues (jump_target))
12401 24 : *jump_target = NULL_TREE;
12402 : return true;
12403 :
12404 24451 : case SWITCH_STMT:
12405 24451 : if (!RECUR (SWITCH_STMT_COND (t), rval))
12406 : return false;
12407 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
12408 : unreachable labels would be checked and it is enough if there is
12409 : a single switch cond value for which it is a valid constant
12410 : expression. We need to check if there are any RETURN_EXPRs
12411 : or CONTINUE_STMTs inside of the body though, as in that case
12412 : we need to set *jump_target. */
12413 : else
12414 : {
12415 24445 : hash_set<tree> pset;
12416 24445 : check_for_return_continue_data data = { &pset, NULL_TREE,
12417 24445 : NULL_TREE, false };
12418 24445 : if (tree ret_expr
12419 24445 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
12420 : &data, &pset))
12421 : /* The switch might return. */
12422 24065 : *jump_target = ret_expr;
12423 380 : else if (data.continue_stmt)
12424 : /* The switch can't return, but might continue. */
12425 3 : *jump_target = data.continue_stmt;
12426 24445 : if (data.could_throw)
12427 25 : *jump_target = void_node;
12428 24445 : }
12429 24445 : return true;
12430 :
12431 50 : case STMT_EXPR:
12432 50 : return RECUR (STMT_EXPR_STMT (t), rval);
12433 :
12434 538741 : case LAMBDA_EXPR:
12435 538741 : if (cxx_dialect >= cxx17)
12436 : /* In C++17 lambdas can be constexpr, don't give up yet. */
12437 : return true;
12438 424 : else if (flags & tf_error)
12439 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
12440 : "constant expression before C++17");
12441 : return false;
12442 :
12443 109779 : case NEW_EXPR:
12444 109779 : case VEC_NEW_EXPR:
12445 109779 : case DELETE_EXPR:
12446 109779 : case VEC_DELETE_EXPR:
12447 109779 : if (cxx_dialect >= cxx20)
12448 : /* In C++20, new-expressions are potentially constant. */
12449 : return true;
12450 1041 : else if (flags & tf_error)
12451 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
12452 : "constant expression before C++20");
12453 : return false;
12454 :
12455 80189 : case DYNAMIC_CAST_EXPR:
12456 80189 : case PSEUDO_DTOR_EXPR:
12457 80189 : case OMP_PARALLEL:
12458 80189 : case OMP_TASK:
12459 80189 : case OMP_FOR:
12460 80189 : case OMP_SIMD:
12461 80189 : case OMP_DISTRIBUTE:
12462 80189 : case OMP_TASKLOOP:
12463 80189 : case OMP_LOOP:
12464 80189 : case OMP_TEAMS:
12465 80189 : case OMP_TARGET_DATA:
12466 80189 : case OMP_TARGET:
12467 80189 : case OMP_SECTIONS:
12468 80189 : case OMP_ORDERED:
12469 80189 : case OMP_CRITICAL:
12470 80189 : case OMP_SINGLE:
12471 80189 : case OMP_SCAN:
12472 80189 : case OMP_SCOPE:
12473 80189 : case OMP_SECTION:
12474 80189 : case OMP_MASTER:
12475 80189 : case OMP_MASKED:
12476 80189 : case OMP_TASKGROUP:
12477 80189 : case OMP_TARGET_UPDATE:
12478 80189 : case OMP_TARGET_ENTER_DATA:
12479 80189 : case OMP_TARGET_EXIT_DATA:
12480 80189 : case OMP_ATOMIC:
12481 80189 : case OMP_ATOMIC_READ:
12482 80189 : case OMP_ATOMIC_CAPTURE_OLD:
12483 80189 : case OMP_ATOMIC_CAPTURE_NEW:
12484 80189 : case OMP_DEPOBJ:
12485 80189 : case OACC_PARALLEL:
12486 80189 : case OACC_KERNELS:
12487 80189 : case OACC_SERIAL:
12488 80189 : case OACC_DATA:
12489 80189 : case OACC_HOST_DATA:
12490 80189 : case OACC_LOOP:
12491 80189 : case OACC_CACHE:
12492 80189 : case OACC_DECLARE:
12493 80189 : case OACC_ENTER_DATA:
12494 80189 : case OACC_EXIT_DATA:
12495 80189 : case OACC_UPDATE:
12496 80189 : case OMP_ARRAY_SECTION:
12497 : /* GCC internal stuff. */
12498 80189 : case VA_ARG_EXPR:
12499 80189 : case TRANSACTION_EXPR:
12500 80189 : case AT_ENCODE_EXPR:
12501 80189 : fail:
12502 80189 : if (flags & tf_error)
12503 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12504 : "expression", t);
12505 : return false;
12506 :
12507 : case OMP_DECLARE_MAPPER:
12508 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12509 : constant. */
12510 : return true;
12511 :
12512 15593 : case THROW_EXPR:
12513 15593 : if (cxx_dialect < cxx26)
12514 279 : goto fail;
12515 15314 : return RECUR (TREE_OPERAND (t, 0), rval);
12516 :
12517 517 : case ASM_EXPR:
12518 517 : if (flags & tf_error)
12519 5 : inline_asm_in_constexpr_error (loc, fundef_p);
12520 : return false;
12521 :
12522 338460 : case OBJ_TYPE_REF:
12523 338460 : if (cxx_dialect >= cxx20)
12524 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12525 : return true;
12526 6602 : else if (flags & tf_error)
12527 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12528 : "%<constexpr%> before C++20");
12529 : return false;
12530 :
12531 4460 : case TYPEID_EXPR:
12532 : /* In C++20, a typeid expression whose operand is of polymorphic
12533 : class type can be constexpr. */
12534 4460 : {
12535 4460 : tree e = TREE_OPERAND (t, 0);
12536 4460 : if (cxx_dialect < cxx20
12537 26 : && strict
12538 26 : && !TYPE_P (e)
12539 19 : && !type_dependent_expression_p (e)
12540 7 : && CLASS_TYPE_P (TREE_TYPE (e))
12541 4465 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12542 : {
12543 4 : if (flags & tf_error)
12544 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12545 : "constant expression because %qE is "
12546 : "of polymorphic type", e);
12547 4 : return false;
12548 : }
12549 : return true;
12550 : }
12551 :
12552 28860308 : case POINTER_DIFF_EXPR:
12553 28860308 : case MINUS_EXPR:
12554 28860308 : want_rval = true;
12555 28860308 : goto binary;
12556 :
12557 75024886 : case LT_EXPR:
12558 75024886 : case LE_EXPR:
12559 75024886 : case GT_EXPR:
12560 75024886 : case GE_EXPR:
12561 75024886 : case EQ_EXPR:
12562 75024886 : case NE_EXPR:
12563 75024886 : case SPACESHIP_EXPR:
12564 75024886 : want_rval = true;
12565 75024886 : goto binary;
12566 :
12567 1789816 : case PREINCREMENT_EXPR:
12568 1789816 : case POSTINCREMENT_EXPR:
12569 1789816 : case PREDECREMENT_EXPR:
12570 1789816 : case POSTDECREMENT_EXPR:
12571 1789816 : if (cxx_dialect < cxx14)
12572 8789 : goto fail;
12573 1781027 : goto unary;
12574 :
12575 1039052 : case BIT_NOT_EXPR:
12576 : /* A destructor. */
12577 1039052 : if (TYPE_P (TREE_OPERAND (t, 0)))
12578 : return true;
12579 : /* fall through. */
12580 :
12581 43310854 : case CONJ_EXPR:
12582 43310854 : case SAVE_EXPR:
12583 43310854 : case FIX_TRUNC_EXPR:
12584 43310854 : case FLOAT_EXPR:
12585 43310854 : case NEGATE_EXPR:
12586 43310854 : case ABS_EXPR:
12587 43310854 : case ABSU_EXPR:
12588 43310854 : case TRUTH_NOT_EXPR:
12589 43310854 : case FIXED_CONVERT_EXPR:
12590 43310854 : case UNARY_PLUS_EXPR:
12591 43310854 : case UNARY_LEFT_FOLD_EXPR:
12592 43310854 : case UNARY_RIGHT_FOLD_EXPR:
12593 43310854 : case VEC_DUPLICATE_EXPR:
12594 1039052 : unary:
12595 43310854 : return RECUR (TREE_OPERAND (t, 0), rval);
12596 :
12597 30177142 : case CAST_EXPR:
12598 30177142 : case CONST_CAST_EXPR:
12599 30177142 : case STATIC_CAST_EXPR:
12600 30177142 : case REINTERPRET_CAST_EXPR:
12601 30177142 : case IMPLICIT_CONV_EXPR:
12602 30177142 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12603 : /* In C++98, a conversion to non-integral type can't be part of a
12604 : constant expression. */
12605 : {
12606 341 : if (flags & tf_error)
12607 0 : constexpr_error (loc, fundef_p,
12608 : "cast to non-integral type %qT in a constant "
12609 0 : "expression", TREE_TYPE (t));
12610 341 : return false;
12611 : }
12612 : /* This might be a conversion from a class to a (potentially) literal
12613 : type. Let's consider it potentially constant since the conversion
12614 : might be a constexpr user-defined conversion. */
12615 30176801 : else if (cxx_dialect >= cxx11
12616 30155423 : && (dependent_type_p (TREE_TYPE (t))
12617 10555069 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12618 10541833 : || literal_type_p (TREE_TYPE (t)))
12619 30118607 : && TREE_OPERAND (t, 0)
12620 60062983 : && (TREE_CODE (t) != CAST_EXPR
12621 22610643 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12622 : {
12623 29845661 : tree from = TREE_OPERAND (t, 0);
12624 29845661 : if (TREE_CODE (t) == CAST_EXPR)
12625 22570122 : from = TREE_VALUE (from);
12626 29845661 : tree type = TREE_TYPE (from);
12627 : /* If this is a dependent type, it could end up being a class
12628 : with conversions. */
12629 29845661 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12630 : return true;
12631 : /* Or a non-dependent class which has conversions. */
12632 609057 : else if (CLASS_TYPE_P (type)
12633 609057 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12634 268906 : return true;
12635 : }
12636 :
12637 23964366 : return (RECUR (TREE_OPERAND (t, 0),
12638 23964366 : !TYPE_REF_P (TREE_TYPE (t))));
12639 :
12640 12892869 : case BIND_EXPR:
12641 12892869 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12642 :
12643 66020144 : case CLEANUP_POINT_EXPR:
12644 66020144 : case MUST_NOT_THROW_EXPR:
12645 66020144 : case TRY_CATCH_EXPR:
12646 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12647 : body could throw, even with catch (...) among handlers we'd need
12648 : to analyze them in detail if they couldn't rethrow it. More
12649 : importantly though, throws (jump_target) is just conservative,
12650 : and there could be e.g.
12651 : try
12652 : {
12653 : possibly_throwing_fn (args);
12654 : break;
12655 : }
12656 : catch (...)
12657 : {
12658 : }
12659 : or continue or return instead of break. So, clearing *jump_target
12660 : because we see catch (...) handler might mean we missed break
12661 : etc. */
12662 66020144 : case TRY_BLOCK:
12663 66020144 : case EH_SPEC_BLOCK:
12664 66020144 : case EXPR_STMT:
12665 66020144 : case PAREN_EXPR:
12666 : /* For convenience. */
12667 66020144 : case LOOP_EXPR:
12668 66020144 : case EXIT_EXPR:
12669 66020144 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12670 :
12671 11736388 : case DECL_EXPR:
12672 11736388 : tmp = DECL_EXPR_DECL (t);
12673 7529924 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12674 17291589 : && (processing_template_decl
12675 5098985 : ? !decl_maybe_constant_var_p (tmp)
12676 4642769 : : !decl_constant_var_p (tmp)))
12677 : {
12678 4507981 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12679 : {
12680 8 : if (flags & tf_error)
12681 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12682 : "%qD defined %<thread_local%> in "
12683 : "%<constexpr%> context", tmp);
12684 8 : return false;
12685 : }
12686 4507973 : else if (TREE_STATIC (tmp))
12687 : {
12688 114 : if (flags & tf_error)
12689 25 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12690 : "%qD defined %<static%> in %<constexpr%> "
12691 : "context", tmp);
12692 114 : return false;
12693 : }
12694 4507859 : else if (!check_for_uninitialized_const_var
12695 4507859 : (tmp, /*constexpr_context_p=*/true, flags))
12696 : return false;
12697 : }
12698 11732531 : if (VAR_P (tmp))
12699 7526067 : return RECUR (DECL_INITIAL (tmp), want_rval);
12700 : return true;
12701 :
12702 27385 : case TRY_FINALLY_EXPR:
12703 27385 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12704 27385 : && RECUR (TREE_OPERAND (t, 1), any));
12705 :
12706 0 : case EH_ELSE_EXPR:
12707 : /* maybe_apply_function_contracts uses this to check postconditions only
12708 : on normal return. */
12709 0 : return (RECUR (TREE_OPERAND (t, 1), any)
12710 0 : || RECUR (TREE_OPERAND (t, 0), any));
12711 :
12712 23913194 : case SCOPE_REF:
12713 23913194 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12714 :
12715 45266686 : case TARGET_EXPR:
12716 45266686 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12717 45079595 : && !TARGET_EXPR_ELIDING_P (t)
12718 81755521 : && !literal_type_p (TREE_TYPE (t)))
12719 : {
12720 807643 : if (flags & tf_error)
12721 : {
12722 23 : auto_diagnostic_group d;
12723 23 : if (constexpr_error (loc, fundef_p,
12724 : "temporary of non-literal type %qT in a "
12725 23 : "constant expression", TREE_TYPE (t)))
12726 23 : explain_non_literal_class (TREE_TYPE (t));
12727 23 : }
12728 807643 : return false;
12729 : }
12730 : /* FALLTHRU */
12731 71138663 : case INIT_EXPR:
12732 71138663 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12733 : return true;
12734 71074070 : return RECUR (TREE_OPERAND (t, 1), rval);
12735 :
12736 40035727 : case CONSTRUCTOR:
12737 40035727 : {
12738 40035727 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12739 40035727 : constructor_elt *ce;
12740 4747634298 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12741 220556872 : if (!RECUR (ce->value, want_rval))
12742 : return false;
12743 : return true;
12744 : }
12745 :
12746 20331784 : case TREE_LIST:
12747 20331784 : {
12748 20331784 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12749 : || DECL_P (TREE_PURPOSE (t)));
12750 20331784 : if (!RECUR (TREE_VALUE (t), want_rval))
12751 : return false;
12752 18215436 : if (TREE_CHAIN (t) == NULL_TREE)
12753 : return true;
12754 51656 : return RECUR (TREE_CHAIN (t), want_rval);
12755 : }
12756 :
12757 14811818 : case TRUNC_DIV_EXPR:
12758 14811818 : case CEIL_DIV_EXPR:
12759 14811818 : case FLOOR_DIV_EXPR:
12760 14811818 : case ROUND_DIV_EXPR:
12761 14811818 : case TRUNC_MOD_EXPR:
12762 14811818 : case CEIL_MOD_EXPR:
12763 14811818 : case ROUND_MOD_EXPR:
12764 14811818 : {
12765 14811818 : tree denom = TREE_OPERAND (t, 1);
12766 14811818 : if (!RECUR (denom, rval))
12767 : return false;
12768 : /* We can't call cxx_eval_outermost_constant_expr on an expression
12769 : that hasn't been through instantiate_non_dependent_expr yet. */
12770 14369827 : if (!processing_template_decl)
12771 7604806 : denom = cxx_eval_outermost_constant_expr (denom, true);
12772 14369827 : if (integer_zerop (denom))
12773 : {
12774 337 : if (flags & tf_error)
12775 35 : constexpr_error (input_location, fundef_p,
12776 : "division by zero is not a constant expression");
12777 337 : return false;
12778 : }
12779 : else
12780 : {
12781 14369490 : want_rval = true;
12782 14369490 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12783 : }
12784 : }
12785 :
12786 6783840 : case COMPOUND_EXPR:
12787 6783840 : {
12788 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12789 : COMPOUND_EXPR; don't get confused. */
12790 6783840 : tree op0 = TREE_OPERAND (t, 0);
12791 6783840 : tree op1 = TREE_OPERAND (t, 1);
12792 6783840 : STRIP_NOPS (op1);
12793 6783840 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12794 1259886 : return RECUR (op0, want_rval);
12795 : else
12796 5523954 : goto binary;
12797 : }
12798 :
12799 : /* If the first operand is the non-short-circuit constant, look at
12800 : the second operand; otherwise we only care about the first one for
12801 : potentiality. */
12802 17182097 : case TRUTH_AND_EXPR:
12803 17182097 : case TRUTH_ANDIF_EXPR:
12804 17182097 : tmp = boolean_true_node;
12805 17182097 : goto truth;
12806 6800147 : case TRUTH_OR_EXPR:
12807 6800147 : case TRUTH_ORIF_EXPR:
12808 6800147 : tmp = boolean_false_node;
12809 23982244 : truth:
12810 23982244 : {
12811 23982244 : tree op0 = TREE_OPERAND (t, 0);
12812 23982244 : tree op1 = TREE_OPERAND (t, 1);
12813 23982244 : if (!RECUR (op0, rval))
12814 : return false;
12815 16429875 : if (!(flags & tf_error) && RECUR (op1, rval))
12816 : /* When quiet, try to avoid expensive trial evaluation by first
12817 : checking potentiality of the second operand. */
12818 : return true;
12819 1727129 : if (!processing_template_decl)
12820 1333661 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12821 1727129 : if (tree_int_cst_equal (op0, tmp))
12822 5143 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12823 : else
12824 : return true;
12825 : }
12826 :
12827 : case PLUS_EXPR:
12828 : case MULT_EXPR:
12829 : case POINTER_PLUS_EXPR:
12830 : case RDIV_EXPR:
12831 : case EXACT_DIV_EXPR:
12832 : case MIN_EXPR:
12833 : case MAX_EXPR:
12834 : case LSHIFT_EXPR:
12835 : case RSHIFT_EXPR:
12836 : case LROTATE_EXPR:
12837 : case RROTATE_EXPR:
12838 : case BIT_IOR_EXPR:
12839 : case BIT_XOR_EXPR:
12840 : case BIT_AND_EXPR:
12841 : case TRUTH_XOR_EXPR:
12842 : case UNORDERED_EXPR:
12843 : case ORDERED_EXPR:
12844 : case UNLT_EXPR:
12845 : case UNLE_EXPR:
12846 : case UNGT_EXPR:
12847 : case UNGE_EXPR:
12848 : case UNEQ_EXPR:
12849 : case LTGT_EXPR:
12850 : case RANGE_EXPR:
12851 : case COMPLEX_EXPR:
12852 222112153 : want_rval = true;
12853 : /* Fall through. */
12854 222112153 : case ARRAY_REF:
12855 222112153 : case ARRAY_RANGE_REF:
12856 222112153 : case MEMBER_REF:
12857 222112153 : case DOTSTAR_EXPR:
12858 222112153 : case MEM_REF:
12859 222112153 : case BINARY_LEFT_FOLD_EXPR:
12860 222112153 : case BINARY_RIGHT_FOLD_EXPR:
12861 222112153 : binary:
12862 490745858 : for (i = 0; i < 2; ++i)
12863 362902998 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12864 : return false;
12865 : return true;
12866 :
12867 : case VEC_PERM_EXPR:
12868 106540 : for (i = 0; i < 3; ++i)
12869 106098 : if (!RECUR (TREE_OPERAND (t, i), true))
12870 : return false;
12871 : return true;
12872 :
12873 6329686 : case COND_EXPR:
12874 6329686 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12875 : {
12876 8 : if (flags & tf_error)
12877 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12878 : "constant expression");
12879 8 : return false;
12880 : }
12881 : /* Fall through. */
12882 15922783 : case IF_STMT:
12883 15922783 : case VEC_COND_EXPR:
12884 : /* If the condition is a known constant, we know which of the legs we
12885 : care about; otherwise we only require that the condition and
12886 : either of the legs be potentially constant. */
12887 15922783 : tmp = TREE_OPERAND (t, 0);
12888 15922783 : if (!RECUR (tmp, rval))
12889 : return false;
12890 14335307 : if (!processing_template_decl)
12891 12421508 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12892 : /* potential_constant_expression* isn't told if it is called for
12893 : manifestly_const_eval or not, so for consteval if always
12894 : process both branches as if the condition is not a known
12895 : constant. */
12896 14335307 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12897 : {
12898 14302325 : if (integer_zerop (tmp))
12899 3005172 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12900 11297153 : else if (TREE_CODE (tmp) == INTEGER_CST)
12901 3487133 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12902 : }
12903 7843002 : tmp = *jump_target;
12904 8978079 : for (i = 1; i < 3; ++i)
12905 : {
12906 8905079 : tree this_jump_target = tmp;
12907 8905079 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12908 : want_rval, strict, now, fundef_p,
12909 : tf_none, &this_jump_target))
12910 : {
12911 7932463 : if (returns (&this_jump_target) || throws (&this_jump_target))
12912 2215586 : *jump_target = this_jump_target;
12913 5554416 : else if (!returns (jump_target) && !throws (jump_target))
12914 : {
12915 5554416 : if (breaks (&this_jump_target)
12916 5554416 : || continues (&this_jump_target))
12917 42 : *jump_target = this_jump_target;
12918 5554416 : if (i == 1)
12919 : {
12920 : /* If the then branch is potentially constant, but
12921 : does not return, check if the else branch
12922 : couldn't return, break or continue. */
12923 4651689 : hash_set<tree> pset;
12924 4651689 : check_for_return_continue_data data = { &pset, NULL_TREE,
12925 : NULL_TREE,
12926 4651689 : false };
12927 9303378 : if (tree ret_expr
12928 4651689 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12929 : check_for_return_continue, &data,
12930 : &pset))
12931 60963 : *jump_target = ret_expr;
12932 4590726 : else if (*jump_target == NULL_TREE)
12933 : {
12934 4590687 : if (data.continue_stmt)
12935 0 : *jump_target = data.continue_stmt;
12936 4590687 : else if (data.break_stmt)
12937 13 : *jump_target = data.break_stmt;
12938 : }
12939 4651689 : if (data.could_throw)
12940 22445 : *jump_target = void_node;
12941 4651689 : }
12942 : }
12943 7770002 : return true;
12944 : }
12945 : }
12946 73000 : if (flags & tf_error)
12947 : {
12948 3 : if (TREE_CODE (t) == IF_STMT)
12949 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
12950 : "constant expression");
12951 : else
12952 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
12953 : "constant expression", t);
12954 : }
12955 : return false;
12956 :
12957 1043 : case VEC_INIT_EXPR:
12958 1043 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
12959 : return true;
12960 411 : if (flags & tf_error)
12961 : {
12962 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
12963 : "initialization"))
12964 2 : diagnose_non_constexpr_vec_init (t);
12965 : }
12966 : return false;
12967 :
12968 : case TYPE_DECL:
12969 : case TAG_DEFN:
12970 : /* We can see these in statement-expressions. */
12971 : return true;
12972 :
12973 1342378 : case CLEANUP_STMT:
12974 1342378 : if (!RECUR (CLEANUP_BODY (t), any))
12975 : return false;
12976 1341855 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
12977 : return false;
12978 : return true;
12979 :
12980 : case EMPTY_CLASS_EXPR:
12981 : return true;
12982 :
12983 36 : case GOTO_EXPR:
12984 36 : {
12985 36 : tree *target = &TREE_OPERAND (t, 0);
12986 : /* Gotos representing break, continue and cdtor return are OK. */
12987 36 : if (breaks (target) || continues (target) || returns (target))
12988 : {
12989 18 : *jump_target = *target;
12990 18 : return true;
12991 : }
12992 18 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
12993 : /* The user didn't write this goto, this isn't the problem. */
12994 : return true;
12995 16 : if (flags & tf_error)
12996 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
12997 : "expression");
12998 : return false;
12999 : }
13000 :
13001 : case ASSERTION_STMT:
13002 : case PRECONDITION_STMT:
13003 : case POSTCONDITION_STMT:
13004 : /* Contracts are not supposed to alter this; we have to check that this
13005 : is not violated at a later time. */
13006 : return true;
13007 :
13008 223 : case LABEL_EXPR:
13009 223 : t = LABEL_EXPR_LABEL (t);
13010 223 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
13011 : return true;
13012 25 : else if (flags & tf_error)
13013 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
13014 : "function only available with %<-std=c++23%> or "
13015 : "%<-std=gnu++23%>");
13016 : return false;
13017 :
13018 9851 : case ANNOTATE_EXPR:
13019 9851 : return RECUR (TREE_OPERAND (t, 0), rval);
13020 :
13021 130611 : case BIT_CAST_EXPR:
13022 130611 : return RECUR (TREE_OPERAND (t, 0), rval);
13023 :
13024 : /* Coroutine await, yield and return expressions are not. */
13025 718 : case CO_AWAIT_EXPR:
13026 718 : case CO_YIELD_EXPR:
13027 718 : case CO_RETURN_EXPR:
13028 718 : case TEMPLATE_FOR_STMT:
13029 718 : if (flags & tf_error)
13030 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
13031 : "%qE is not a constant expression", t);
13032 : return false;
13033 :
13034 : /* Assume a TU-local entity is not constant, we'll error later when
13035 : instantiating. */
13036 : case TU_LOCAL_ENTITY:
13037 : return false;
13038 :
13039 : /* A splice expression is dependent, but will be constant after
13040 : substitution. */
13041 : case SPLICE_EXPR:
13042 : return true;
13043 :
13044 30 : case NONTYPE_ARGUMENT_PACK:
13045 30 : {
13046 30 : tree args = ARGUMENT_PACK_ARGS (t);
13047 30 : int len = TREE_VEC_LENGTH (args);
13048 90 : for (int i = 0; i < len; ++i)
13049 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
13050 : return false;
13051 : return true;
13052 : }
13053 :
13054 0 : default:
13055 0 : if (objc_non_constant_expr_p (t))
13056 : return false;
13057 :
13058 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13059 0 : gcc_unreachable ();
13060 : return false;
13061 : }
13062 : #undef RECUR
13063 4488325938 : }
13064 :
13065 : bool
13066 1616881253 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13067 : bool fundef_p, tsubst_flags_t flags)
13068 : {
13069 1616881253 : if (flags & tf_error)
13070 : {
13071 : /* Check potentiality quietly first, as that could be performed more
13072 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13073 : that fails, replay the check noisily to give errors. */
13074 10027617 : flags &= ~tf_error;
13075 10027617 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13076 : flags))
13077 : return true;
13078 972 : flags |= tf_error;
13079 : }
13080 :
13081 1606854608 : tree target = NULL_TREE;
13082 1606854608 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13083 1606854608 : flags, &target);
13084 : }
13085 :
13086 : /* The main entry point to the above. */
13087 :
13088 : bool
13089 412230888 : potential_constant_expression (tree t)
13090 : {
13091 412230888 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13092 : /*now*/false, /*fundef_p*/false,
13093 412230888 : tf_none);
13094 : }
13095 :
13096 : /* As above, but require a constant rvalue. */
13097 :
13098 : bool
13099 32914753 : potential_rvalue_constant_expression (tree t)
13100 : {
13101 32914753 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13102 : /*now*/false, /*fundef_p*/false,
13103 32914753 : tf_none);
13104 : }
13105 :
13106 : /* Like above, but complain about non-constant expressions. */
13107 :
13108 : bool
13109 80 : require_potential_constant_expression (tree t)
13110 : {
13111 80 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13112 : /*now*/false, /*fundef_p*/false,
13113 80 : tf_warning_or_error);
13114 : }
13115 :
13116 : /* Cross product of the above. */
13117 :
13118 : bool
13119 127405 : require_potential_rvalue_constant_expression (tree t)
13120 : {
13121 127405 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13122 : /*now*/false, /*fundef_p*/false,
13123 127405 : tf_warning_or_error);
13124 : }
13125 :
13126 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
13127 :
13128 : bool
13129 252 : require_potential_rvalue_constant_expression_fncheck (tree t)
13130 : {
13131 252 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13132 : /*now*/false, /*fundef_p*/true,
13133 252 : tf_warning_or_error);
13134 : }
13135 :
13136 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
13137 :
13138 : bool
13139 771 : require_rvalue_constant_expression (tree t)
13140 : {
13141 771 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13142 : /*now*/true, /*fundef_p*/false,
13143 771 : tf_warning_or_error);
13144 : }
13145 :
13146 : /* Like potential_constant_expression, but don't consider possible constexpr
13147 : substitution of the current function. That is, PARM_DECL qualifies under
13148 : potential_constant_expression, but not here.
13149 :
13150 : This is basically what you can check when any actual constant values might
13151 : be value-dependent. */
13152 :
13153 : bool
13154 868616621 : is_constant_expression (tree t)
13155 : {
13156 868616621 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13157 : /*now*/true, /*fundef_p*/false,
13158 868616621 : tf_none);
13159 : }
13160 :
13161 : /* As above, but expect an rvalue. */
13162 :
13163 : bool
13164 148783887 : is_rvalue_constant_expression (tree t)
13165 : {
13166 148783887 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13167 : /*now*/true, /*fundef_p*/false,
13168 148783887 : tf_none);
13169 : }
13170 :
13171 : /* Like above, but complain about non-constant expressions. */
13172 :
13173 : bool
13174 9899109 : require_constant_expression (tree t)
13175 : {
13176 9899109 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13177 : /*now*/true, /*fundef_p*/false,
13178 9899109 : tf_warning_or_error);
13179 : }
13180 :
13181 : /* Like is_constant_expression, but allow const variables that are not allowed
13182 : under constexpr rules. */
13183 :
13184 : bool
13185 134279870 : is_static_init_expression (tree t)
13186 : {
13187 134279870 : return potential_constant_expression_1 (t, /*want_rval*/false,
13188 : /*strict*/false, /*now*/true,
13189 134279870 : /*fundef_p*/false, tf_none);
13190 : }
13191 :
13192 : /* Returns true if T is a potential constant expression that is not
13193 : instantiation-dependent, and therefore a candidate for constant folding even
13194 : in a template. */
13195 :
13196 : bool
13197 838771534 : is_nondependent_constant_expression (tree t)
13198 : {
13199 838771534 : return (!type_unknown_p (t)
13200 838771485 : && is_constant_expression (t)
13201 1571335270 : && !instantiation_dependent_expression_p (t));
13202 : }
13203 :
13204 : /* Returns true if T is a potential static initializer expression that is not
13205 : instantiation-dependent. */
13206 :
13207 : bool
13208 134279870 : is_nondependent_static_init_expression (tree t)
13209 : {
13210 134279870 : return (!type_unknown_p (t)
13211 134279870 : && is_static_init_expression (t)
13212 254218365 : && !instantiation_dependent_expression_p (t));
13213 : }
13214 :
13215 : /* True iff FN is an implicitly constexpr function. */
13216 :
13217 : bool
13218 180267 : decl_implicit_constexpr_p (tree fn)
13219 : {
13220 180267 : if (!(flag_implicit_constexpr
13221 2 : && TREE_CODE (fn) == FUNCTION_DECL
13222 2 : && DECL_DECLARED_CONSTEXPR_P (fn)))
13223 : return false;
13224 :
13225 2 : if (DECL_CLONED_FUNCTION_P (fn))
13226 0 : fn = DECL_CLONED_FUNCTION (fn);
13227 :
13228 2 : return (DECL_LANG_SPECIFIC (fn)
13229 2 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
13230 : }
13231 :
13232 : /* Finalize constexpr processing after parsing. */
13233 :
13234 : void
13235 96703 : fini_constexpr (void)
13236 : {
13237 : /* The contexpr call and fundef copies tables are no longer needed. */
13238 96703 : constexpr_call_table = NULL;
13239 96703 : fundef_copies_table = NULL;
13240 96703 : }
13241 :
13242 : #include "gt-cp-constexpr.h"
|