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 192830319 : is_instantiation_of_constexpr (tree fun)
62 : {
63 267422550 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 118721169 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 287095248 : || (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 184238820 : literal_type_p (tree t)
73 : {
74 181050223 : if (SCALAR_TYPE_P (t)
75 83706160 : || VECTOR_TYPE_P (t)
76 83674603 : || TYPE_REF_P (t)
77 253935595 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : return true;
79 64789003 : if (CLASS_TYPE_P (t))
80 : {
81 63142632 : t = complete_type (t);
82 63142632 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 63142632 : return CLASSTYPE_LITERAL_P (t);
84 : }
85 1646371 : if (TREE_CODE (t) == ARRAY_TYPE)
86 1646159 : 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 337305206 : ensure_literal_type_for_constexpr_object (tree decl)
96 : {
97 337305206 : tree type = TREE_TYPE (decl);
98 337305206 : if (VAR_P (decl)
99 127120501 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 88655819 : || var_in_constexpr_fn (decl))
101 395091857 : && !processing_template_decl)
102 : {
103 37676977 : tree stype = strip_array_types (type);
104 37676977 : 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 37676971 : else if (!literal_type_p (type))
108 : {
109 24951 : 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 24898 : else if (cxx_dialect < cxx23)
119 : {
120 18357 : 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 18357 : cp_function_chain->invalid_constexpr = true;
131 : }
132 : }
133 37652020 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 37652020 : && 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 337305206 : 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 1040 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : const char *gmsgid, ...)
156 : {
157 1040 : diagnostics::diagnostic_info diagnostic;
158 1040 : va_list ap;
159 1040 : rich_location richloc (line_table, location);
160 1040 : va_start (ap, gmsgid);
161 1040 : bool ret;
162 1040 : if (!constexpr_fundef_p)
163 : {
164 : /* Report an error that cannot be suppressed. */
165 742 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
166 : diagnostics::kind::error);
167 742 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
168 : }
169 298 : else if (warn_invalid_constexpr)
170 : {
171 163 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
172 163 : (cxx_dialect < cxx23
173 : ? diagnostics::kind::pedwarn
174 : : diagnostics::kind::warning));
175 163 : diagnostic.m_option_id = OPT_Winvalid_constexpr;
176 163 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
177 : }
178 : else
179 : ret = false;
180 1040 : va_end (ap);
181 2080 : return ret;
182 1040 : }
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 773716370 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : const constexpr_fundef *rhs)
202 : {
203 746616236 : 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 736112587 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : {
212 736112587 : return DECL_UID (fundef->decl);
213 : }
214 :
215 : /* Return a previously saved definition of function FUN. */
216 :
217 : constexpr_fundef *
218 104085418 : retrieve_constexpr_fundef (tree fun)
219 : {
220 104085418 : if (constexpr_fundef_table == NULL)
221 : return NULL;
222 :
223 104081417 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 104081417 : 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 31742883 : is_valid_constexpr_fn (tree fun, bool complain)
232 : {
233 31742883 : bool ret = true;
234 :
235 63485766 : if (DECL_INHERITED_CTOR (fun)
236 3682402 : && 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 31742883 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 64080196 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 32337313 : if (!literal_type_p (TREE_TYPE (parm)))
248 : {
249 14194 : ret = false;
250 14194 : if (complain)
251 : {
252 23 : auto_diagnostic_group d;
253 23 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
254 : "invalid type for parameter %d of "
255 : "%<constexpr%> function %q+#D",
256 23 : DECL_PARM_INDEX (parm), fun))
257 15 : explain_non_literal_class (TREE_TYPE (parm));
258 23 : }
259 : }
260 : }
261 :
262 52627061 : 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 63485760 : 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 31742880 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : {
279 27391603 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 27391603 : if (!literal_type_p (rettype))
281 : {
282 51447 : ret = false;
283 51447 : if (complain)
284 : {
285 21 : auto_diagnostic_group d;
286 21 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
287 : "invalid return type %qT of %<constexpr%> "
288 : "function %q+D", rettype, fun))
289 10 : explain_non_literal_class (rettype);
290 21 : }
291 : }
292 :
293 : /* C++14 DR 1684 removed this restriction. */
294 27391603 : if (cxx_dialect < cxx14
295 32941 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 27392724 : && !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 4351277 : 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 31742883 : 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 4477 : 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 4477 : auto_vec<tree, 2> fields;
344 9050 : do
345 : {
346 9050 : fields.safe_push (TREE_OPERAND (member, 1));
347 9050 : member = TREE_OPERAND (member, 0);
348 : }
349 9050 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 18106 : && 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 9050 : while (field = fields.pop(),
357 9050 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : {
359 4573 : tree ctor;
360 : /* If there is already an outer constructor entry for the anonymous
361 : aggregate FIELD, use it; otherwise, insert one. */
362 4573 : if (vec_safe_is_empty (*vec)
363 198 : || (*vec)->last().index != field)
364 : {
365 4471 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 4471 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : }
368 : else
369 102 : ctor = (*vec)->last().value;
370 4573 : 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 4477 : gcc_assert (fields.is_empty());
376 4477 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 :
378 4477 : return true;
379 4477 : }
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 6082828 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : {
390 6787620 : tree member, init;
391 6787620 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 3094817 : t = TREE_OPERAND (t, 0);
393 6787620 : if (TREE_CODE (t) == EXPR_STMT)
394 3093613 : t = TREE_OPERAND (t, 0);
395 6787620 : if (t == error_mark_node)
396 : return false;
397 6787611 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : {
399 7682174 : for (tree stmt : tsi_range (t))
400 899520 : if (! build_data_member_initialization (stmt, vec))
401 9 : return false;
402 : return true;
403 : }
404 6087776 : 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 670998 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : }
413 5416778 : if (TREE_CODE (t) == CONVERT_EXPR)
414 3023932 : t = TREE_OPERAND (t, 0);
415 5416778 : if (TREE_CODE (t) == INIT_EXPR)
416 : {
417 2880115 : member = TREE_OPERAND (t, 0);
418 2880115 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : }
420 2536663 : else if (TREE_CODE (t) == CALL_EXPR)
421 : {
422 2063866 : tree fn = get_callee_fndecl (t);
423 4127732 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : /* We're only interested in calls to subobject constructors. */
425 : return true;
426 1653501 : 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 1653501 : init = break_out_target_exprs (t);
431 : }
432 472797 : else if (TREE_CODE (t) == BIND_EXPR)
433 33794 : 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 4533616 : if (INDIRECT_REF_P (member))
438 61471 : member = TREE_OPERAND (member, 0);
439 4533616 : if (TREE_CODE (member) == NOP_EXPR)
440 : {
441 561038 : tree op = member;
442 561038 : STRIP_NOPS (op);
443 561038 : if (TREE_CODE (op) == ADDR_EXPR)
444 : {
445 656 : 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 560382 : else if (op == current_class_ptr
453 1120613 : && (same_type_ignoring_top_level_qualifiers_p
454 560231 : (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 483299 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : }
464 : }
465 4533616 : if (TREE_CODE (member) == ADDR_EXPR)
466 1154590 : member = TREE_OPERAND (member, 0);
467 4533616 : if (TREE_CODE (member) == COMPONENT_REF)
468 : {
469 3947533 : tree aggr = TREE_OPERAND (member, 0);
470 3947533 : if (TREE_CODE (aggr) == VAR_DECL)
471 : /* Initializing a local variable, don't add anything. */
472 : return true;
473 3947531 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : /* Normal member initialization. */
475 3943051 : member = TREE_OPERAND (member, 1);
476 4480 : else if (VAR_P (get_base_address (aggr)))
477 : /* Initializing a local variable, don't add anything. */
478 : return true;
479 4477 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
480 : /* Initializing a member of an anonymous union. */
481 4477 : 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 5653407 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
491 80 : (*vec)->last().value = init;
492 : else
493 4529054 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
494 : return true;
495 : }
496 :
497 : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
498 : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
499 : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
500 :
501 : static bool
502 2401 : check_constexpr_bind_expr_vars (tree t)
503 : {
504 2401 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
505 :
506 3239 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
507 850 : if (TREE_CODE (var) == TYPE_DECL
508 831 : && DECL_IMPLICIT_TYPEDEF_P (var)
509 872 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
510 : return false;
511 : return true;
512 : }
513 :
514 : /* Subroutine of check_constexpr_ctor_body. */
515 :
516 : static bool
517 4135 : check_constexpr_ctor_body_1 (tree last, tree list)
518 : {
519 4135 : switch (TREE_CODE (list))
520 : {
521 6 : case DECL_EXPR:
522 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
523 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
524 : return true;
525 : return false;
526 :
527 4 : case CLEANUP_POINT_EXPR:
528 4 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
529 4 : /*complain=*/false);
530 :
531 2058 : case BIND_EXPR:
532 2058 : if (!check_constexpr_bind_expr_vars (list)
533 2058 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
534 : /*complain=*/false))
535 43 : return false;
536 : return true;
537 :
538 : case USING_STMT:
539 : case STATIC_ASSERT:
540 : case DEBUG_BEGIN_STMT:
541 : return true;
542 :
543 : default:
544 : return false;
545 : }
546 : }
547 :
548 : /* Make sure that there are no statements after LAST in the constructor
549 : body represented by LIST. */
550 :
551 : bool
552 5994501 : 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 5994501 : if (cxx_dialect >= cxx14)
556 : return true;
557 :
558 13818 : bool ok = true;
559 13818 : if (TREE_CODE (list) == STATEMENT_LIST)
560 : {
561 13791 : tree_stmt_iterator i = tsi_last (list);
562 17829 : for (; !tsi_end_p (i); tsi_prev (&i))
563 : {
564 15702 : tree t = tsi_stmt (i);
565 15702 : if (t == last)
566 : break;
567 4108 : if (!check_constexpr_ctor_body_1 (last, t))
568 : {
569 : ok = false;
570 : break;
571 : }
572 : }
573 : }
574 27 : else if (list != last
575 27 : && !check_constexpr_ctor_body_1 (last, list))
576 : ok = false;
577 13818 : if (!ok && complain)
578 : {
579 49 : pedwarn (input_location, OPT_Wc__14_extensions,
580 : "%<constexpr%> constructor does not have empty body");
581 49 : ok = true;
582 : }
583 : return ok;
584 : }
585 :
586 : /* V is a vector of constructor elements built up for the base and member
587 : initializers of a constructor for TYPE. They need to be in increasing
588 : offset order, which they might not be yet if TYPE has a primary base
589 : which is not first in the base-clause or a vptr and at least one base
590 : all of which are non-primary. */
591 :
592 : static vec<constructor_elt, va_gc> *
593 3604477 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
594 : {
595 3604477 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
596 3604477 : tree field_type;
597 3604477 : unsigned i;
598 3604477 : constructor_elt *ce;
599 :
600 3604477 : if (pri)
601 87966 : field_type = BINFO_TYPE (pri);
602 3516511 : else if (TYPE_CONTAINS_VPTR_P (type))
603 37513 : 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 167122 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
610 129489 : if (TREE_TYPE (ce->index) == field_type)
611 : break;
612 :
613 147691 : if (i > 0 && i < vec_safe_length (v))
614 : {
615 28 : vec<constructor_elt, va_gc> &vref = *v;
616 28 : constructor_elt elt = vref[i];
617 56 : for (; i > 0; --i)
618 28 : vref[i] = vref[i-1];
619 28 : 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 3681517 : build_constexpr_constructor_member_initializers (tree type, tree body)
630 : {
631 3681517 : vec<constructor_elt, va_gc> *vec = NULL;
632 3681517 : bool ok = true;
633 5744876 : while (true)
634 5744876 : switch (TREE_CODE (body))
635 : {
636 2063282 : case MUST_NOT_THROW_EXPR:
637 2063282 : case EH_SPEC_BLOCK:
638 2063282 : case TRY_FINALLY_EXPR: // For C++26 postconditions.
639 2063282 : body = TREE_OPERAND (body, 0);
640 2063282 : break;
641 :
642 77 : case STATEMENT_LIST:
643 5744978 : for (tree stmt : tsi_range (body))
644 : {
645 157 : body = stmt;
646 157 : if (TREE_CODE (body) == BIND_EXPR)
647 : break;
648 : }
649 : break;
650 :
651 3681517 : case BIND_EXPR:
652 3681517 : body = BIND_EXPR_BODY (body);
653 3681517 : goto found;
654 :
655 0 : default:
656 0 : gcc_unreachable ();
657 : }
658 3681517 : found:
659 3681517 : if (TREE_CODE (body) == TRY_BLOCK)
660 : {
661 31 : body = TREE_OPERAND (body, 0);
662 31 : if (TREE_CODE (body) == BIND_EXPR)
663 31 : body = BIND_EXPR_BODY (body);
664 : }
665 3681517 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
666 : {
667 1993933 : body = TREE_OPERAND (body, 0);
668 1993933 : if (TREE_CODE (body) == EXPR_STMT)
669 1993897 : body = TREE_OPERAND (body, 0);
670 1993933 : if (TREE_CODE (body) == INIT_EXPR
671 1993933 : && (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 1993933 : ok = build_data_member_initialization (body, &vec);
679 : }
680 1687584 : else if (TREE_CODE (body) == STATEMENT_LIST)
681 : {
682 4866316 : for (tree stmt : tsi_range (body))
683 : {
684 3184054 : ok = build_data_member_initialization (stmt, &vec);
685 3184054 : if (!ok)
686 : break;
687 : }
688 : }
689 5321 : else if (EXPR_P (body))
690 5321 : ok = build_data_member_initialization (body, &vec);
691 : else
692 0 : gcc_assert (errorcount > 0);
693 3681517 : if (ok)
694 : {
695 3681508 : if (vec_safe_length (vec) > 0)
696 : {
697 : /* In a delegating constructor, return the target. */
698 3409212 : constructor_elt *ce = &(*vec)[0];
699 3409212 : if (ce->index == current_class_ptr)
700 : {
701 77031 : body = ce->value;
702 77031 : vec_free (vec);
703 77031 : return body;
704 : }
705 : }
706 3604477 : vec = sort_constexpr_mem_initializers (type, vec);
707 3604477 : 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 509189141 : get_function_named_in_call (tree t)
719 : {
720 509189141 : tree callee = cp_get_callee (t);
721 509189141 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
722 509189141 : 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 70550 : constexpr_fn_retval (tree body)
732 : {
733 77350 : switch (TREE_CODE (body))
734 : {
735 18773 : case STATEMENT_LIST:
736 18773 : {
737 18773 : tree expr = NULL_TREE;
738 56249 : for (tree stmt : tsi_range (body))
739 : {
740 37505 : tree s = constexpr_fn_retval (stmt);
741 37505 : if (s == error_mark_node)
742 : return error_mark_node;
743 37478 : else if (s == NULL_TREE)
744 : /* Keep iterating. */;
745 18738 : else if (expr)
746 : /* Multiple return statements. */
747 : return error_mark_node;
748 : else
749 : expr = s;
750 : }
751 : return expr;
752 : }
753 :
754 32995 : case RETURN_EXPR:
755 32995 : 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 6463 : case CLEANUP_POINT_EXPR:
768 6463 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
769 :
770 343 : case BIND_EXPR:
771 343 : if (!check_constexpr_bind_expr_vars (body))
772 6 : return error_mark_node;
773 337 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
774 :
775 : case USING_STMT:
776 : case DEBUG_BEGIN_STMT:
777 : return NULL_TREE;
778 :
779 0 : case CALL_EXPR:
780 0 : {
781 0 : tree fun = get_function_named_in_call (body);
782 0 : if (fun != NULL_TREE
783 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
784 : return NULL_TREE;
785 : }
786 : /* Fallthru. */
787 :
788 30 : default:
789 30 : return error_mark_node;
790 : }
791 : }
792 :
793 : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
794 : FUN; do the necessary transformations to turn it into a single expression
795 : that we can store in the hash table. */
796 :
797 : static tree
798 31043598 : massage_constexpr_body (tree fun, tree body)
799 : {
800 62087196 : if (DECL_CONSTRUCTOR_P (fun))
801 3681517 : body = build_constexpr_constructor_member_initializers
802 3681517 : (DECL_CONTEXT (fun), body);
803 27362081 : else if (cxx_dialect < cxx14)
804 : {
805 32810 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
806 1 : body = EH_SPEC_STMTS (body);
807 32810 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
808 21425 : body = TREE_OPERAND (body, 0);
809 32810 : body = constexpr_fn_retval (body);
810 : }
811 31043598 : 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 3337714 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
819 : {
820 : /* We allow uninitialized bases/fields in C++20. */
821 3337714 : if (cxx_dialect >= cxx20)
822 : return false;
823 :
824 13658 : unsigned nelts = 0;
825 :
826 13658 : if (body)
827 : {
828 13654 : if (TREE_CODE (body) != CONSTRUCTOR)
829 : return false;
830 13603 : nelts = CONSTRUCTOR_NELTS (body);
831 : }
832 13607 : tree field = TYPE_FIELDS (ctype);
833 :
834 13607 : if (TREE_CODE (ctype) == UNION_TYPE)
835 : {
836 110 : if (nelts == 0 && next_aggregate_field (field))
837 : {
838 2 : if (complain)
839 2 : error ("%<constexpr%> constructor for union %qT must "
840 : "initialize exactly one non-static data member", ctype);
841 2 : return true;
842 : }
843 108 : return false;
844 : }
845 :
846 : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
847 : need an explicit initialization. */
848 : bool bad = false;
849 28714 : for (unsigned i = 0; i <= nelts; ++i)
850 : {
851 28714 : tree index = NULL_TREE;
852 28714 : if (i < nelts)
853 : {
854 15217 : index = CONSTRUCTOR_ELT (body, i)->index;
855 : /* Skip vptr adjustment represented with a COMPONENT_REF. */
856 15217 : if (TREE_CODE (index) != FIELD_DECL)
857 627 : continue;
858 : }
859 :
860 496454 : for (; field != index; field = DECL_CHAIN (field))
861 : {
862 468373 : tree ftype;
863 468373 : if (TREE_CODE (field) != FIELD_DECL)
864 934929 : continue;
865 1789 : if (DECL_UNNAMED_BIT_FIELD (field))
866 10 : continue;
867 : /* Artificial fields can be ignored unless they're bases. */
868 1779 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
869 6 : continue;
870 1773 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
871 : {
872 : /* Recurse to check the anonymous aggregate member. */
873 8 : bad |= cx_check_missing_mem_inits
874 4 : (TREE_TYPE (field), NULL_TREE, complain);
875 4 : if (bad && !complain)
876 6 : return true;
877 4 : continue;
878 : }
879 1769 : ftype = TREE_TYPE (field);
880 1769 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
881 : /* A flexible array can't be initialized here, so don't complain
882 : that it isn't. */
883 1 : continue;
884 1768 : if (is_empty_field (field))
885 : /* An empty field doesn't need an initializer. */
886 1733 : continue;
887 35 : ftype = strip_array_types (ftype);
888 35 : if (type_has_constexpr_default_constructor (ftype))
889 : {
890 : /* It's OK to skip a member with a trivial constexpr ctor.
891 : A constexpr ctor that isn't trivial should have been
892 : added in by now. */
893 7 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
894 : || errorcount != 0);
895 7 : continue;
896 : }
897 28 : if (!complain)
898 : return true;
899 22 : auto_diagnostic_group d;
900 22 : error ("member %qD must be initialized by mem-initializer "
901 : "in %<constexpr%> constructor", field);
902 22 : inform (DECL_SOURCE_LOCATION (field), "declared here");
903 22 : bad = true;
904 22 : }
905 28081 : if (field == NULL_TREE)
906 : break;
907 :
908 14590 : 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 14590 : 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 appropriate requirements and
924 : enter it in the constexpr function definition table. */
925 :
926 : void
927 167439005 : maybe_save_constexpr_fundef (tree fun)
928 : {
929 167439005 : if (processing_template_decl
930 69246067 : || cp_function_chain->invalid_constexpr
931 236666824 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
932 136395701 : 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 52935801 : bool implicit = false;
937 52935801 : if (flag_implicit_constexpr)
938 : {
939 22 : if (DECL_DELETING_DESTRUCTOR_P (fun)
940 22 : && 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 22 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
946 18 : && DECL_DECLARED_INLINE_P (fun)
947 36 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
948 : implicit = true;
949 : }
950 :
951 52935801 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
952 : return;
953 :
954 31099984 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
955 :
956 31099984 : if (!is_valid_constexpr_fn (fun, complain))
957 : return;
958 :
959 31043518 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
960 31043518 : 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 31043482 : bool potential = potential_rvalue_constant_expression (massaged);
969 31043482 : if (!potential && complain)
970 256 : require_potential_rvalue_constant_expression_fncheck (massaged);
971 :
972 34724976 : if (DECL_CONSTRUCTOR_P (fun) && potential
973 34720936 : && !DECL_DEFAULTED_FN (fun))
974 : {
975 3337658 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
976 : massaged, complain))
977 : potential = false;
978 3337632 : 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 3332835 : massaged = DECL_SAVED_TREE (fun);
983 3332835 : potential = potential_rvalue_constant_expression (massaged);
984 3332835 : if (!potential && complain)
985 18 : require_potential_rvalue_constant_expression_fncheck (massaged);
986 : }
987 : }
988 :
989 31043482 : 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 294 : && warn_invalid_constexpr != 0)
995 : return;
996 :
997 31043315 : if (implicit)
998 : {
999 14 : if (potential)
1000 : {
1001 3 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
1002 3 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
1003 6 : 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 31043304 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1013 31043304 : bool clear_ctx = false;
1014 31043304 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1015 : {
1016 31043304 : clear_ctx = true;
1017 31043304 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1018 : }
1019 31043304 : tree saved_fn = current_function_decl;
1020 31043304 : current_function_decl = fun;
1021 31043304 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1022 31043304 : current_function_decl = saved_fn;
1023 31043304 : if (clear_ctx)
1024 31043304 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1025 31043304 : 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 61007 : entry.result = error_mark_node;
1030 :
1031 31043304 : 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 31081430 : register_constexpr_fundef (const constexpr_fundef &value)
1039 : {
1040 : /* Create the constexpr function table if necessary. */
1041 31081430 : if (constexpr_fundef_table == NULL)
1042 27510 : constexpr_fundef_table
1043 27510 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1044 :
1045 31081430 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1046 31081430 : (const_cast<constexpr_fundef *> (&value), INSERT);
1047 :
1048 31081430 : gcc_assert (*slot == NULL);
1049 31081430 : *slot = ggc_alloc<constexpr_fundef> ();
1050 31081430 : **slot = value;
1051 31081430 : }
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 1037 : explain_invalid_constexpr_fn (tree fun)
1061 : {
1062 1037 : static hash_set<tree> *diagnosed;
1063 1037 : tree body;
1064 :
1065 : /* Don't try to explain a function we already complained about. */
1066 1037 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1067 833 : if (f->language->erroneous)
1068 1037 : 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 988 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1075 : /* Go on. */;
1076 : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1077 952 : else if (!DECL_DEFAULTED_FN (fun)
1078 1204 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1079 915 : && !(flag_implicit_constexpr
1080 9 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1081 9 : && DECL_DECLARED_INLINE_P (fun))
1082 1861 : && !is_instantiation_of_constexpr (fun))
1083 : {
1084 891 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1085 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1086 894 : && decl_defined_p (fun))
1087 3 : inform (DECL_SOURCE_LOCATION (fun),
1088 : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1089 891 : return;
1090 : }
1091 97 : if (diagnosed == NULL)
1092 74 : diagnosed = new hash_set<tree>;
1093 97 : if (diagnosed->add (fun))
1094 : /* Already explained. */
1095 : return;
1096 :
1097 96 : iloc_sentinel ils = input_location;
1098 96 : 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 84 : input_location = DECL_SOURCE_LOCATION (fun);
1103 84 : inform (input_location,
1104 : "%qD is not usable as a %<constexpr%> function because:", fun);
1105 : }
1106 : /* First check the declaration. */
1107 96 : if (is_valid_constexpr_fn (fun, true))
1108 : {
1109 : /* Then if it's OK, the body. */
1110 90 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1111 90 : && DECL_DEFAULTED_FN (fun))
1112 10 : explain_implicit_non_constexpr (fun);
1113 : else
1114 : {
1115 80 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1116 54 : body = fd->body;
1117 : else
1118 26 : body = DECL_SAVED_TREE (fun);
1119 80 : tree massaged = massage_constexpr_body (fun, body);
1120 80 : require_potential_rvalue_constant_expression (massaged);
1121 160 : if (DECL_CONSTRUCTOR_P (fun))
1122 : {
1123 14 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1124 14 : if (cxx_dialect > cxx11)
1125 : /* Also check the body, not just the ctor-initializer. */
1126 12 : require_potential_rvalue_constant_expression (body);
1127 : }
1128 66 : 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 96 : }
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 63459678 : 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 540846418 : constexpr_global_ctx ()
1229 1081692836 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1230 540846418 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1231 540846418 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1232 540846418 : contract_condition_non_const (false), metafns_called (false) {}
1233 :
1234 426086872 : bool is_outside_lifetime (tree t)
1235 : {
1236 426086872 : if (tree *p = values.get (t))
1237 198041807 : if (*p == void_node)
1238 193 : return true;
1239 : return false;
1240 : }
1241 538292242 : tree get_value (tree t)
1242 : {
1243 538292242 : if (tree *p = values.get (t))
1244 289963101 : if (*p != void_node)
1245 282132332 : return *p;
1246 : return NULL_TREE;
1247 : }
1248 118104314 : tree *get_value_ptr (tree t, bool initializing)
1249 : {
1250 118104314 : if (modifiable && !modifiable->contains (t))
1251 : return nullptr;
1252 118104303 : if (tree *p = values.get (t))
1253 : {
1254 118098234 : 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 276519167 : void put_value (tree t, tree v)
1265 : {
1266 276519167 : bool already_in_map = values.put (t, v);
1267 276519167 : if (!already_in_map && modifiable)
1268 30 : modifiable->add (t);
1269 276519167 : }
1270 226348140 : void destroy_value (tree t)
1271 : {
1272 226348140 : if (TREE_CODE (t) == VAR_DECL
1273 226348140 : || TREE_CODE (t) == PARM_DECL
1274 : || TREE_CODE (t) == RESULT_DECL)
1275 226339374 : values.put (t, void_node);
1276 : else
1277 8766 : values.remove (t);
1278 226348140 : }
1279 18398500 : void clear_value (tree t)
1280 : {
1281 36797000 : 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 173486 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1300 : {
1301 173486 : global->modifiable = &set;
1302 173486 : }
1303 173486 : ~modifiable_tracker ()
1304 : {
1305 173516 : for (tree t: set)
1306 30 : global->clear_value (t);
1307 173486 : global->modifiable = nullptr;
1308 173486 : }
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 1376 : cxx_constexpr_quiet_p (const constexpr_ctx *ctx)
1350 : {
1351 1376 : return ctx->quiet;
1352 : }
1353 :
1354 : /* Return ctx->manifestly_const_eval. For use in reflect.cc. */
1355 :
1356 : mce_value
1357 396 : cxx_constexpr_manifestly_const_eval (const constexpr_ctx *ctx)
1358 : {
1359 396 : 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 2120 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1367 : {
1368 2120 : if (ctx->call)
1369 958 : 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 158 : cxx_constexpr_consteval_block (const constexpr_ctx *ctx)
1378 : {
1379 158 : return ctx->global->consteval_block;
1380 : }
1381 :
1382 : /* Predicates for the meaning of *jump_target. */
1383 :
1384 : static bool
1385 106207827 : returns (tree *jump_target)
1386 : {
1387 18801409 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1388 : }
1389 :
1390 : static bool
1391 101043417 : breaks (tree *jump_target)
1392 : {
1393 101043417 : return (*jump_target
1394 101043417 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1395 57 : && LABEL_DECL_BREAK (*jump_target))
1396 1377090 : || TREE_CODE (*jump_target) == BREAK_STMT
1397 1238609 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1398 : }
1399 :
1400 : static bool
1401 136105003 : continues (tree *jump_target)
1402 : {
1403 136105003 : return (*jump_target
1404 136105003 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1405 93 : && LABEL_DECL_CONTINUE (*jump_target))
1406 1255577 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1407 : }
1408 :
1409 : static bool
1410 12615100 : switches (tree *jump_target)
1411 : {
1412 77462 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1413 : }
1414 :
1415 : static bool
1416 1780741935 : 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 200835248 : 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 222332403 : p2280_active_p (const constexpr_ctx *ctx)
1429 : {
1430 30417254 : 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 225969629 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1445 : {
1446 225969629 : 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 225969604 : if (!*non_constant_p
1451 199194447 : && DECL_P (t)
1452 : /* Non-trivial destructors have their lifetimes ended explicitly
1453 : with a clobber, so don't worry about it here. */
1454 199185878 : && (!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 731632 : || TREE_CODE (t) == PARM_DECL)
1461 424425007 : && 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 225969604 : 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 9013980 : uid_sensitive_constexpr_evaluation_p ()
1490 : {
1491 8926920 : if (uid_sensitive_constexpr_evaluation_value)
1492 : {
1493 254511 : ++uid_sensitive_constexpr_evaluation_true_counter;
1494 254502 : 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 80729966 : uid_sensitive_constexpr_evaluation_sentinel
1506 80729966 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1507 80729966 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1508 : {
1509 80729966 : }
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 3829785365 : uid_sensitive_constexpr_evaluation_checker
1516 3829785365 : ::uid_sensitive_constexpr_evaluation_checker ()
1517 3829785365 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1518 : {
1519 3829785365 : }
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 2536096019 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1527 : {
1528 2536096019 : return (uid_sensitive_constexpr_evaluation_value
1529 2536096019 : && 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 236636580 : constexpr_call_hasher::hash (constexpr_call *info)
1542 : {
1543 236636580 : 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 244100443 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1552 : {
1553 244100443 : if (lhs == rhs)
1554 : return true;
1555 244100443 : if (lhs->hash != rhs->hash)
1556 : return false;
1557 27100134 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1558 : return false;
1559 27100134 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1560 : }
1561 :
1562 : /* Initialize the constexpr call table, if needed. */
1563 :
1564 : static void
1565 36062802 : maybe_initialize_constexpr_call_table (void)
1566 : {
1567 36062802 : if (constexpr_call_table == NULL)
1568 18556 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1569 36062802 : }
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 80979100 : get_fundef_copy (constexpr_fundef *fundef)
1591 : {
1592 80979100 : tree copy;
1593 80979100 : bool existed;
1594 80979100 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1595 80979100 : (fundef_copies_table, fundef->decl, &existed, 127));
1596 :
1597 80979100 : 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 7573694 : copy = build_tree_list (fundef->body, fundef->parms);
1603 7573694 : TREE_TYPE (copy) = fundef->result;
1604 : }
1605 73405406 : else if (*slot == NULL_TREE)
1606 : {
1607 4438 : if (uid_sensitive_constexpr_evaluation_p ())
1608 9 : return NULL_TREE;
1609 :
1610 : /* We've already used the function itself, so make a copy. */
1611 4429 : copy = build_tree_list (NULL, NULL);
1612 4429 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1613 4429 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1614 4429 : tree saved_result = DECL_RESULT (fundef->decl);
1615 4429 : tree saved_fn = current_function_decl;
1616 4429 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1617 4429 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1618 4429 : DECL_RESULT (fundef->decl) = fundef->result;
1619 4429 : current_function_decl = fundef->decl;
1620 4429 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1621 4429 : TREE_TYPE (copy));
1622 4429 : current_function_decl = saved_fn;
1623 4429 : DECL_RESULT (fundef->decl) = saved_result;
1624 4429 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1625 4429 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1626 : }
1627 : else
1628 : {
1629 : /* We have a cached function available. */
1630 73400968 : copy = *slot;
1631 73400968 : *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 80979087 : save_fundef_copy (tree fun, tree copy)
1643 : {
1644 80979087 : tree *slot = fundef_copies_table->get (fun);
1645 80979087 : TREE_CHAIN (copy) = *slot;
1646 80979087 : *slot = copy;
1647 80979087 : }
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 396 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1661 : {
1662 396 : tree type = strip_array_types (TREE_TYPE (exc));
1663 396 : if (!CLASS_TYPE_P (type))
1664 : return NULL;
1665 340 : tree std_exception = lookup_qualified_name (std_node, "exception",
1666 : LOOK_want::NORMAL, false);
1667 340 : if (TREE_CODE (std_exception) != TYPE_DECL)
1668 : return NULL;
1669 334 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1670 : return NULL;
1671 334 : base_kind b_kind;
1672 334 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1673 : tf_none);
1674 334 : if (binfo == NULL_TREE || binfo == error_mark_node)
1675 : return NULL;
1676 326 : if (type != TREE_TYPE (exc))
1677 288 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1678 326 : tree call
1679 326 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1680 : tf_none);
1681 326 : if (call == error_mark_node)
1682 : return NULL;
1683 326 : releasing_vec what_args;
1684 326 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1685 326 : if (call == error_mark_node)
1686 : return NULL;
1687 326 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1688 326 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1689 326 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1690 326 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1691 326 : TYPE_SIZE_UNIT (char_type_node))
1692 652 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1693 0 : return NULL;
1694 326 : if (!potential_constant_expression (call))
1695 : return NULL;
1696 326 : bool non_constant_p = false, overflow_p = false;
1697 326 : tree jmp_target = NULL;
1698 326 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1699 : &non_constant_p, &overflow_p,
1700 : &jmp_target);
1701 326 : if (throws (&jmp_target) || non_constant_p)
1702 : return NULL;
1703 326 : if (reduced_constant_expression_p (ptr))
1704 42 : if (const char *msg = c_getstr (ptr))
1705 42 : return xstrdup (msg);
1706 284 : auto_vec <char, 32> v;
1707 11754 : for (unsigned i = 0; i < INT_MAX; ++i)
1708 : {
1709 11754 : tree t = call;
1710 11754 : if (i)
1711 11470 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1712 11754 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1713 11754 : non_constant_p = false;
1714 11754 : overflow_p = false;
1715 11754 : jmp_target = NULL;
1716 11754 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1717 : &non_constant_p, &overflow_p,
1718 : &jmp_target);
1719 11754 : if (throws (&jmp_target)
1720 11754 : || non_constant_p
1721 11754 : || !tree_fits_shwi_p (t2))
1722 0 : return NULL;
1723 11754 : char c = tree_to_shwi (t2);
1724 11754 : v.safe_push (c);
1725 11754 : if (c == '\0')
1726 : break;
1727 : }
1728 568 : return xstrdup (v.address ());
1729 610 : }
1730 :
1731 : /* Diagnose constant expression evaluation encountering call to
1732 : std::terminate due to exception EXC. */
1733 :
1734 : static void
1735 22 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1736 : {
1737 22 : tree type = strip_array_types (TREE_TYPE (exc));
1738 22 : if (char *str = exception_what_str (ctx, exc))
1739 : {
1740 4 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1741 : "%<what()%>: %qs", "std::terminate", type, str);
1742 4 : free (str);
1743 : }
1744 : else
1745 : {
1746 18 : if (type != TREE_TYPE (exc))
1747 18 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1748 18 : bool non_constant_p = false, overflow_p = false;
1749 18 : tree jmp_target = NULL;
1750 18 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1751 : &non_constant_p, &overflow_p,
1752 : &jmp_target);
1753 18 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1754 18 : if (reduced_constant_expression_p (val))
1755 18 : 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 22 : }
1762 :
1763 : /* Diagnose constant expression evaluation encountering call to
1764 : uncaught exception EXC. */
1765 :
1766 : static void
1767 374 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1768 : {
1769 374 : tree type = strip_array_types (TREE_TYPE (exc));
1770 374 : if (char *str = exception_what_str (ctx, exc))
1771 : {
1772 322 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1773 322 : free (str);
1774 : }
1775 : else
1776 : {
1777 52 : if (type != TREE_TYPE (exc))
1778 52 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1779 52 : bool non_constant_p = false, overflow_p = false;
1780 52 : tree jmp_target = NULL;
1781 52 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1782 : &non_constant_p, &overflow_p,
1783 : &jmp_target);
1784 52 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1785 52 : if (reduced_constant_expression_p (val))
1786 50 : error_at (loc, "uncaught exception %qE", val);
1787 : else
1788 2 : error_at (loc, "uncaught exception of type %qT", type);
1789 : }
1790 374 : }
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 27159145 : cxx_cxa_builtin_fn_p (tree fndecl)
1818 : {
1819 27159145 : if (cxx_dialect < cxx26)
1820 : return CXA_NONE;
1821 2744976 : if (DECL_LANGUAGE (fndecl) != lang_c)
1822 : {
1823 2236517 : if (!decl_in_std_namespace_p (fndecl))
1824 : return CXA_NONE;
1825 1008279 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1826 : return STD_RETHROW_EXCEPTION;
1827 : return CXA_NONE;
1828 : }
1829 508459 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1830 : return CXA_NONE;
1831 73743 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1832 : return CXA_ALLOCATE_EXCEPTION;
1833 20795 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1834 : return CXA_FREE_EXCEPTION;
1835 20793 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1836 : return CXA_THROW;
1837 11986 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1838 : return CXA_BEGIN_CATCH;
1839 4499 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1840 : return CXA_END_CATCH;
1841 1032 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1842 : return CXA_RETHROW;
1843 944 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1844 : return CXA_GET_EXCEPTION_PTR;
1845 908 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1846 : return CXA_BAD_CAST;
1847 612 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1848 : return CXA_BAD_TYPEID;
1849 599 : 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 1548 : cxa_check_throw_arg (tree arg, bool free_exc)
1862 : {
1863 1548 : STRIP_NOPS (arg);
1864 1548 : if (TREE_CODE (arg) != ADDR_EXPR)
1865 : return NULL_TREE;
1866 1548 : arg = TREE_OPERAND (arg, 0);
1867 1548 : if (!VAR_P (arg)
1868 1548 : || !DECL_ARTIFICIAL (arg)
1869 1548 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1870 1548 : && DECL_NAME (arg) != heap_identifier)
1871 3096 : || !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 16578 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1882 : tree refcount)
1883 : {
1884 16578 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1885 16578 : DECL_ARTIFICIAL (var) = 1;
1886 16578 : retrofit_lang_decl (var);
1887 16578 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1888 16578 : ctx->global->heap_vars.safe_push (var);
1889 16578 : 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 25542 : 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 25542 : int nargs = call_expr_nargs (call);
1902 25542 : location_t loc = cp_expr_loc_or_input_loc (call);
1903 25542 : tree args[4], arg;
1904 25542 : 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 25542 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1914 5511 : && nargs == 1
1915 5511 : && (arg = CALL_EXPR_ARG (call, 0))
1916 5511 : && TREE_CODE (arg) == CALL_EXPR
1917 5511 : && call_expr_nargs (arg) == 1
1918 31053 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1919 5511 : if (tree fun = get_function_named_in_call (arg))
1920 5511 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1921 : {
1922 5511 : if (ctx->global->caught_exceptions.length () < 2)
1923 : {
1924 2012 : no_caught_exceptions:
1925 2012 : if (!ctx->quiet)
1926 0 : error_at (loc, "%qD called with no caught exceptions pending",
1927 : fndecl);
1928 2012 : *non_constant_p = true;
1929 2012 : 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 3499 : tree handler_type = ctx->global->caught_exceptions.last ();
1942 3499 : if (handler_type && VAR_P (handler_type))
1943 0 : goto no_caught_exceptions;
1944 3499 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1945 3499 : arg = ctx->global->caught_exceptions[idx];
1946 3499 : gcc_assert (VAR_P (arg));
1947 3499 : if (kind == CXA_BEGIN_CATCH)
1948 : {
1949 3471 : ctx->global->caught_exceptions.pop ();
1950 3471 : --ctx->global->uncaught_exceptions;
1951 : }
1952 3499 : if (handler_type == NULL_TREE)
1953 : /* Used for catch (...). Just return void. */
1954 68 : return void_node;
1955 3431 : else if (POINTER_TYPE_P (handler_type))
1956 : {
1957 : /* Used for catch of a pointer. */
1958 66 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1959 66 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1960 : size_zero_node, NULL_TREE, NULL_TREE);
1961 66 : arg = cp_convert (handler_type, arg,
1962 66 : ctx->quiet ? tf_none : tf_warning_or_error);
1963 66 : 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 3365 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1973 3365 : tree exc_ptr_type = build_pointer_type (exc_type);
1974 3365 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
1975 3365 : if (CLASS_TYPE_P (handler_type))
1976 : {
1977 3285 : tree ptr_type = build_pointer_type (handler_type);
1978 3285 : arg = cp_convert (ptr_type, arg,
1979 3285 : ctx->quiet ? tf_none
1980 : : tf_warning_or_error);
1981 3285 : if (arg == error_mark_node)
1982 : {
1983 0 : *non_constant_p = true;
1984 0 : return call;
1985 : }
1986 : }
1987 : }
1988 3431 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
1989 : non_constant_p, overflow_p,
1990 3431 : jump_target);
1991 : }
1992 37762 : for (int i = 0; i < nargs; ++i)
1993 : {
1994 17731 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1995 : vc_prvalue, non_constant_p,
1996 : overflow_p, jump_target);
1997 17731 : if (*non_constant_p)
1998 : return call;
1999 17731 : if (*jump_target)
2000 : return NULL_TREE;
2001 : }
2002 20031 : switch (kind)
2003 : {
2004 13279 : case CXA_ALLOCATE_EXCEPTION:
2005 13279 : if (nargs != 1)
2006 0 : goto invalid_nargs;
2007 13279 : 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 26558 : tree type = build_array_type_nelts (char_type_node,
2017 13279 : tree_to_uhwi (args[0]));
2018 13279 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2019 13279 : ctx->global->put_value (var, NULL_TREE);
2020 13279 : return fold_convert (ptr_type_node, build_address (var));
2021 : }
2022 2 : case CXA_FREE_EXCEPTION:
2023 2 : if (nargs != 1)
2024 0 : goto invalid_nargs;
2025 2 : arg = cxa_check_throw_arg (args[0], true);
2026 2 : 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 2 : DECL_NAME (arg) = heap_deleted_identifier;
2036 2 : ctx->global->destroy_value (arg);
2037 2 : ctx->global->heap_dealloc_count++;
2038 2 : return void_node;
2039 1398 : case CXA_THROW:
2040 1398 : if (nargs != 3)
2041 0 : goto invalid_nargs;
2042 1398 : arg = cxa_check_throw_arg (args[0], false);
2043 1398 : if (arg == NULL_TREE)
2044 0 : goto invalid_ptr;
2045 1398 : DECL_EXCEPTION_REFCOUNT (arg)
2046 1398 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2047 : size_one_node);
2048 1398 : ++ctx->global->uncaught_exceptions;
2049 1398 : *jump_target = arg;
2050 1398 : return void_node;
2051 0 : case CXA_BEGIN_CATCH:
2052 0 : case CXA_GET_EXCEPTION_PTR:
2053 0 : goto invalid_nargs;
2054 3467 : case CXA_END_CATCH:
2055 3467 : if (nargs != 0)
2056 0 : goto invalid_nargs;
2057 3467 : 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 3467 : arg = ctx->global->caught_exceptions.pop ();
2069 3467 : if (arg == NULL_TREE || !VAR_P (arg))
2070 0 : goto no_active_exc;
2071 3467 : free_except:
2072 3533 : DECL_EXCEPTION_REFCOUNT (arg)
2073 3533 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2074 : size_one_node);
2075 3533 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2076 : {
2077 3349 : if (type_build_dtor_call (TREE_TYPE (arg)))
2078 : {
2079 : /* So that we don't complain about out-of-consteval use. */
2080 3197 : temp_override<tree> ovr (current_function_decl);
2081 3197 : if (ctx->call && ctx->call->fundef)
2082 3197 : current_function_decl = ctx->call->fundef->decl;
2083 3197 : tree cleanup
2084 3219 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2085 : : tf_warning_or_error));
2086 3197 : if (cleanup == error_mark_node)
2087 0 : *non_constant_p = true;
2088 3197 : tree jmp_target = NULL_TREE;
2089 3197 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2090 : non_constant_p, overflow_p,
2091 : &jmp_target);
2092 3197 : if (throws (&jmp_target))
2093 0 : *jump_target = jmp_target;
2094 3197 : }
2095 3349 : DECL_NAME (arg) = heap_deleted_identifier;
2096 3349 : ctx->global->destroy_value (arg);
2097 3349 : ctx->global->heap_dealloc_count++;
2098 : }
2099 : }
2100 3533 : return void_node;
2101 82 : case CXA_RETHROW:
2102 82 : if (nargs != 0)
2103 0 : goto invalid_nargs;
2104 82 : unsigned idx;
2105 164 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2106 68 : if (arg == NULL_TREE || !VAR_P (arg))
2107 0 : --idx;
2108 : else
2109 : break;
2110 82 : 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 68 : DECL_EXCEPTION_REFCOUNT (arg)
2119 68 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2120 68 : ++ctx->global->uncaught_exceptions;
2121 68 : *jump_target = arg;
2122 68 : return void_node;
2123 163 : case CXA_BAD_CAST:
2124 163 : case CXA_BAD_TYPEID:
2125 163 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2126 163 : if (nargs != 0)
2127 0 : goto invalid_nargs;
2128 : else
2129 : {
2130 163 : tree name;
2131 163 : switch (kind)
2132 : {
2133 123 : case CXA_BAD_CAST:
2134 123 : name = get_identifier ("bad_cast");
2135 123 : break;
2136 7 : case CXA_BAD_TYPEID:
2137 7 : name = get_identifier ("bad_typeid");
2138 7 : break;
2139 33 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2140 33 : name = get_identifier ("bad_array_new_length");
2141 33 : break;
2142 : default:
2143 : gcc_unreachable ();
2144 : }
2145 163 : tree decl = lookup_qualified_name (std_node, name);
2146 163 : if (TREE_CODE (decl) != TYPE_DECL
2147 135 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2148 298 : || !type_build_ctor_call (TREE_TYPE (decl)))
2149 : {
2150 28 : if (!ctx->quiet)
2151 8 : error_at (loc, "%qD called without %<std::%D%> being defined",
2152 : fndecl, name);
2153 28 : *non_constant_p = true;
2154 28 : return call;
2155 : }
2156 135 : tree type = TREE_TYPE (decl);
2157 135 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2158 135 : tree ctor
2159 135 : = build_special_member_call (var, complete_ctor_identifier,
2160 : NULL, type, LOOKUP_NORMAL,
2161 135 : ctx->quiet ? tf_none
2162 : : tf_warning_or_error);
2163 135 : if (ctor == error_mark_node)
2164 : {
2165 0 : *non_constant_p = true;
2166 0 : return call;
2167 : }
2168 135 : if (TREE_CONSTANT (ctor))
2169 0 : ctx->global->put_value (var, ctor);
2170 : else
2171 : {
2172 135 : ctx->global->put_value (var, NULL_TREE);
2173 135 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2174 : non_constant_p, overflow_p,
2175 : jump_target);
2176 135 : if (*non_constant_p)
2177 : return call;
2178 135 : if (throws (jump_target))
2179 : return NULL_TREE;
2180 : }
2181 135 : ++ctx->global->uncaught_exceptions;
2182 135 : *jump_target = var;
2183 : }
2184 135 : return void_node;
2185 60 : case BUILTIN_UNCAUGHT_EXCEPTIONS:
2186 60 : 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 60 : if (ctx->manifestly_const_eval != mce_true)
2195 : {
2196 34 : *non_constant_p = true;
2197 34 : return call;
2198 : }
2199 26 : return build_int_cst (integer_type_node,
2200 26 : ctx->global->uncaught_exceptions);
2201 1432 : case BUILTIN_CURRENT_EXCEPTION:
2202 1432 : if (nargs != 0)
2203 0 : goto invalid_nargs;
2204 : else
2205 : {
2206 1432 : tree name = get_identifier ("exception_ptr");
2207 1432 : tree decl = lookup_qualified_name (std_node, name);
2208 1432 : tree fld;
2209 1432 : if (TREE_CODE (decl) != TYPE_DECL
2210 1432 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2211 1432 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2212 1432 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2213 1432 : || DECL_ARTIFICIAL (fld)
2214 1432 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2215 1432 : || next_aggregate_field (DECL_CHAIN (fld))
2216 2864 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2217 1432 : 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 2864 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2226 36 : 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 1432 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2237 : {
2238 1390 : *non_constant_p = true;
2239 1390 : return call;
2240 : }
2241 42 : if (arg == NULL_TREE)
2242 6 : arg = build_zero_cst (TREE_TYPE (fld));
2243 : else
2244 : {
2245 36 : DECL_EXCEPTION_REFCOUNT (arg)
2246 36 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2247 : size_one_node);
2248 36 : arg = fold_convert (ptr_type_node, build_address (arg));
2249 : }
2250 42 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2251 : }
2252 40 : case STD_RETHROW_EXCEPTION:
2253 40 : if (nargs != 1)
2254 0 : goto invalid_nargs;
2255 40 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2256 : {
2257 40 : arg = args[0];
2258 40 : STRIP_NOPS (arg);
2259 40 : if (TREE_CODE (arg) == ADDR_EXPR)
2260 : {
2261 40 : args[0]
2262 40 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2263 : vc_prvalue, non_constant_p,
2264 : overflow_p, jump_target);
2265 40 : if (*non_constant_p)
2266 : return call;
2267 40 : if (*jump_target)
2268 : return NULL_TREE;
2269 : }
2270 : }
2271 40 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2272 40 : || 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 40 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2282 40 : if (arg == NULL_TREE)
2283 0 : goto invalid_std_rethrow;
2284 40 : DECL_EXCEPTION_REFCOUNT (arg)
2285 40 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2286 40 : ++ctx->global->uncaught_exceptions;
2287 40 : *jump_target = arg;
2288 40 : return void_node;
2289 108 : case BUILTIN_EH_PTR_ADJUST_REF:
2290 108 : if (nargs != 2)
2291 0 : goto invalid_nargs;
2292 108 : arg = cxa_check_throw_arg (args[0], false);
2293 108 : if (arg == NULL_TREE)
2294 0 : goto invalid_ptr;
2295 108 : if (integer_onep (args[1]))
2296 42 : DECL_EXCEPTION_REFCOUNT (arg)
2297 84 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2298 : size_one_node);
2299 66 : else if (integer_minus_onep (args[1]))
2300 66 : 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 42 : 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 210 : cxx_eval_constexpr_diag (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
2336 : bool *overflow_p, tree *jump_target)
2337 : {
2338 210 : location_t loc = EXPR_LOCATION (t);
2339 210 : if (call_expr_nargs (t) != 3)
2340 : {
2341 12 : if (!ctx->quiet)
2342 12 : error_at (loc, "wrong number of arguments to %qs call",
2343 : "__builtin_constexpr_diag");
2344 12 : *non_constant_p = true;
2345 12 : return t;
2346 : }
2347 : tree args[3];
2348 792 : for (int i = 0; i < 3; ++i)
2349 : {
2350 594 : tree arg = convert_from_reference (CALL_EXPR_ARG (t, i));
2351 594 : arg = cxx_eval_constant_expression (ctx, arg,
2352 : (i == 0
2353 534 : || POINTER_TYPE_P (TREE_TYPE (arg)))
2354 990 : ? vc_prvalue : vc_glvalue,
2355 : non_constant_p, overflow_p,
2356 : jump_target);
2357 594 : if (*jump_target)
2358 : return NULL_TREE;
2359 594 : if (*non_constant_p)
2360 : return t;
2361 594 : args[i] = arg;
2362 : }
2363 396 : if (TREE_CODE (args[0]) != INTEGER_CST
2364 198 : || wi::to_widest (args[0]) < 0
2365 194 : || wi::to_widest (args[0]) > 18
2366 396 : || (wi::to_widest (args[0]) & 15) > 2)
2367 : {
2368 8 : if (!ctx->quiet)
2369 8 : error_at (loc, "first %qs call argument should be 0, 1, 2, 16, 17 or "
2370 : "18", "__builtin_constexpr_diag");
2371 8 : *non_constant_p = true;
2372 8 : return t;
2373 : }
2374 190 : const char *msgs[2] = {};
2375 190 : int lens[3] = {};
2376 1140 : cexpr_str cstrs[2];
2377 478 : diagnostics::kind kind = diagnostics::kind::error;
2378 478 : for (int i = 1; i < 3; ++i)
2379 : {
2380 344 : tree arg = args[i];
2381 344 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2382 : {
2383 206 : tree str = arg;
2384 206 : STRIP_NOPS (str);
2385 206 : if (TREE_CODE (str) == ADDR_EXPR
2386 206 : && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST)
2387 : {
2388 206 : str = TREE_OPERAND (str, 0);
2389 206 : tree eltype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)));
2390 206 : if (eltype == char_type_node
2391 24 : || (i == 2 && eltype == char8_type_node))
2392 344 : arg = str;
2393 : }
2394 : }
2395 344 : cstrs[i - 1].message = arg;
2396 344 : if (!cstrs[i - 1].type_check (loc, i == 2))
2397 : {
2398 40 : *non_constant_p = true;
2399 40 : return t;
2400 : }
2401 304 : if (!cstrs[i - 1].extract (loc, msgs[i - 1], lens[i - 1], ctx,
2402 : non_constant_p, overflow_p, jump_target))
2403 : {
2404 16 : if (*jump_target)
2405 : return NULL_TREE;
2406 16 : *non_constant_p = true;
2407 16 : return t;
2408 : }
2409 : }
2410 134 : if (msgs[0])
2411 : {
2412 726 : for (int i = 0; i < lens[0]; ++i)
2413 596 : if (!ISALNUM (msgs[0][i]) && msgs[0][i] != '_')
2414 : {
2415 4 : if (!ctx->quiet)
2416 4 : error_at (loc, "%qs tag string contains %qc character other than"
2417 : " letters, digits or %<_%>",
2418 : "__builtin_constexpr_diag", msgs[0][i]);
2419 4 : *non_constant_p = true;
2420 4 : return t;
2421 : }
2422 : }
2423 130 : if (ctx->manifestly_const_eval == mce_unknown)
2424 : {
2425 0 : *non_constant_p = true;
2426 0 : return t;
2427 : }
2428 130 : int arg0 = tree_to_uhwi (args[0]);
2429 130 : if (arg0 & 16)
2430 : {
2431 32 : arg0 &= 15;
2432 32 : if (!call_stack.is_empty ())
2433 : {
2434 32 : tree call = call_stack.last ();
2435 32 : if (EXPR_HAS_LOCATION (call))
2436 32 : loc = EXPR_LOCATION (call);
2437 : }
2438 : }
2439 130 : if (arg0 == 0)
2440 : kind = diagnostics::kind::note;
2441 82 : else if (arg0 == 1)
2442 36 : kind = diagnostics::kind::warning;
2443 130 : if (lens[0])
2444 : {
2445 88 : const char *color = "error";
2446 88 : if (kind == diagnostics::kind::note)
2447 : color = "note";
2448 60 : else if (kind == diagnostics::kind::warning)
2449 32 : color = "warning";
2450 88 : 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 42 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s",
2455 : lens[1], msgs[1]);
2456 130 : return void_node;
2457 570 : }
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 16930480 : 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 16930480 : const int nargs = call_expr_nargs (t);
2470 16930480 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2471 16930480 : tree new_call;
2472 16930480 : int i;
2473 :
2474 : /* Don't fold __builtin_constant_p within a constexpr function. */
2475 16930480 : 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 3488866 : if (bi_const_p
2480 3488866 : && ctx->manifestly_const_eval != mce_true
2481 1673106 : && current_function_decl
2482 1671909 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2483 : {
2484 1368133 : *non_constant_p = true;
2485 1368133 : return t;
2486 : }
2487 :
2488 15562347 : if (fndecl_built_in_p (fun, BUILT_IN_FRONTEND))
2489 3980236 : switch (DECL_FE_FUNCTION_CODE (fun))
2490 : {
2491 3962546 : 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 3962546 : if (ctx->manifestly_const_eval == mce_unknown)
2498 : {
2499 3914642 : *non_constant_p = true;
2500 3914642 : return t;
2501 : }
2502 47904 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2503 47904 : boolean_type_node);
2504 :
2505 10917 : case CP_BUILT_IN_SOURCE_LOCATION:
2506 10917 : {
2507 10917 : temp_override<tree> ovr (current_function_decl);
2508 10917 : if (ctx->call && ctx->call->fundef)
2509 2753 : current_function_decl = ctx->call->fundef->decl;
2510 10917 : return fold_builtin_source_location (t);
2511 10917 : }
2512 :
2513 108 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
2514 108 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2515 : fun, non_constant_p, overflow_p,
2516 108 : jump_target);
2517 :
2518 1432 : case CP_BUILT_IN_CURRENT_EXCEPTION:
2519 1432 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_CURRENT_EXCEPTION,
2520 : fun, non_constant_p, overflow_p,
2521 1432 : jump_target);
2522 :
2523 60 : case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
2524 60 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_UNCAUGHT_EXCEPTIONS,
2525 : fun, non_constant_p, overflow_p,
2526 60 : jump_target);
2527 :
2528 210 : case CP_BUILT_IN_CONSTEXPR_DIAG:
2529 210 : return cxx_eval_constexpr_diag (ctx, t, non_constant_p, overflow_p,
2530 210 : jump_target);
2531 :
2532 : default:
2533 : break;
2534 : }
2535 :
2536 11587074 : int strops = 0;
2537 11587074 : int strret = 0;
2538 11587074 : bool bos = false;
2539 11587074 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2540 10877399 : switch (DECL_FUNCTION_CODE (fun))
2541 : {
2542 : case BUILT_IN_STRLEN:
2543 : case BUILT_IN_STRNLEN:
2544 11586947 : strops = 1;
2545 : break;
2546 117799 : case BUILT_IN_MEMCHR:
2547 117799 : case BUILT_IN_STRCHR:
2548 117799 : case BUILT_IN_STRRCHR:
2549 117799 : strops = 1;
2550 117799 : strret = 1;
2551 117799 : break;
2552 77227 : case BUILT_IN_MEMCMP:
2553 77227 : case BUILT_IN_STRCMP:
2554 77227 : strops = 2;
2555 77227 : break;
2556 29784 : case BUILT_IN_STRSTR:
2557 29784 : strops = 2;
2558 29784 : strret = 1;
2559 29784 : break;
2560 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2561 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2562 42 : case BUILT_IN_OBSERVABLE_CHKPT:
2563 : /* These builtins shall be ignored during constant expression
2564 : evaluation. */
2565 42 : return void_node;
2566 85 : case BUILT_IN_UNREACHABLE:
2567 85 : case BUILT_IN_TRAP:
2568 85 : if (!*non_constant_p && !ctx->quiet)
2569 : {
2570 : /* Do not allow__builtin_unreachable in constexpr function.
2571 : The __builtin_unreachable call with BUILTINS_LOCATION
2572 : comes from cp_maybe_instrument_return. */
2573 25 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2574 0 : error ("%<constexpr%> call flows off the end of the function");
2575 : else
2576 25 : error ("%q+E is not a constant expression", t);
2577 : }
2578 85 : *non_constant_p = true;
2579 85 : return t;
2580 2679 : case BUILT_IN_OBJECT_SIZE:
2581 2679 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
2582 2679 : bos = ctx->manifestly_const_eval == mce_true;
2583 2679 : break;
2584 : default:
2585 : break;
2586 : }
2587 :
2588 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2589 : return constant false for a non-constant argument. */
2590 11586947 : constexpr_ctx new_ctx = *ctx;
2591 11586947 : new_ctx.quiet = true;
2592 30988577 : for (i = 0; i < nargs; ++i)
2593 : {
2594 19401632 : tree arg = CALL_EXPR_ARG (t, i);
2595 19401632 : tree oarg = arg;
2596 :
2597 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2598 : expand_builtin doesn't know how to look in the values table. */
2599 19401632 : bool strop = i < strops;
2600 19401632 : if (strop)
2601 : {
2602 469962 : STRIP_NOPS (arg);
2603 469962 : if (TREE_CODE (arg) == ADDR_EXPR)
2604 8007 : arg = TREE_OPERAND (arg, 0);
2605 : else
2606 : strop = false;
2607 : }
2608 :
2609 : /* If builtin_valid_in_constant_expr_p is true,
2610 : potential_constant_expression_1 has not recursed into the arguments
2611 : of the builtin, verify it here. */
2612 19401632 : if (!builtin_valid_in_constant_expr_p (fun)
2613 19401632 : || potential_constant_expression (arg))
2614 : {
2615 19401501 : bool dummy1 = false, dummy2 = false;
2616 19401501 : tree jmp_target = NULL_TREE;
2617 19401501 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2618 : &dummy1, &dummy2, &jmp_target);
2619 19401499 : if (jmp_target)
2620 : {
2621 0 : *jump_target = jmp_target;
2622 0 : return NULL_TREE;
2623 : }
2624 : }
2625 :
2626 19401630 : if (bi_const_p)
2627 : /* For __builtin_constant_p, fold all expressions with constant values
2628 : even if they aren't C++ constant-expressions. */
2629 2120731 : arg = cp_fold_rvalue (arg);
2630 17280899 : else if (strop)
2631 : {
2632 8007 : if (TREE_CODE (arg) == CONSTRUCTOR)
2633 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2634 8007 : if (TREE_CODE (arg) == STRING_CST)
2635 2982 : arg = build_address (arg);
2636 : else
2637 : arg = oarg;
2638 : }
2639 :
2640 19401630 : args[i] = arg;
2641 : }
2642 11586945 : if (bos)
2643 : {
2644 102 : tree arg = args[0];
2645 102 : STRIP_NOPS (arg);
2646 102 : if (TREE_CODE (arg) == ADDR_EXPR)
2647 102 : args[0] = arg;
2648 : }
2649 :
2650 11586945 : bool save_ffbcp = force_folding_builtin_constant_p;
2651 11586945 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2652 11586945 : tree save_cur_fn = current_function_decl;
2653 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2654 11586945 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2655 37 : && ctx->call
2656 11586949 : && ctx->call->fundef)
2657 4 : current_function_decl = ctx->call->fundef->decl;
2658 11586945 : if (fndecl_built_in_p (fun,
2659 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2660 : BUILT_IN_FRONTEND))
2661 : {
2662 336 : location_t loc = EXPR_LOCATION (t);
2663 336 : if (nargs >= 1)
2664 333 : VERIFY_CONSTANT (args[0]);
2665 136 : new_call
2666 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2667 : args);
2668 : }
2669 11586609 : else if (fndecl_built_in_p (fun,
2670 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2671 : BUILT_IN_FRONTEND))
2672 : {
2673 471 : location_t loc = EXPR_LOCATION (t);
2674 471 : if (nargs >= 2)
2675 : {
2676 465 : VERIFY_CONSTANT (args[0]);
2677 237 : VERIFY_CONSTANT (args[1]);
2678 : }
2679 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2680 : }
2681 11586138 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
2682 : BUILT_IN_FRONTEND))
2683 : {
2684 4156 : location_t loc = EXPR_LOCATION (t);
2685 4156 : if (nargs >= 1)
2686 : {
2687 4156 : tree arg = CALL_EXPR_ARG (t, 0);
2688 4156 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2689 : non_constant_p, overflow_p,
2690 : jump_target);
2691 4156 : if (*jump_target)
2692 : return NULL_TREE;
2693 4156 : args[0] = arg;
2694 : }
2695 4156 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
2696 : }
2697 : else
2698 23163964 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2699 11581982 : CALL_EXPR_FN (t), nargs, args);
2700 11586517 : current_function_decl = save_cur_fn;
2701 11586517 : force_folding_builtin_constant_p = save_ffbcp;
2702 11586517 : if (new_call == NULL)
2703 : {
2704 6886929 : if (!*non_constant_p && !ctx->quiet)
2705 : {
2706 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2707 0 : CALL_EXPR_FN (t), nargs, args);
2708 0 : error ("%q+E is not a constant expression", new_call);
2709 : }
2710 6886929 : *non_constant_p = true;
2711 6886929 : return t;
2712 : }
2713 :
2714 4699588 : if (!potential_constant_expression (new_call))
2715 : {
2716 771 : if (!*non_constant_p && !ctx->quiet)
2717 4 : error ("%q+E is not a constant expression", new_call);
2718 771 : *non_constant_p = true;
2719 771 : return t;
2720 : }
2721 :
2722 4698817 : if (strret)
2723 : {
2724 : /* memchr returns a pointer into the first argument, but we replaced the
2725 : argument above with a STRING_CST; put it back it now. */
2726 283 : tree op = CALL_EXPR_ARG (t, strret-1);
2727 283 : STRIP_NOPS (new_call);
2728 283 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2729 142 : TREE_OPERAND (new_call, 0) = op;
2730 141 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2731 4698817 : new_call = op;
2732 : }
2733 :
2734 4698817 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2735 : non_constant_p, overflow_p,
2736 4698817 : jump_target);
2737 : }
2738 :
2739 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2740 : the type of the value to match. */
2741 :
2742 : static tree
2743 127221276 : adjust_temp_type (tree type, tree temp)
2744 : {
2745 127221276 : if (same_type_p (TREE_TYPE (temp), type))
2746 : return temp;
2747 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2748 56966502 : if (TREE_CODE (temp) == CONSTRUCTOR)
2749 : {
2750 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2751 3753507 : tree t = copy_node (temp);
2752 3753507 : TREE_TYPE (t) = type;
2753 3753507 : return t;
2754 : }
2755 53212995 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2756 0 : return build0 (EMPTY_CLASS_EXPR, type);
2757 53212995 : gcc_assert (scalarish_type_p (type));
2758 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2759 : type is cv-unqualified. */
2760 53212995 : return cp_fold_convert (cv_unqualified (type), temp);
2761 : }
2762 :
2763 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2764 : sub-CONSTRUCTORs. Otherwise return T.
2765 :
2766 : We use this whenever we initialize an object as a whole, whether it's a
2767 : parameter, a local variable, or a subobject, so that subsequent
2768 : modifications don't affect other places where it was used. */
2769 :
2770 : tree
2771 117725889 : unshare_constructor (tree t MEM_STAT_DECL)
2772 : {
2773 117725889 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2774 : return t;
2775 17720164 : auto_vec <tree*, 4> ptrs;
2776 17720164 : ptrs.safe_push (&t);
2777 17720164 : while (!ptrs.is_empty ())
2778 : {
2779 23442200 : tree *p = ptrs.pop ();
2780 23442200 : tree n = copy_node (*p PASS_MEM_STAT);
2781 38689096 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2782 23442200 : *p = n;
2783 23442200 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2784 23442200 : constructor_elt *ce;
2785 89812010 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2786 25207446 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2787 5722036 : ptrs.safe_push (&ce->value);
2788 : }
2789 17720164 : return t;
2790 17720164 : }
2791 :
2792 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2793 :
2794 : static void
2795 874206 : free_constructor (tree t)
2796 : {
2797 874206 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2798 0 : return;
2799 874206 : releasing_vec ctors;
2800 874206 : vec_safe_push (ctors, t);
2801 1787745 : while (!ctors->is_empty ())
2802 : {
2803 913539 : tree c = ctors->pop ();
2804 913539 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2805 : {
2806 : constructor_elt *ce;
2807 393152 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2808 254571 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2809 39333 : vec_safe_push (ctors, ce->value);
2810 138581 : ggc_free (elts);
2811 : }
2812 913539 : ggc_free (c);
2813 : }
2814 874206 : }
2815 :
2816 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2817 : if *TP is address of a static variable (or part of it) currently being
2818 : constructed or of a heap artificial variable. */
2819 :
2820 : static tree
2821 46600175 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2822 : {
2823 46600175 : if (TREE_CODE (*tp) == ADDR_EXPR)
2824 8544991 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2825 8544991 : if (VAR_P (var) && TREE_STATIC (var))
2826 : {
2827 5164836 : if (DECL_NAME (var) == heap_uninit_identifier
2828 5164836 : || DECL_NAME (var) == heap_identifier
2829 5164836 : || DECL_NAME (var) == heap_vec_uninit_identifier
2830 10329672 : || DECL_NAME (var) == heap_vec_identifier)
2831 : return var;
2832 :
2833 5164836 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2834 5164836 : if (global->get_value (var))
2835 : return var;
2836 : }
2837 46158306 : if (TYPE_P (*tp))
2838 2491 : *walk_subtrees = false;
2839 : return NULL_TREE;
2840 : }
2841 :
2842 : /* Subroutine of cxx_eval_call_expression.
2843 : We are processing a call expression (either CALL_EXPR or
2844 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2845 : all arguments and bind their values to correspondings
2846 : parameters, making up the NEW_CALL context. */
2847 :
2848 : static tree
2849 165715608 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2850 : tree orig_fun, bool *non_constant_p,
2851 : bool *overflow_p, bool *non_constant_args,
2852 : tree *jump_target)
2853 : {
2854 165715608 : int nargs = call_expr_nargs (t);
2855 165715608 : tree parms = DECL_ARGUMENTS (fun);
2856 165715608 : int i, j = 0;
2857 165715608 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2858 1486 : ++nargs;
2859 165715608 : if (DECL_HAS_VTT_PARM_P (fun)
2860 1488 : && fun != orig_fun
2861 165717094 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2862 1069 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2863 439 : ++nargs;
2864 : /* We don't record ellipsis args below. */
2865 165715608 : int nparms = list_length (parms);
2866 165715608 : int nbinds = nargs < nparms ? nargs : nparms;
2867 165715608 : tree binds = make_tree_vec (nbinds);
2868 :
2869 : /* The call is not a constant expression if it involves the cdtor for a type
2870 : with virtual bases before C++26. */
2871 165715608 : if (cxx_dialect < cxx26
2872 165715608 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2873 : {
2874 17 : if (!ctx->quiet)
2875 : {
2876 3 : error_at (cp_expr_loc_or_input_loc (t),
2877 : "call to non-%<constexpr%> function %qD", fun);
2878 3 : explain_invalid_constexpr_fn (fun);
2879 : }
2880 17 : *non_constant_p = true;
2881 17 : return binds;
2882 : }
2883 :
2884 301542214 : for (i = 0; i < nargs; ++i)
2885 : {
2886 198003358 : tree x, arg;
2887 198003358 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2888 198003358 : if (parms && DECL_BY_REFERENCE (parms))
2889 9858 : type = TREE_TYPE (type);
2890 198003358 : if (i == 1
2891 198003358 : && j == 0
2892 43020554 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2893 198004727 : && orig_fun != fun)
2894 : {
2895 1369 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2896 1369 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2897 419 : x = boolean_true_node;
2898 : else
2899 950 : x = boolean_false_node;
2900 : j = -1;
2901 : }
2902 198001989 : else if (i == 2
2903 198001989 : && j == -1
2904 1369 : && DECL_HAS_VTT_PARM_P (fun)
2905 1369 : && orig_fun != fun
2906 198003358 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2907 972 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2908 : {
2909 419 : x = build_zero_cst (type);
2910 419 : j = -2;
2911 : }
2912 : else
2913 198001570 : x = get_nth_callarg (t, i + j);
2914 : /* For member function, the first argument is a pointer to the implied
2915 : object. For a constructor, it might still be a dummy object, in
2916 : which case we get the real argument from ctx. */
2917 288482352 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2918 217815775 : && is_dummy_object (x))
2919 : {
2920 9198696 : x = ctx->object;
2921 9198696 : x = build_address (x);
2922 : }
2923 198003358 : if (TREE_ADDRESSABLE (type))
2924 : {
2925 : /* Undo convert_for_arg_passing work here. */
2926 16788 : x = convert_from_reference (x);
2927 16788 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2928 : non_constant_p, overflow_p,
2929 : jump_target);
2930 : }
2931 : else
2932 : /* Normally we would strip a TARGET_EXPR in an initialization context
2933 : such as this, but here we do the elision differently: we keep the
2934 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2935 197986570 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2936 : non_constant_p, overflow_p,
2937 : jump_target);
2938 198003358 : if (*jump_target)
2939 : break;
2940 : /* Check we aren't dereferencing a null pointer when calling a non-static
2941 : member function, which is undefined behaviour. */
2942 144241121 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2943 81934423 : && integer_zerop (arg)
2944 : /* But ignore calls from within compiler-generated code, to handle
2945 : cases like lambda function pointer conversion operator thunks
2946 : which pass NULL as the 'this' pointer. */
2947 198132730 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2948 : {
2949 332 : if (!ctx->quiet)
2950 6 : error_at (cp_expr_loc_or_input_loc (x),
2951 : "dereferencing a null pointer");
2952 332 : *non_constant_p = true;
2953 : }
2954 : /* Don't VERIFY_CONSTANT here. */
2955 198003286 : if (*non_constant_p && ctx->quiet)
2956 : break;
2957 : /* Just discard ellipsis args after checking their constantitude. */
2958 135826623 : if (!parms)
2959 294 : continue;
2960 :
2961 135826329 : if (!*non_constant_p)
2962 : {
2963 : /* Make sure the binding has the same type as the parm. But
2964 : only for constant args. */
2965 135825555 : if (TREE_ADDRESSABLE (type))
2966 : {
2967 10275 : if (!same_type_p (type, TREE_TYPE (arg)))
2968 : {
2969 9 : arg = build_fold_addr_expr (arg);
2970 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2971 9 : arg = convert_from_reference (arg);
2972 : }
2973 : }
2974 135815280 : else if (!TYPE_REF_P (type))
2975 97503905 : arg = adjust_temp_type (type, arg);
2976 135825555 : if (!TREE_CONSTANT (arg))
2977 87757858 : *non_constant_args = true;
2978 48067697 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2979 : /* The destructor needs to see any modifications the callee makes
2980 : to the argument. */
2981 0 : *non_constant_args = true;
2982 : /* If arg is or contains address of a heap artificial variable or
2983 : of a static variable being constructed, avoid caching the
2984 : function call, as those variables might be modified by the
2985 : function, or might be modified by the callers in between
2986 : the cached function and just read by the function. */
2987 48067697 : else if (!*non_constant_args
2988 48067697 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2989 : NULL))
2990 441869 : *non_constant_args = true;
2991 :
2992 : /* For virtual calls, adjust the this argument, so that it is
2993 : the object on which the method is called, rather than
2994 : one of its bases. */
2995 135825555 : if (i == 0 && DECL_VIRTUAL_P (fun))
2996 : {
2997 26869 : tree addr = arg;
2998 26869 : STRIP_NOPS (addr);
2999 26869 : if (TREE_CODE (addr) == ADDR_EXPR)
3000 : {
3001 26838 : tree obj = TREE_OPERAND (addr, 0);
3002 26838 : while (TREE_CODE (obj) == COMPONENT_REF
3003 11688 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
3004 38606 : && !same_type_ignoring_top_level_qualifiers_p
3005 11597 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
3006 171 : obj = TREE_OPERAND (obj, 0);
3007 26838 : if (obj != TREE_OPERAND (addr, 0))
3008 168 : arg = build_fold_addr_expr_with_type (obj,
3009 : TREE_TYPE (arg));
3010 : }
3011 : }
3012 135825555 : TREE_VEC_ELT (binds, i) = arg;
3013 : }
3014 135826329 : parms = TREE_CHAIN (parms);
3015 : }
3016 :
3017 : return binds;
3018 : }
3019 :
3020 : static int
3021 103013732 : push_cx_call_context (tree call)
3022 : {
3023 103013732 : ++call_stack_tick;
3024 103013732 : if (!EXPR_HAS_LOCATION (call))
3025 76719 : SET_EXPR_LOCATION (call, input_location);
3026 103013732 : call_stack.safe_push (call);
3027 103013732 : int len = call_stack.length ();
3028 103013732 : if (len > max_constexpr_depth)
3029 30 : return false;
3030 : return len;
3031 : }
3032 :
3033 : static void
3034 103013728 : pop_cx_call_context (void)
3035 : {
3036 103013728 : ++call_stack_tick;
3037 103013728 : call_stack.pop ();
3038 103013728 : }
3039 :
3040 : vec<tree>
3041 251113 : cx_error_context (void)
3042 : {
3043 251113 : vec<tree> r = vNULL;
3044 251113 : if (call_stack_tick != last_cx_error_tick
3045 251113 : && !call_stack.is_empty ())
3046 : r = call_stack;
3047 251113 : last_cx_error_tick = call_stack_tick;
3048 251113 : return r;
3049 : }
3050 :
3051 : /* E is an operand of a failed assertion, fold it either with or without
3052 : constexpr context. */
3053 :
3054 : static tree
3055 797 : fold_operand (tree e, const constexpr_ctx *ctx)
3056 : {
3057 797 : if (ctx)
3058 : {
3059 32 : bool new_non_constant_p = false, new_overflow_p = false;
3060 32 : tree jmp_target = NULL_TREE;
3061 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3062 : &new_non_constant_p,
3063 : &new_overflow_p, &jmp_target);
3064 : }
3065 : else
3066 765 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3067 797 : return e;
3068 : }
3069 :
3070 : /* If we have a condition in conjunctive normal form (CNF), find the first
3071 : failing clause. In other words, given an expression like
3072 :
3073 : true && true && false && true && false
3074 :
3075 : return the first 'false'. EXPR is the expression. */
3076 :
3077 : static tree
3078 332 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3079 : {
3080 437 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3081 : {
3082 : /* First check the left side... */
3083 192 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3084 192 : if (e == NULL_TREE)
3085 : /* ...if we didn't find a false clause, check the right side. */
3086 105 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3087 : return e;
3088 : }
3089 245 : tree e = contextual_conv_bool (expr, tf_none);
3090 245 : e = fold_operand (e, ctx);
3091 245 : if (integer_zerop (e))
3092 : /* This is the failing clause. */
3093 140 : return expr;
3094 : return NULL_TREE;
3095 : }
3096 :
3097 : /* Wrapper for find_failing_clause_r. */
3098 :
3099 : tree
3100 1948 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3101 : {
3102 1948 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3103 140 : if (tree e = find_failing_clause_r (ctx, expr))
3104 1948 : expr = e;
3105 1948 : return expr;
3106 : }
3107 :
3108 : /* Emit additional diagnostics for failing condition BAD.
3109 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3110 : If SHOW_EXPR_P is true, print the condition (because it was
3111 : instantiation-dependent). */
3112 :
3113 : void
3114 1948 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3115 : const constexpr_ctx *ctx /* = nullptr */)
3116 : {
3117 : /* Nobody wants to see the artificial (bool) cast. */
3118 1948 : bad = tree_strip_nop_conversions (bad);
3119 1948 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3120 3 : bad = TREE_OPERAND (bad, 0);
3121 :
3122 1948 : auto_diagnostic_nesting_level sentinel;
3123 :
3124 : /* Actually explain the failure if this is a concept check or a
3125 : requires-expression. */
3126 1948 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3127 322 : diagnose_constraints (cloc, bad, NULL_TREE);
3128 : /* Similarly if this is a standard trait. */
3129 1626 : else if (maybe_diagnose_standard_trait (cloc, bad))
3130 : ;
3131 1274 : else if (COMPARISON_CLASS_P (bad)
3132 1274 : && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))
3133 56 : || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))))
3134 : {
3135 276 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3136 276 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3137 276 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3138 276 : inform (cloc, "the comparison reduces to %qE", cond);
3139 : }
3140 998 : else if (show_expr_p)
3141 750 : inform (cloc, "%qE evaluates to false", bad);
3142 1948 : }
3143 :
3144 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3145 : do it without changing the current evaluation state. If it evaluates to
3146 : false, complain and return false; otherwise, return true. */
3147 :
3148 : static bool
3149 173478 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3150 : location_t loc, bool evaluated,
3151 : bool *non_constant_p, bool *overflow_p)
3152 : {
3153 173478 : if (*non_constant_p)
3154 : return true;
3155 :
3156 173478 : tree eval, jmp_target = NULL_TREE;
3157 173478 : if (!evaluated)
3158 : {
3159 173478 : if (!potential_rvalue_constant_expression (arg))
3160 16 : return true;
3161 :
3162 173462 : constexpr_ctx new_ctx = *ctx;
3163 173462 : new_ctx.quiet = true;
3164 173462 : bool new_non_constant_p = false, new_overflow_p = false;
3165 : /* Avoid modification of existing values. */
3166 173462 : modifiable_tracker ms (new_ctx.global);
3167 173462 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3168 : &new_non_constant_p,
3169 : &new_overflow_p, &jmp_target);
3170 173462 : }
3171 : else
3172 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3173 : non_constant_p,
3174 : overflow_p, &jmp_target);
3175 173462 : if (jmp_target)
3176 : return true;
3177 :
3178 173462 : if (!*non_constant_p && integer_zerop (eval))
3179 : {
3180 42 : if (!ctx->quiet)
3181 : {
3182 : /* See if we can find which clause was failing
3183 : (for logical AND). */
3184 13 : tree bad = find_failing_clause (ctx, arg);
3185 : /* If not, or its location is unusable, fall back to the
3186 : previous location. */
3187 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3188 :
3189 : /* Report the error. */
3190 13 : auto_diagnostic_group d;
3191 13 : error_at (cloc, msg);
3192 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3193 13 : return bad;
3194 13 : }
3195 29 : *non_constant_p = true;
3196 29 : return false;
3197 : }
3198 :
3199 : return true;
3200 : }
3201 :
3202 : /* Evaluate a call T to a GCC internal function when possible and return
3203 : the evaluated result or, under the control of CTX, give an error, set
3204 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3205 :
3206 : static tree
3207 386371 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3208 : value_cat lval,
3209 : bool *non_constant_p, bool *overflow_p,
3210 : tree *jump_target)
3211 : {
3212 386371 : enum tree_code opcode = ERROR_MARK;
3213 :
3214 386371 : switch (CALL_EXPR_IFN (t))
3215 : {
3216 165 : case IFN_UBSAN_NULL:
3217 165 : case IFN_UBSAN_BOUNDS:
3218 165 : case IFN_UBSAN_VPTR:
3219 165 : case IFN_FALLTHROUGH:
3220 165 : return void_node;
3221 :
3222 173478 : case IFN_ASSUME:
3223 173478 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3224 : G_("failed %<assume%> attribute assumption"),
3225 173478 : EXPR_LOCATION (t), /*eval*/false,
3226 : non_constant_p, overflow_p))
3227 : return t;
3228 173449 : return void_node;
3229 :
3230 : case IFN_ADD_OVERFLOW:
3231 : opcode = PLUS_EXPR;
3232 : break;
3233 385 : case IFN_SUB_OVERFLOW:
3234 385 : opcode = MINUS_EXPR;
3235 385 : break;
3236 205248 : case IFN_MUL_OVERFLOW:
3237 205248 : opcode = MULT_EXPR;
3238 205248 : break;
3239 :
3240 74 : case IFN_LAUNDER:
3241 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3242 : vc_prvalue, non_constant_p,
3243 74 : overflow_p, jump_target);
3244 :
3245 0 : case IFN_DEFERRED_INIT:
3246 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3247 :
3248 90 : case IFN_BSWAP:
3249 90 : case IFN_BITREVERSE:
3250 90 : {
3251 90 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3252 : vc_prvalue, non_constant_p,
3253 : overflow_p, jump_target);
3254 90 : if (*jump_target)
3255 : return NULL_TREE;
3256 90 : if (*non_constant_p)
3257 : return t;
3258 60 : location_t loc = cp_expr_loc_or_input_loc (t);
3259 60 : if (TREE_CODE (arg) != INTEGER_CST
3260 60 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg))
3261 60 : || !TYPE_UNSIGNED (TREE_TYPE (arg))
3262 120 : || (CALL_EXPR_IFN (t) == IFN_BSWAP
3263 30 : && (TYPE_PRECISION (TREE_TYPE (arg)) % 8) != 0))
3264 : {
3265 0 : if (!ctx->quiet)
3266 0 : error_at (loc, "call to internal function %qE", t);
3267 0 : *non_constant_p = true;
3268 0 : return t;
3269 : }
3270 60 : return fold_build_builtin_bswapg_bitreverseg (loc, CALL_EXPR_IFN (t),
3271 60 : arg);
3272 : }
3273 :
3274 6436 : case IFN_VEC_CONVERT:
3275 6436 : {
3276 6436 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3277 : vc_prvalue, non_constant_p,
3278 : overflow_p, jump_target);
3279 6436 : if (*jump_target)
3280 : return NULL_TREE;
3281 6436 : if (TREE_CODE (arg) == VECTOR_CST)
3282 6237 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3283 : return r;
3284 : }
3285 : /* FALLTHRU */
3286 :
3287 204 : default:
3288 204 : if (!ctx->quiet)
3289 0 : error_at (cp_expr_loc_or_input_loc (t),
3290 : "call to internal function %qE", t);
3291 204 : *non_constant_p = true;
3292 204 : return t;
3293 : }
3294 :
3295 : /* Evaluate constant arguments using OPCODE and return a complex
3296 : number containing the result and the overflow bit. */
3297 206128 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3298 : non_constant_p, overflow_p,
3299 : jump_target);
3300 206128 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3301 : non_constant_p, overflow_p,
3302 : jump_target);
3303 206128 : if (*jump_target)
3304 : return NULL_TREE;
3305 206128 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3306 : {
3307 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3308 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3309 2 : tree result = fold_binary_loc (loc, opcode, type,
3310 : fold_convert_loc (loc, type, arg0),
3311 : fold_convert_loc (loc, type, arg1));
3312 2 : tree ovf
3313 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3314 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3315 2 : if (TREE_OVERFLOW (result))
3316 0 : TREE_OVERFLOW (result) = 0;
3317 :
3318 2 : return build_complex (TREE_TYPE (t), result, ovf);
3319 : }
3320 :
3321 206126 : *non_constant_p = true;
3322 206126 : return t;
3323 : }
3324 :
3325 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3326 :
3327 : static void
3328 5741653 : clear_no_implicit_zero (tree ctor)
3329 : {
3330 5741653 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3331 : {
3332 1606223 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3333 6769566 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3334 2107071 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3335 699132 : clear_no_implicit_zero (e.value);
3336 : }
3337 5741653 : }
3338 :
3339 : /* Complain about a const object OBJ being modified in a constant expression.
3340 : EXPR is the MODIFY_EXPR expression performing the modification. */
3341 :
3342 : static void
3343 63 : modifying_const_object_error (tree expr, tree obj)
3344 : {
3345 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3346 63 : auto_diagnostic_group d;
3347 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3348 63 : "a constant expression", TREE_OPERAND (expr, 0));
3349 :
3350 : /* Find the underlying object that was declared as const. */
3351 63 : location_t decl_loc = UNKNOWN_LOCATION;
3352 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3353 75 : switch (TREE_CODE (probe))
3354 : {
3355 39 : case BIT_FIELD_REF:
3356 39 : case COMPONENT_REF:
3357 39 : {
3358 39 : tree elt = TREE_OPERAND (probe, 1);
3359 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3360 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3361 39 : probe = TREE_OPERAND (probe, 0);
3362 : }
3363 39 : break;
3364 :
3365 0 : case ARRAY_REF:
3366 0 : case REALPART_EXPR:
3367 0 : case IMAGPART_EXPR:
3368 0 : probe = TREE_OPERAND (probe, 0);
3369 0 : break;
3370 :
3371 36 : default:
3372 36 : decl_loc = location_of (probe);
3373 36 : break;
3374 : }
3375 63 : inform (decl_loc, "originally declared %<const%> here");
3376 63 : }
3377 :
3378 : /* Return true if FNDECL is a replaceable global allocation function that
3379 : should be usable during constant expression evaluation. */
3380 :
3381 : static inline bool
3382 28602557 : cxx_replaceable_global_alloc_fn (tree fndecl)
3383 : {
3384 28602557 : return (cxx_dialect >= cxx20
3385 27084152 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3386 1786366 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3387 30388609 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3388 907938 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3389 : }
3390 :
3391 : /* Return true if FNDECL is a placement new function that should be
3392 : usable during constant expression evaluation of std::construct_at. */
3393 :
3394 : static inline bool
3395 27479577 : cxx_placement_new_fn (tree fndecl)
3396 : {
3397 27479577 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3398 : }
3399 :
3400 : /* Return true if FNDECL is std::construct_at. */
3401 :
3402 : static inline bool
3403 2310261 : is_std_construct_at (tree fndecl)
3404 : {
3405 2310261 : if (!decl_in_std_namespace_p (fndecl))
3406 : return false;
3407 :
3408 2279333 : tree name = DECL_NAME (fndecl);
3409 2279333 : return name && id_equal (name, "construct_at");
3410 : }
3411 :
3412 : /* Overload for the above taking constexpr_call*. */
3413 :
3414 : static inline bool
3415 2063822 : is_std_construct_at (const constexpr_call *call)
3416 : {
3417 2063822 : return (call
3418 1919583 : && call->fundef
3419 3983405 : && is_std_construct_at (call->fundef->decl));
3420 : }
3421 :
3422 : /* True if CTX is an instance of std::NAME class. */
3423 :
3424 : bool
3425 11389492 : is_std_class (tree ctx, const char *name)
3426 : {
3427 11389492 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3428 : return false;
3429 :
3430 11388870 : tree decl = TYPE_MAIN_DECL (ctx);
3431 11388870 : tree dname = DECL_NAME (decl);
3432 11388870 : if (dname == NULL_TREE || !id_equal (dname, name))
3433 : return false;
3434 :
3435 684436 : return decl_in_std_namespace_p (decl);
3436 : }
3437 :
3438 : /* True if CTX is an instance of std::allocator. */
3439 :
3440 : bool
3441 523534 : is_std_allocator (tree ctx)
3442 : {
3443 523534 : return is_std_class (ctx, "allocator");
3444 : }
3445 :
3446 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3447 :
3448 : static bool
3449 560888 : is_std_allocator_allocate (tree fndecl)
3450 : {
3451 560888 : tree name = DECL_NAME (fndecl);
3452 560888 : if (name == NULL_TREE
3453 560888 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3454 : return false;
3455 :
3456 521914 : return is_std_allocator (DECL_CONTEXT (fndecl));
3457 : }
3458 :
3459 : /* Overload for the above taking constexpr_call*. */
3460 :
3461 : static inline bool
3462 381332 : is_std_allocator_allocate (const constexpr_call *call)
3463 : {
3464 381332 : return (call
3465 283031 : && call->fundef
3466 664363 : && is_std_allocator_allocate (call->fundef->decl));
3467 : }
3468 :
3469 : /* Return true if FNDECL is std::source_location::current. */
3470 :
3471 : static inline bool
3472 49774 : is_std_source_location_current (tree fndecl)
3473 : {
3474 49774 : if (!decl_in_std_namespace_p (fndecl))
3475 : return false;
3476 :
3477 33121 : tree name = DECL_NAME (fndecl);
3478 33121 : if (name == NULL_TREE || !id_equal (name, "current"))
3479 : return false;
3480 :
3481 159 : tree ctx = DECL_CONTEXT (fndecl);
3482 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3483 : return false;
3484 :
3485 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3486 159 : return name && id_equal (name, "source_location");
3487 : }
3488 :
3489 : /* Overload for the above taking constexpr_call*. */
3490 :
3491 : static inline bool
3492 59825 : is_std_source_location_current (const constexpr_call *call)
3493 : {
3494 59825 : return (call
3495 49774 : && call->fundef
3496 109599 : && is_std_source_location_current (call->fundef->decl));
3497 : }
3498 :
3499 : /* Return true if FNDECL is __dynamic_cast. */
3500 :
3501 : static inline bool
3502 27165897 : cxx_dynamic_cast_fn_p (tree fndecl)
3503 : {
3504 27165897 : return (cxx_dialect >= cxx20
3505 25647487 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3506 6756 : && CP_DECL_CONTEXT (fndecl) == abi_node
3507 : /* Only consider implementation-detail __dynamic_cast calls that
3508 : correspond to a dynamic_cast, and ignore direct calls to
3509 : abi::__dynamic_cast. */
3510 27172653 : && DECL_ARTIFICIAL (fndecl));
3511 : }
3512 :
3513 : /* Often, we have an expression in the form of address + offset, e.g.
3514 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3515 :
3516 : static tree
3517 5834 : extract_obj_from_addr_offset (tree expr)
3518 : {
3519 5834 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3520 2786 : expr = TREE_OPERAND (expr, 0);
3521 5834 : STRIP_NOPS (expr);
3522 5834 : if (TREE_CODE (expr) == ADDR_EXPR)
3523 5823 : expr = TREE_OPERAND (expr, 0);
3524 5834 : return expr;
3525 : }
3526 :
3527 : /* Given a PATH like
3528 :
3529 : g.D.2181.D.2154.D.2102.D.2093
3530 :
3531 : find a component with type TYPE. Return NULL_TREE if not found, and
3532 : error_mark_node if the component is not accessible. If STOP is non-null,
3533 : this function will return NULL_TREE if STOP is found before TYPE. */
3534 :
3535 : static tree
3536 2831 : get_component_with_type (tree path, tree type, tree stop)
3537 : {
3538 7921 : while (true)
3539 : {
3540 5376 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3541 : /* Found it. */
3542 : return path;
3543 3761 : else if (stop
3544 3761 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3545 : stop)))
3546 : return NULL_TREE;
3547 3716 : else if (TREE_CODE (path) == COMPONENT_REF
3548 3716 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3549 : {
3550 : /* We need to check that the component we're accessing is in fact
3551 : accessible. */
3552 3716 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3553 3716 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3554 1171 : return error_mark_node;
3555 2545 : path = TREE_OPERAND (path, 0);
3556 : }
3557 : else
3558 : return NULL_TREE;
3559 : }
3560 : }
3561 :
3562 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3563 :
3564 : The declaration of __dynamic_cast is:
3565 :
3566 : void* __dynamic_cast (const void* __src_ptr,
3567 : const __class_type_info* __src_type,
3568 : const __class_type_info* __dst_type,
3569 : ptrdiff_t __src2dst);
3570 :
3571 : where src2dst has the following possible values
3572 :
3573 : >-1: src_type is a unique public non-virtual base of dst_type
3574 : dst_ptr + src2dst == src_ptr
3575 : -1: unspecified relationship
3576 : -2: src_type is not a public base of dst_type
3577 : -3: src_type is a multiple public non-virtual base of dst_type */
3578 :
3579 : static tree
3580 3167 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3581 : bool *non_constant_p, bool *overflow_p,
3582 : tree *jump_target)
3583 : {
3584 : /* T will be something like
3585 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3586 : dismantle it. */
3587 3167 : gcc_assert (call_expr_nargs (call) == 4);
3588 3167 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3589 3167 : tree obj = CALL_EXPR_ARG (call, 0);
3590 3167 : tree type = CALL_EXPR_ARG (call, 2);
3591 3167 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3592 3167 : location_t loc = cp_expr_loc_or_input_loc (call);
3593 :
3594 : /* Get the target type of the dynamic_cast. */
3595 3167 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3596 3167 : type = TREE_OPERAND (type, 0);
3597 3167 : type = TREE_TYPE (DECL_NAME (type));
3598 :
3599 : /* TYPE can only be either T* or T&. We can't know which of these it
3600 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3601 : and something like "(T*)(T&)(T*) x" in the second case.
3602 : This is true for the reference cases in C++ < 26 or when exceptions
3603 : aren't enabled, in that case we should diagnose errors. For C++26
3604 : with exceptions we should silently evaluate to null pointer and
3605 : let the callers call __cxa_bad_cast () later to throw an exception. */
3606 3167 : bool fail_for_non_constant_p = false;
3607 12893 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3608 : {
3609 9726 : if (cxx_dialect < cxx26 || !flag_exceptions)
3610 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3611 9726 : obj = TREE_OPERAND (obj, 0);
3612 : }
3613 :
3614 : /* Evaluate the object so that we know its dynamic type. */
3615 3167 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3616 : overflow_p, jump_target);
3617 3167 : if (*non_constant_p)
3618 : return call;
3619 3048 : if (*jump_target)
3620 : return NULL_TREE;
3621 :
3622 : /* For dynamic_cast from classes with virtual bases we can get something
3623 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3624 : d.D.1234 using cxx_fold_indirect_ref. */
3625 3048 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3626 : {
3627 1206 : tree objo = obj;
3628 1206 : STRIP_NOPS (objo);
3629 1206 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3630 : {
3631 1152 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3632 : obj, NULL, jump_target);
3633 1152 : if (objo)
3634 1152 : obj = build_fold_addr_expr (objo);
3635 : }
3636 : }
3637 :
3638 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3639 : but when HINT is > 0, it can also be something like
3640 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3641 3048 : obj = extract_obj_from_addr_offset (obj);
3642 3048 : const tree objtype = TREE_TYPE (obj);
3643 : /* If OBJ doesn't refer to a base field, we're done. */
3644 6023 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3645 3048 : ? TREE_OPERAND (obj, 1) : obj))
3646 3048 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3647 : {
3648 73 : if (fail_for_non_constant_p)
3649 : {
3650 55 : if (!ctx->quiet)
3651 : {
3652 2 : auto_diagnostic_group d;
3653 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3654 2 : inform (loc, "dynamic type %qT of its operand does "
3655 : "not have a base class of type %qT",
3656 : objtype, type);
3657 2 : }
3658 55 : *non_constant_p = true;
3659 : }
3660 73 : return integer_zero_node;
3661 : }
3662 :
3663 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3664 : or in a destructor ... if the operand of the dynamic_cast refers
3665 : to the object under construction or destruction, this object is
3666 : considered to be a most derived object that has the type of the
3667 : constructor or destructor's class. */
3668 2975 : tree vtable = build_vfield_ref (obj, objtype);
3669 2975 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3670 : non_constant_p, overflow_p,
3671 : jump_target);
3672 2975 : if (*non_constant_p)
3673 : return call;
3674 2798 : if (*jump_target)
3675 : return NULL_TREE;
3676 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3677 : so it's possible that we got a null pointer now. */
3678 2798 : if (integer_zerop (vtable))
3679 : {
3680 12 : if (!ctx->quiet)
3681 3 : error_at (loc, "virtual table pointer is used uninitialized");
3682 12 : *non_constant_p = true;
3683 12 : return integer_zero_node;
3684 : }
3685 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3686 2786 : vtable = extract_obj_from_addr_offset (vtable);
3687 2786 : const tree mdtype = DECL_CONTEXT (vtable);
3688 :
3689 : /* Given dynamic_cast<T>(v),
3690 :
3691 : [expr.dynamic.cast] If C is the class type to which T points or refers,
3692 : the runtime check logically executes as follows:
3693 :
3694 : If, in the most derived object pointed (referred) to by v, v points
3695 : (refers) to a public base class subobject of a C object, and if only
3696 : one object of type C is derived from the subobject pointed (referred)
3697 : to by v the result points (refers) to that C object.
3698 :
3699 : In this case, HINT >= 0 or -3. */
3700 2786 : if (hint >= 0 || hint == -3)
3701 : {
3702 : /* Look for a component with type TYPE. */
3703 687 : tree t = get_component_with_type (obj, type, mdtype);
3704 : /* If not accessible, give an error. */
3705 687 : if (t == error_mark_node)
3706 : {
3707 198 : if (fail_for_non_constant_p)
3708 : {
3709 108 : if (!ctx->quiet)
3710 : {
3711 12 : auto_diagnostic_group d;
3712 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3713 12 : inform (loc, "static type %qT of its operand is a "
3714 : "non-public base class of dynamic type %qT",
3715 : objtype, type);
3716 :
3717 12 : }
3718 108 : *non_constant_p = true;
3719 : }
3720 198 : return integer_zero_node;
3721 : }
3722 489 : else if (t)
3723 : /* The result points to the TYPE object. */
3724 444 : return cp_build_addr_expr (t, complain);
3725 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3726 : Fall through to the normal processing. */
3727 : }
3728 :
3729 : /* Otherwise, if v points (refers) to a public base class subobject of the
3730 : most derived object, and the type of the most derived object has a base
3731 : class, of type C, that is unambiguous and public, the result points
3732 : (refers) to the C subobject of the most derived object.
3733 :
3734 : But it can also be an invalid case. */
3735 :
3736 : /* Get the most derived object. */
3737 2144 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3738 2144 : if (obj == error_mark_node)
3739 : {
3740 973 : if (fail_for_non_constant_p)
3741 : {
3742 350 : if (!ctx->quiet)
3743 : {
3744 38 : auto_diagnostic_group d;
3745 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3746 38 : inform (loc, "static type %qT of its operand is a non-public"
3747 : " base class of dynamic type %qT", objtype, mdtype);
3748 38 : }
3749 350 : *non_constant_p = true;
3750 : }
3751 973 : return integer_zero_node;
3752 : }
3753 : else
3754 1171 : gcc_assert (obj);
3755 :
3756 : /* Check that the type of the most derived object has a base class
3757 : of type TYPE that is unambiguous and public. */
3758 1171 : base_kind b_kind;
3759 1171 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3760 1171 : if (!binfo || binfo == error_mark_node)
3761 : {
3762 441 : if (fail_for_non_constant_p)
3763 : {
3764 134 : if (!ctx->quiet)
3765 : {
3766 16 : auto_diagnostic_group d;
3767 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3768 16 : if (b_kind == bk_ambig)
3769 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3770 : "type %qT of its operand", type, mdtype);
3771 : else
3772 10 : inform (loc, "dynamic type %qT of its operand does not "
3773 : "have an unambiguous public base class %qT",
3774 : mdtype, type);
3775 16 : }
3776 134 : *non_constant_p = true;
3777 : }
3778 441 : return integer_zero_node;
3779 : }
3780 : /* If so, return the TYPE subobject of the most derived object. */
3781 730 : obj = convert_to_base_statically (obj, binfo);
3782 730 : return cp_build_addr_expr (obj, complain);
3783 : }
3784 :
3785 : /* Data structure used by replace_decl and replace_decl_r. */
3786 :
3787 : struct replace_decl_data
3788 : {
3789 : /* The _DECL we want to replace. */
3790 : tree decl;
3791 : /* The replacement for DECL. */
3792 : tree replacement;
3793 : /* Trees we've visited. */
3794 : hash_set<tree> *pset;
3795 : /* Whether we've performed any replacements. */
3796 : bool changed;
3797 : };
3798 :
3799 : /* Helper function for replace_decl, called through cp_walk_tree. */
3800 :
3801 : static tree
3802 15535876 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3803 : {
3804 15535876 : replace_decl_data *d = (replace_decl_data *) data;
3805 :
3806 : /* We could be replacing
3807 : &<retval>.bar -> &foo.bar
3808 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3809 : on the ADDR_EXPR around it. */
3810 15535876 : if (TREE_CODE (*tp) == ADDR_EXPR)
3811 : {
3812 1347130 : d->pset->add (*tp);
3813 1347130 : auto save_changed = d->changed;
3814 1347130 : d->changed = false;
3815 1347130 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3816 1347130 : if (d->changed)
3817 : {
3818 1730 : cxx_mark_addressable (*tp);
3819 1730 : recompute_tree_invariant_for_addr_expr (*tp);
3820 : }
3821 : else
3822 1345400 : d->changed = save_changed;
3823 1347130 : *walk_subtrees = 0;
3824 : }
3825 14188746 : else if (*tp == d->decl)
3826 : {
3827 1966 : *tp = unshare_expr (d->replacement);
3828 1966 : d->changed = true;
3829 1966 : *walk_subtrees = 0;
3830 : }
3831 14186780 : else if (TYPE_P (*tp)
3832 14186780 : || d->pset->add (*tp))
3833 2774767 : *walk_subtrees = 0;
3834 :
3835 15535876 : return NULL_TREE;
3836 : }
3837 :
3838 : /* Replace every occurrence of DECL with (an unshared copy of)
3839 : REPLACEMENT within the expression *TP. Returns true iff a
3840 : replacement was performed. */
3841 :
3842 : bool
3843 2105714 : replace_decl (tree *tp, tree decl, tree replacement)
3844 : {
3845 2105714 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3846 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3847 2105714 : hash_set<tree> pset;
3848 2105714 : replace_decl_data data = { decl, replacement, &pset, false };
3849 2105714 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3850 2105714 : return data.changed;
3851 2105714 : }
3852 :
3853 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3854 :
3855 : static tree
3856 30 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3857 : value_cat lval,
3858 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3859 : {
3860 30 : tree function = THUNK_TARGET (thunk_fndecl);
3861 :
3862 30 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3863 : {
3864 13 : if (!ctx->quiet)
3865 : {
3866 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3867 : {
3868 0 : error ("call to non-%<constexpr%> function %qD", function);
3869 0 : explain_invalid_constexpr_fn (function);
3870 : }
3871 : else
3872 : /* virtual_offset is only set for virtual bases, which make the
3873 : class non-literal, so we don't need to handle it here. */
3874 3 : error ("calling constexpr member function %qD through virtual "
3875 : "base subobject", function);
3876 : }
3877 13 : *non_constant_p = true;
3878 13 : return t;
3879 : }
3880 :
3881 17 : tree new_call = copy_node (t);
3882 17 : CALL_EXPR_FN (new_call) = function;
3883 17 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3884 :
3885 17 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3886 :
3887 17 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3888 : {
3889 : /* 'this'-adjusting thunk. */
3890 11 : tree this_arg = CALL_EXPR_ARG (t, 0);
3891 11 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3892 : this_arg, offset);
3893 11 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3894 : }
3895 : else
3896 : /* Return-adjusting thunk. */
3897 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3898 : new_call, offset);
3899 :
3900 17 : return cxx_eval_constant_expression (ctx, new_call, lval,
3901 : non_constant_p, overflow_p,
3902 17 : jump_target);
3903 : }
3904 :
3905 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3906 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3907 : 'tors to detect modifying const objects in a constexpr context. */
3908 :
3909 : static void
3910 10315487 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3911 : bool readonly_p, bool *non_constant_p,
3912 : bool *overflow_p, tree *jump_target)
3913 : {
3914 20630974 : if (CLASS_TYPE_P (TREE_TYPE (object))
3915 20630974 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3916 : {
3917 : /* Subobjects might not be stored in ctx->global->values but we
3918 : can get its CONSTRUCTOR by evaluating *this. */
3919 1033905 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3920 : non_constant_p, overflow_p,
3921 : jump_target);
3922 1033905 : if (!*non_constant_p
3923 11308570 : && !throws (jump_target)
3924 2026988 : && TREE_CODE (e) == CONSTRUCTOR)
3925 993083 : TREE_READONLY (e) = readonly_p;
3926 : }
3927 10315487 : }
3928 :
3929 : /* Allocate an exception for OBJECT and throw it. */
3930 :
3931 : tree
3932 3164 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
3933 : tree object)
3934 : {
3935 3164 : tree type = TREE_TYPE (object);
3936 : /* This simulates a call to __cxa_allocate_exception. We need
3937 : (struct exception *) &heap -- memory on the heap so that
3938 : it can survive the stack being unwound. */
3939 3164 : tree arr = build_array_of_n_type (type, 1);
3940 3164 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
3941 3164 : DECL_NAME (var) = heap_identifier;
3942 3164 : ctx->global->put_value (var, NULL_TREE);
3943 :
3944 : /* *(struct exception *) &heap = exc{ ... } */
3945 3164 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
3946 3164 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
3947 3164 : bool non_constant_p = false, overflow_p = false;
3948 3164 : tree jump_target = NULL_TREE;
3949 3164 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
3950 : &overflow_p, &jump_target);
3951 3164 : if (non_constant_p)
3952 : {
3953 0 : if (!ctx->quiet)
3954 0 : error_at (loc, "couldn%'t throw %qT", type);
3955 0 : return NULL_TREE;
3956 : }
3957 :
3958 : /* Now we can __cxa_throw. */
3959 3164 : DECL_EXCEPTION_REFCOUNT (var)
3960 3164 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
3961 3164 : ++ctx->global->uncaught_exceptions;
3962 :
3963 3164 : return var;
3964 : }
3965 :
3966 : /* Subroutine of cxx_eval_constant_expression.
3967 : Evaluate the call expression tree T in the context of OLD_CALL expression
3968 : evaluation. */
3969 :
3970 : static tree
3971 183993615 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3972 : value_cat lval,
3973 : bool *non_constant_p, bool *overflow_p,
3974 : tree *jump_target)
3975 : {
3976 183993615 : location_t loc = cp_expr_loc_or_input_loc (t);
3977 183993615 : tree fun = get_function_named_in_call (t);
3978 :
3979 183993615 : if (fun == NULL_TREE)
3980 386371 : return cxx_eval_internal_function (ctx, t, lval,
3981 : non_constant_p, overflow_p,
3982 386371 : jump_target);
3983 :
3984 183607244 : if (TREE_CODE (fun) != FUNCTION_DECL)
3985 : {
3986 : /* Might be a constexpr function pointer. */
3987 241179 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3988 : non_constant_p, overflow_p,
3989 : jump_target);
3990 241179 : if (*jump_target)
3991 : return NULL_TREE;
3992 241179 : STRIP_NOPS (fun);
3993 241179 : if (TREE_CODE (fun) == ADDR_EXPR)
3994 31076 : fun = TREE_OPERAND (fun, 0);
3995 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3996 : indirection, the called expression is a pointer into the
3997 : virtual table which should contain FDESC_EXPR. Extract the
3998 : FUNCTION_DECL from there. */
3999 : else if (TARGET_VTABLE_USES_DESCRIPTORS
4000 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
4001 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
4002 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
4003 : {
4004 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
4005 : if (VAR_P (d)
4006 : && DECL_VTABLE_OR_VTT_P (d)
4007 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
4008 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
4009 : && DECL_INITIAL (d)
4010 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
4011 : {
4012 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
4013 : TYPE_SIZE_UNIT (vtable_entry_type));
4014 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
4015 : if (idx >= 0)
4016 : {
4017 : tree fdesc
4018 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
4019 : if (TREE_CODE (fdesc) == FDESC_EXPR
4020 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
4021 : fun = TREE_OPERAND (fdesc, 0);
4022 : }
4023 : }
4024 : }
4025 : }
4026 183607244 : if (TREE_CODE (fun) != FUNCTION_DECL)
4027 : {
4028 210103 : if (!ctx->quiet && !*non_constant_p)
4029 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
4030 : "function", fun);
4031 210103 : *non_constant_p = true;
4032 210103 : return t;
4033 : }
4034 183397141 : tree orig_fun = fun;
4035 183397141 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
4036 21252904 : fun = DECL_CLONED_FUNCTION (fun);
4037 :
4038 183397141 : if (is_ubsan_builtin_p (fun))
4039 58 : return void_node;
4040 :
4041 183397083 : if (fndecl_built_in_p (fun))
4042 16930480 : return cxx_eval_builtin_function_call (ctx, t, fun,
4043 : lval, non_constant_p, overflow_p,
4044 16930478 : jump_target);
4045 166466603 : if (DECL_THUNK_P (fun))
4046 30 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
4047 30 : jump_target);
4048 166466573 : if (metafunction_p (fun))
4049 : {
4050 : /* To be able to evaluate a metafunction, we may have to instantiate
4051 : constexpr functions. If we're not allowed to instantiate, leave
4052 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4053 : otherwise we might fold those prematurely. See
4054 : g++.dg/reflect/p2996-17.C. */
4055 82622 : if (uid_sensitive_constexpr_evaluation_p ()
4056 82060 : || ctx->manifestly_const_eval == mce_unknown)
4057 : {
4058 20730 : *non_constant_p = true;
4059 20730 : return t;
4060 : }
4061 61892 : ctx->global->metafns_called = true;
4062 61892 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4063 : jump_target);
4064 61892 : if (*jump_target)
4065 : return NULL_TREE;
4066 58716 : if (*non_constant_p)
4067 : return t;
4068 57648 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4069 : non_constant_p, overflow_p,
4070 : jump_target);
4071 57648 : if (*jump_target)
4072 : return NULL_TREE;
4073 57648 : if (*non_constant_p)
4074 : return t;
4075 : return e;
4076 : }
4077 166383951 : bool non_constexpr_call = false;
4078 166383951 : if (!maybe_constexpr_fn (fun))
4079 : {
4080 695776 : if (TREE_CODE (t) == CALL_EXPR
4081 693353 : && cxx_replaceable_global_alloc_fn (fun)
4082 1164255 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4083 242940 : || is_std_allocator_allocate (ctx->call)))
4084 : {
4085 380229 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4086 380229 : const int nargs = call_expr_nargs (t);
4087 380229 : tree arg0 = NULL_TREE;
4088 653761 : for (int i = 0; i < nargs; ++i)
4089 : {
4090 381198 : tree arg = CALL_EXPR_ARG (t, i);
4091 381198 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4092 : non_constant_p, overflow_p,
4093 : jump_target);
4094 381198 : if (*jump_target)
4095 : return NULL_TREE;
4096 : /* Deleting a non-constant pointer has a better error message
4097 : below. */
4098 381188 : if (new_op_p || i != 0)
4099 304092 : VERIFY_CONSTANT (arg);
4100 196436 : if (i == 0)
4101 : arg0 = arg;
4102 : }
4103 272563 : gcc_assert (arg0);
4104 272563 : if (new_op_p)
4105 : {
4106 195467 : if (!tree_fits_uhwi_p (arg0))
4107 : {
4108 : /* We should not get here; the VERIFY_CONSTANT above
4109 : should have already caught it. */
4110 0 : gcc_checking_assert (false);
4111 : if (!ctx->quiet)
4112 : error_at (loc, "cannot allocate array: size not constant");
4113 : *non_constant_p = true;
4114 : return t;
4115 : }
4116 390934 : tree type = build_array_type_nelts (char_type_node,
4117 195467 : tree_to_uhwi (arg0));
4118 195467 : tree var = build_decl (loc, VAR_DECL,
4119 195467 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4120 : & OVL_OP_FLAG_VEC)
4121 : ? heap_vec_uninit_identifier
4122 : : heap_uninit_identifier,
4123 195467 : type);
4124 195467 : DECL_ARTIFICIAL (var) = 1;
4125 195467 : ctx->global->heap_vars.safe_push (var);
4126 195467 : ctx->global->put_value (var, NULL_TREE);
4127 195467 : return fold_convert (ptr_type_node, build_address (var));
4128 : }
4129 : else
4130 : {
4131 77096 : STRIP_NOPS (arg0);
4132 77096 : if (TREE_CODE (arg0) == ADDR_EXPR
4133 77096 : && VAR_P (TREE_OPERAND (arg0, 0)))
4134 : {
4135 77096 : tree var = TREE_OPERAND (arg0, 0);
4136 77096 : if (DECL_NAME (var) == heap_uninit_identifier
4137 77096 : || DECL_NAME (var) == heap_identifier)
4138 : {
4139 76922 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4140 : & OVL_OP_FLAG_VEC)
4141 : {
4142 9 : if (!ctx->quiet)
4143 : {
4144 3 : auto_diagnostic_group d;
4145 3 : error_at (loc, "array deallocation of object "
4146 : "allocated with non-array "
4147 : "allocation");
4148 3 : inform (DECL_SOURCE_LOCATION (var),
4149 : "allocation performed here");
4150 3 : }
4151 9 : *non_constant_p = true;
4152 9 : return t;
4153 : }
4154 76913 : DECL_NAME (var) = heap_deleted_identifier;
4155 76913 : ctx->global->destroy_value (var);
4156 76913 : ctx->global->heap_dealloc_count++;
4157 76913 : return void_node;
4158 : }
4159 174 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4160 174 : || DECL_NAME (var) == heap_vec_identifier)
4161 : {
4162 150 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4163 : & OVL_OP_FLAG_VEC) == 0)
4164 : {
4165 9 : if (!ctx->quiet)
4166 : {
4167 3 : auto_diagnostic_group d;
4168 3 : error_at (loc, "non-array deallocation of "
4169 : "object allocated with array "
4170 : "allocation");
4171 3 : inform (DECL_SOURCE_LOCATION (var),
4172 : "allocation performed here");
4173 3 : }
4174 9 : *non_constant_p = true;
4175 9 : return t;
4176 : }
4177 141 : DECL_NAME (var) = heap_deleted_identifier;
4178 141 : ctx->global->destroy_value (var);
4179 141 : ctx->global->heap_dealloc_count++;
4180 141 : return void_node;
4181 : }
4182 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4183 : {
4184 15 : if (!ctx->quiet)
4185 6 : error_at (loc, "deallocation of already deallocated "
4186 : "storage");
4187 15 : *non_constant_p = true;
4188 15 : return t;
4189 : }
4190 : }
4191 9 : if (!ctx->quiet)
4192 3 : error_at (loc, "deallocation of storage that was "
4193 : "not previously allocated");
4194 9 : *non_constant_p = true;
4195 9 : return t;
4196 : }
4197 : }
4198 : /* Allow placement new in std::construct_at, just return the second
4199 : argument. */
4200 315547 : if (TREE_CODE (t) == CALL_EXPR
4201 313124 : && cxx_placement_new_fn (fun)
4202 494665 : && is_std_construct_at (ctx->call))
4203 : {
4204 44891 : const int nargs = call_expr_nargs (t);
4205 44891 : tree arg1 = NULL_TREE;
4206 134673 : for (int i = 0; i < nargs; ++i)
4207 : {
4208 89782 : tree arg = CALL_EXPR_ARG (t, i);
4209 89782 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4210 : non_constant_p, overflow_p,
4211 : jump_target);
4212 89782 : if (*jump_target)
4213 : return NULL_TREE;
4214 89782 : if (i == 1)
4215 : arg1 = arg;
4216 : else
4217 89782 : VERIFY_CONSTANT (arg);
4218 : }
4219 44891 : gcc_assert (arg1);
4220 : return arg1;
4221 : }
4222 270656 : else if (cxx_dynamic_cast_fn_p (fun))
4223 3167 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4224 3167 : jump_target);
4225 267489 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4226 23942 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4227 : non_constant_p, overflow_p,
4228 23942 : jump_target);
4229 :
4230 : /* Calls to non-constexpr functions can be diagnosed right away
4231 : before C++26, though in C++26 evaluation of the arguments might
4232 : throw and if caught it could be still constant expression.
4233 : So for C++26 this is diagnosed only after
4234 : cxx_bind_parameters_in_call. */
4235 243547 : if (cxx_dialect >= cxx26)
4236 : non_constexpr_call = true;
4237 : else
4238 : {
4239 216114 : if (!ctx->quiet)
4240 : {
4241 195 : if (!lambda_static_thunk_p (fun))
4242 195 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4243 195 : explain_invalid_constexpr_fn (fun);
4244 : }
4245 216114 : *non_constant_p = true;
4246 216114 : return t;
4247 : }
4248 : }
4249 :
4250 165715608 : constexpr_ctx new_ctx = *ctx;
4251 165715608 : ctx = &new_ctx;
4252 185528042 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4253 169191556 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4254 : {
4255 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4256 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4257 2794 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4258 2794 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4259 2794 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4260 2794 : ctx->global->put_value (new_ctx.object, ctor);
4261 : }
4262 : /* An immediate invocation is manifestly constant evaluated including the
4263 : arguments of the call, so use mce_true even for the argument
4264 : evaluation. */
4265 331431216 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4266 4777251 : new_ctx.manifestly_const_eval = mce_true;
4267 :
4268 : /* We used to shortcut trivial constructor/op= here, but nowadays
4269 : we can only get a trivial function here with -fno-elide-constructors. */
4270 165715744 : gcc_checking_assert (!trivial_fn_p (fun)
4271 : || !flag_elide_constructors
4272 : /* Or it's a call from maybe_thunk_body (111075). */
4273 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4274 : : AGGR_INIT_FROM_THUNK_P (t))
4275 : /* We don't elide constructors when processing
4276 : a noexcept-expression. */
4277 : || cp_noexcept_operand);
4278 :
4279 165715608 : bool non_constant_args = false;
4280 165715608 : constexpr_call new_call;
4281 165715608 : new_call.bindings
4282 165715608 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4283 : overflow_p, &non_constant_args,
4284 : jump_target);
4285 :
4286 : /* We build up the bindings list before we know whether we already have this
4287 : call cached. If we don't end up saving these bindings, ggc_free them when
4288 : this function exits. */
4289 165715608 : class free_bindings
4290 : {
4291 : tree *bindings;
4292 : public:
4293 165715608 : free_bindings (tree &b): bindings (&b) { }
4294 165715604 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4295 4629787 : void preserve () { bindings = NULL; }
4296 165715608 : } fb (new_call.bindings);
4297 :
4298 165715608 : if (*jump_target)
4299 : return NULL_TREE;
4300 165715536 : if (*non_constant_p)
4301 : return t;
4302 103538105 : if (non_constexpr_call)
4303 : {
4304 3871 : if (!ctx->quiet)
4305 : {
4306 423 : if (!lambda_static_thunk_p (fun))
4307 423 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4308 423 : explain_invalid_constexpr_fn (fun);
4309 : }
4310 3871 : *non_constant_p = true;
4311 3871 : return t;
4312 : }
4313 :
4314 : /* We can't defer instantiating the function any longer. */
4315 103534234 : if (!DECL_INITIAL (fun)
4316 3134821 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4317 103534234 : && !uid_sensitive_constexpr_evaluation_p ())
4318 : {
4319 2881528 : location_t save_loc = input_location;
4320 2881528 : input_location = loc;
4321 2881528 : ++function_depth;
4322 2881528 : if (ctx->manifestly_const_eval == mce_true)
4323 381096 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4324 2881528 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4325 2881514 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4326 : else
4327 14 : synthesize_method (fun);
4328 2881528 : --function_depth;
4329 2881528 : input_location = save_loc;
4330 : }
4331 :
4332 : /* If in direct recursive call, optimize definition search. */
4333 103534234 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4334 27735 : new_call.fundef = ctx->call->fundef;
4335 : else
4336 : {
4337 103506499 : new_call.fundef = retrieve_constexpr_fundef (fun);
4338 103506499 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4339 103000119 : || new_call.fundef->result == error_mark_node
4340 102986000 : || fun == current_function_decl)
4341 : {
4342 520502 : if (!ctx->quiet)
4343 : {
4344 : /* We need to check for current_function_decl here in case we're
4345 : being called during cp_fold_function, because at that point
4346 : DECL_INITIAL is set properly and we have a fundef but we
4347 : haven't lowered invisirefs yet (c++/70344). */
4348 187 : if (DECL_INITIAL (fun) == error_mark_node
4349 187 : || fun == current_function_decl)
4350 15 : error_at (loc, "%qD called in a constant expression before its "
4351 : "definition is complete", fun);
4352 172 : else if (DECL_INITIAL (fun))
4353 : {
4354 : /* The definition of fun was somehow unsuitable. But pretend
4355 : that lambda static thunks don't exist. */
4356 134 : if (!lambda_static_thunk_p (fun))
4357 122 : error_at (loc, "%qD called in a constant expression", fun);
4358 134 : explain_invalid_constexpr_fn (fun);
4359 : }
4360 : else
4361 38 : error_at (loc, "%qD used before its definition", fun);
4362 : }
4363 520502 : *non_constant_p = true;
4364 520502 : return t;
4365 : }
4366 : }
4367 :
4368 : /* Don't complain about problems evaluating an ill-formed function. */
4369 103013732 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4370 103013732 : if (f->language->erroneous)
4371 871 : new_ctx.quiet = true;
4372 :
4373 103013732 : int depth_ok = push_cx_call_context (t);
4374 :
4375 : /* Remember the object we are constructing or destructing. */
4376 103013732 : tree new_obj = NULL_TREE;
4377 206027464 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4378 : {
4379 : /* In a cdtor, it should be the first `this' argument.
4380 : At this point it has already been evaluated in the call
4381 : to cxx_bind_parameters_in_call. */
4382 12606259 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4383 12606259 : bool empty_base = false;
4384 12606259 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4385 : &empty_base, jump_target);
4386 12606259 : if (*jump_target)
4387 0 : return NULL_TREE;
4388 : /* If we're initializing an empty class, don't set constness, because
4389 : cxx_fold_indirect_ref will return the wrong object to set constness
4390 : of. */
4391 12606259 : if (empty_base)
4392 : new_obj = NULL_TREE;
4393 6391383 : else if (ctx->call && ctx->call->fundef
4394 23152770 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4395 : {
4396 3328675 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4397 3328675 : STRIP_NOPS (cur_obj);
4398 3328675 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4399 3326170 : cur_obj = TREE_OPERAND (cur_obj, 0);
4400 3328675 : if (new_obj == cur_obj)
4401 : /* We're calling the target constructor of a delegating
4402 : constructor, or accessing a base subobject through a
4403 : NOP_EXPR as part of a call to a base constructor, so
4404 : there is no new (sub)object. */
4405 2290750 : new_obj = NULL_TREE;
4406 : }
4407 : }
4408 :
4409 103013732 : tree result = NULL_TREE;
4410 :
4411 103013732 : constexpr_call *entry = NULL;
4412 103013732 : if (depth_ok && !non_constant_args && ctx->strict)
4413 : {
4414 36062802 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4415 36062802 : new_call.hash
4416 36062802 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4417 :
4418 : /* If we have seen this call before, we are done. */
4419 36062802 : maybe_initialize_constexpr_call_table ();
4420 36062802 : bool insert = depth_ok < constexpr_cache_depth;
4421 36062802 : constexpr_call **slot
4422 48816258 : = constexpr_call_table->find_slot (&new_call,
4423 : insert ? INSERT : NO_INSERT);
4424 36062802 : entry = slot ? *slot : NULL;
4425 31729837 : if (entry == NULL)
4426 : {
4427 : /* Only cache up to constexpr_cache_depth to limit memory use. */
4428 8962752 : if (insert)
4429 : {
4430 : /* We need to keep a pointer to the entry, not just the slot, as
4431 : the slot can move during evaluation of the body. */
4432 4629787 : *slot = entry = ggc_alloc<constexpr_call> ();
4433 4629787 : *entry = new_call;
4434 4629787 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4435 4629787 : fb.preserve ();
4436 :
4437 : /* Unshare args going into the hash table to separate them
4438 : from the caller's context, for better GC and to avoid
4439 : problems with verify_gimple. */
4440 4629787 : tree bound = new_call.bindings;
4441 7472429 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4442 : {
4443 2842642 : tree &arg = TREE_VEC_ELT (bound, i);
4444 2842642 : arg = unshare_expr_without_location (arg);
4445 : }
4446 : }
4447 : }
4448 : /* Calls that are in progress have their result set to unknown_type_node,
4449 : so that we can detect circular dependencies. Now that we only cache
4450 : up to constexpr_cache_depth this won't catch circular dependencies that
4451 : start deeper, but they'll hit the recursion or ops limit. */
4452 27100050 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4453 : {
4454 6 : if (!ctx->quiet)
4455 0 : error ("call has circular dependency");
4456 6 : *non_constant_p = true;
4457 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4458 : }
4459 : else
4460 27100044 : result = entry->result (ctx->manifestly_const_eval);
4461 : }
4462 :
4463 103013732 : if (!depth_ok)
4464 : {
4465 30 : if (!ctx->quiet)
4466 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4467 : "%<-fconstexpr-depth=%> to increase the maximum)",
4468 : max_constexpr_depth);
4469 30 : *non_constant_p = true;
4470 30 : result = error_mark_node;
4471 : }
4472 : else
4473 : {
4474 103013702 : bool cacheable = !!entry;
4475 103013702 : if (result && result != error_mark_node)
4476 : /* OK */;
4477 80979100 : else if (!DECL_SAVED_TREE (fun))
4478 : {
4479 : /* When at_eof >= 3, cgraph has started throwing away
4480 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4481 : late code generation for VEC_INIT_EXPR, which needs to be
4482 : completely reconsidered. */
4483 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4484 0 : *non_constant_p = true;
4485 : }
4486 80979100 : else if (tree copy = get_fundef_copy (new_call.fundef))
4487 : {
4488 80979091 : tree body, parms, res;
4489 80979091 : releasing_vec ctors;
4490 :
4491 : /* Reuse or create a new unshared copy of this function's body. */
4492 80979091 : body = TREE_PURPOSE (copy);
4493 80979091 : parms = TREE_VALUE (copy);
4494 80979091 : res = TREE_TYPE (copy);
4495 :
4496 : /* Associate the bindings with the remapped parms. */
4497 80979091 : tree bound = new_call.bindings;
4498 80979091 : tree remapped = parms;
4499 197916968 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4500 : {
4501 116937877 : tree arg = TREE_VEC_ELT (bound, i);
4502 116937877 : if (entry)
4503 : {
4504 : /* Unshare again so the callee doesn't change the
4505 : argument values in the hash table. XXX Could we unshare
4506 : lazily in cxx_eval_store_expression? */
4507 3077751 : arg = unshare_constructor (arg);
4508 3077751 : if (TREE_CODE (arg) == CONSTRUCTOR)
4509 873704 : vec_safe_push (ctors, arg);
4510 : }
4511 116937877 : ctx->global->put_value (remapped, arg);
4512 116937877 : remapped = DECL_CHAIN (remapped);
4513 : }
4514 80979091 : if (remapped)
4515 : {
4516 : /* We shouldn't have any parms without args, but fail gracefully
4517 : in error recovery. */
4518 6 : gcc_checking_assert (seen_error ());
4519 6 : *non_constant_p = true;
4520 : }
4521 : /* Add the RESULT_DECL to the values map, too. */
4522 80979091 : gcc_assert (!DECL_BY_REFERENCE (res));
4523 80979091 : ctx->global->put_value (res, NULL_TREE);
4524 :
4525 : /* Remember the current call we're evaluating. */
4526 80979091 : constexpr_ctx call_ctx = *ctx;
4527 80979091 : call_ctx.call = &new_call;
4528 80979091 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4529 80979091 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4530 80979091 : bool save_metafns_called = ctx->global->metafns_called;
4531 :
4532 : /* Make sure we fold std::is_constant_evaluated to true in an
4533 : immediate function. */
4534 161958182 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4535 2755907 : call_ctx.manifestly_const_eval = mce_true;
4536 :
4537 : /* If this is a constexpr destructor, the object's const and volatile
4538 : semantics are no longer in effect; see [class.dtor]p5. */
4539 91294578 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4540 1210965 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4541 : non_constant_p, overflow_p, jump_target);
4542 :
4543 : /* If this is a constructor, we are beginning the lifetime of the
4544 : object we are initializing. */
4545 80979091 : if (new_obj
4546 20630974 : && DECL_CONSTRUCTOR_P (fun)
4547 9104522 : && TREE_CODE (new_obj) == COMPONENT_REF
4548 83584288 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4549 : {
4550 8147 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4551 8147 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4552 8147 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4553 : new_obj, ctor);
4554 8147 : cxx_eval_constant_expression (ctx, activate,
4555 : lval, non_constant_p, overflow_p,
4556 : jump_target);
4557 8147 : ggc_free (activate);
4558 8147 : if (*jump_target)
4559 0 : return NULL_TREE;
4560 : }
4561 :
4562 80979091 : ctx->global->metafns_called = false;
4563 :
4564 80979091 : tree jmp_target = NULL_TREE;
4565 80979091 : cxx_eval_constant_expression (&call_ctx, body,
4566 : vc_discard, non_constant_p, overflow_p,
4567 : &jmp_target);
4568 :
4569 80979087 : if (!*non_constant_p && throws (&jmp_target))
4570 : {
4571 2778 : result = NULL_TREE;
4572 2778 : cacheable = false;
4573 2778 : *jump_target = jmp_target;
4574 : }
4575 161952618 : else if (DECL_CONSTRUCTOR_P (fun))
4576 : /* This can be null for a subobject constructor call, in
4577 : which case what we care about is the initialization
4578 : side-effects rather than the value. We could get at the
4579 : value by evaluating *this, but we don't bother; there's
4580 : no need to put such a call in the hash table. */
4581 11227495 : result = lval ? ctx->object : ctx->ctor;
4582 69748814 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4583 7083637 : result = void_node;
4584 : else
4585 : {
4586 62665177 : result = ctx->global->get_value (res);
4587 9895478 : if (result == NULL_TREE && !*non_constant_p
4588 62665395 : && !DECL_DESTRUCTOR_P (fun))
4589 : {
4590 109 : if (!ctx->quiet)
4591 6 : error ("%<constexpr%> call flows off the end "
4592 : "of the function");
4593 109 : *non_constant_p = true;
4594 : }
4595 : }
4596 :
4597 80979087 : if (ctx->global->metafns_called)
4598 65666 : cacheable = false;
4599 80979087 : ctx->global->metafns_called |= save_metafns_called;
4600 :
4601 : /* At this point, the object's constructor will have run, so
4602 : the object is no longer under construction, and its possible
4603 : 'const' semantics now apply. Make a note of this fact by
4604 : marking the CONSTRUCTOR TREE_READONLY. */
4605 91294574 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4606 9104522 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4607 : non_constant_p, overflow_p, jump_target);
4608 :
4609 : /* Remove the parms/result from the values map. */
4610 80979087 : destroy_value_checked (ctx, res, non_constant_p);
4611 197916968 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4612 116937881 : destroy_value_checked (ctx, parm, non_constant_p);
4613 :
4614 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4615 81852791 : while (!ctors->is_empty ())
4616 : {
4617 873704 : tree c = ctors->pop ();
4618 873704 : if (c != result)
4619 873704 : free_constructor (c);
4620 : }
4621 :
4622 : /* Make the unshared function copy we used available for re-use. */
4623 80979087 : save_fundef_copy (fun, copy);
4624 :
4625 : /* If the call allocated some heap object that hasn't been
4626 : deallocated during the call, or if it deallocated some heap
4627 : object it has not allocated, the call isn't really stateless
4628 : for the constexpr evaluation and should not be cached.
4629 : It is fine if the call allocates something and deallocates it
4630 : too. */
4631 80979087 : if (cacheable
4632 90669819 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4633 9686961 : || (save_heap_dealloc_count
4634 9686961 : != ctx->global->heap_dealloc_count)))
4635 : {
4636 3771 : tree heap_var;
4637 3771 : unsigned int i;
4638 3771 : if ((ctx->global->heap_vars.length ()
4639 3771 : - ctx->global->heap_dealloc_count)
4640 3771 : != save_heap_alloc_count - save_heap_dealloc_count)
4641 : cacheable = false;
4642 : else
4643 130997 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4644 : save_heap_alloc_count)
4645 128878 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4646 : {
4647 : cacheable = false;
4648 : break;
4649 : }
4650 : }
4651 :
4652 : /* Rewrite all occurrences of the function's RESULT_DECL with the
4653 : current object under construction. */
4654 80979087 : if (!*non_constant_p
4655 68018075 : && ctx->object
4656 16258399 : && CLASS_TYPE_P (TREE_TYPE (res))
4657 83993134 : && !is_empty_class (TREE_TYPE (res)))
4658 : {
4659 2105576 : if (!same_type_ignoring_top_level_qualifiers_p
4660 2105576 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4661 0 : *non_constant_p = true;
4662 2105576 : else if (replace_decl (&result, res, ctx->object))
4663 : {
4664 1677 : cacheable = false;
4665 1677 : result = cxx_eval_constant_expression (ctx, result, lval,
4666 : non_constant_p,
4667 : overflow_p,
4668 : jump_target);
4669 1677 : if (*jump_target)
4670 : {
4671 0 : cacheable = false;
4672 0 : result = NULL_TREE;
4673 : }
4674 : }
4675 : }
4676 :
4677 : /* Only cache a permitted result of a constant expression. */
4678 80977410 : if (cacheable && !reduced_constant_expression_p (result))
4679 : cacheable = false;
4680 :
4681 : /* Only cache a result without contract violations. */
4682 76003253 : if (cacheable && ctx->global->contract_statement)
4683 76266327 : cacheable = false;
4684 80979087 : }
4685 : else
4686 : /* Couldn't get a function copy to evaluate. */
4687 9 : *non_constant_p = true;
4688 :
4689 103013698 : if (result == error_mark_node)
4690 0 : *non_constant_p = true;
4691 103013698 : if (*non_constant_p || *overflow_p)
4692 12961177 : result = error_mark_node;
4693 90052521 : else if (!result)
4694 3443215 : result = void_node;
4695 103013698 : if (entry)
4696 : {
4697 63459670 : entry->result (ctx->manifestly_const_eval)
4698 31729835 : = cacheable ? result : error_mark_node;
4699 :
4700 31729835 : if (result != error_mark_node
4701 26819234 : && ctx->manifestly_const_eval == mce_unknown)
4702 : {
4703 : /* Evaluation succeeded and was independent of whether we're in a
4704 : manifestly constant-evaluated context, so we can also reuse
4705 : this result when evaluating this call with a fixed context. */
4706 1903936 : if (!entry->result (mce_true))
4707 708811 : entry->result (mce_true) = entry->result (mce_unknown);
4708 1903936 : if (!entry->result (mce_false))
4709 676089 : entry->result (mce_false) = entry->result (mce_unknown);
4710 : }
4711 : }
4712 : }
4713 :
4714 : /* The result of a constexpr function must be completely initialized.
4715 :
4716 : However, in C++20, a constexpr constructor doesn't necessarily have
4717 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4718 : in order to detect reading an uninitialized object in constexpr instead
4719 : of value-initializing it. (reduced_constant_expression_p is expected to
4720 : take care of clearing the flag.) */
4721 103013728 : if (TREE_CODE (result) == CONSTRUCTOR
4722 103013728 : && (cxx_dialect < cxx20
4723 20499926 : || !DECL_CONSTRUCTOR_P (fun)))
4724 5042521 : clear_no_implicit_zero (result);
4725 :
4726 103013728 : pop_cx_call_context ();
4727 103013728 : return result;
4728 165715604 : }
4729 :
4730 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4731 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4732 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4733 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4734 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4735 :
4736 : bool
4737 1215821911 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4738 : {
4739 1215821911 : if (t == NULL_TREE)
4740 : return false;
4741 :
4742 1210915940 : switch (TREE_CODE (t))
4743 : {
4744 : case PTRMEM_CST:
4745 : case REFLECT_EXPR:
4746 : /* Even if we can't lower this yet, it's constant. */
4747 : return true;
4748 :
4749 : case OMP_DECLARE_MAPPER:
4750 : return true;
4751 :
4752 60036244 : case CONSTRUCTOR:
4753 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4754 60036244 : tree field;
4755 60036244 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4756 : /* A constant vector would be folded to VECTOR_CST.
4757 : A CONSTRUCTOR of scalar type means uninitialized. */
4758 : return false;
4759 60019927 : if (CONSTRUCTOR_NO_CLEARING (t))
4760 : {
4761 3467220 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4762 : {
4763 : /* There must be a valid constant initializer at every array
4764 : index. */
4765 5666 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4766 5666 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4767 5666 : tree cursor = min;
4768 94343 : for (auto &e: CONSTRUCTOR_ELTS (t))
4769 : {
4770 77455 : if (!reduced_constant_expression_p (e.value))
4771 : return false;
4772 77405 : if (array_index_cmp (cursor, e.index) != 0)
4773 : return false;
4774 77345 : if (TREE_CODE (e.index) == RANGE_EXPR)
4775 0 : cursor = TREE_OPERAND (e.index, 1);
4776 77345 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4777 0 : cursor
4778 0 : = int_const_binop (PLUS_EXPR, cursor,
4779 0 : size_int (RAW_DATA_LENGTH (e.value)));
4780 : else
4781 77345 : cursor = int_const_binop (PLUS_EXPR, cursor,
4782 77345 : size_one_node);
4783 : }
4784 5556 : if (find_array_ctor_elt (t, max) == -1)
4785 : return false;
4786 5468 : goto ok;
4787 : }
4788 3461554 : else if (cxx_dialect >= cxx20
4789 3461554 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4790 : {
4791 : /* A union can have at most one active member. A union with no
4792 : active member has no constituent values, so all constituent
4793 : values are constant. */
4794 : field = NULL_TREE;
4795 : }
4796 : else
4797 3457997 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4798 : }
4799 : else
4800 : field = NULL_TREE;
4801 583825320 : for (auto &e: CONSTRUCTOR_ELTS (t))
4802 : {
4803 : /* If VAL is null, we're in the middle of initializing this
4804 : element. */
4805 461417570 : if (!reduced_constant_expression_p (e.value,
4806 461417570 : (e.index
4807 461417534 : && (TREE_CODE (e.index)
4808 : == FIELD_DECL))
4809 53691093 : ? DECL_SIZE (e.index)
4810 : : NULL_TREE))
4811 : return false;
4812 : /* We want to remove initializers for empty fields in a struct to
4813 : avoid confusing output_constructor. */
4814 460874645 : if (is_empty_field (e.index)
4815 460874645 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4816 : return false;
4817 : /* Check for non-empty fields between initialized fields when
4818 : CONSTRUCTOR_NO_CLEARING. */
4819 459800669 : for (; field && e.index != field;
4820 381576 : field = next_subobject_field (DECL_CHAIN (field)))
4821 386232 : if (!is_really_empty_class (TREE_TYPE (field),
4822 : /*ignore_vptr*/false))
4823 : return false;
4824 459414437 : if (field)
4825 3938005 : field = next_subobject_field (DECL_CHAIN (field));
4826 : }
4827 : /* There could be a non-empty field at the end. */
4828 58295361 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4829 290575 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4830 : {
4831 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4832 : the parent FIELD_DECL (if any) for classes with virtual
4833 : bases. */
4834 6342 : if (cxx_dialect >= cxx26
4835 4204 : && sz
4836 6853 : && tree_int_cst_le (sz, bit_position (field)))
4837 : break;
4838 5954 : return false;
4839 : }
4840 58004786 : ok:
4841 58010642 : if (CONSTRUCTOR_NO_CLEARING (t))
4842 : /* All the fields are initialized. */
4843 3338604 : CONSTRUCTOR_NO_CLEARING (t) = false;
4844 : return true;
4845 :
4846 1150558628 : default:
4847 : /* FIXME are we calling this too much? */
4848 1150558628 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4849 : }
4850 : }
4851 :
4852 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4853 : why and suggest what could be done about it. */
4854 :
4855 : static tree
4856 1435 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4857 : {
4858 1435 : bool ref_p = false;
4859 :
4860 : /* No need to look into types or unevaluated operands. */
4861 1435 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4862 : {
4863 0 : *walk_subtrees = false;
4864 0 : return NULL_TREE;
4865 : }
4866 :
4867 1435 : switch (TREE_CODE (*tp))
4868 : {
4869 104 : CASE_CONVERT:
4870 104 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4871 : break;
4872 85 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4873 85 : *tp = TREE_OPERAND (*tp, 0);
4874 217 : gcc_fallthrough ();
4875 217 : case ADDR_EXPR:
4876 217 : {
4877 217 : tree op = TREE_OPERAND (*tp, 0);
4878 217 : if (VAR_P (op)
4879 116 : && DECL_DECLARED_CONSTEXPR_P (op)
4880 39 : && !TREE_STATIC (op)
4881 : /* ??? We should also say something about temporaries. */
4882 253 : && !DECL_ARTIFICIAL (op))
4883 : {
4884 33 : if (ref_p)
4885 12 : inform (location_of (*tp), "reference to %qD is not a constant "
4886 : "expression", op);
4887 : else
4888 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4889 : "expression", op);
4890 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4891 33 : rich_location richloc (line_table, op_loc);
4892 33 : richloc.add_fixit_insert_before (op_loc, "static ");
4893 33 : inform (&richloc,
4894 : "address of non-static constexpr variable %qD may differ on "
4895 : "each invocation of the enclosing function; add %<static%> "
4896 : "to give it a constant address", op);
4897 33 : }
4898 : break;
4899 : }
4900 : default:
4901 : break;
4902 : }
4903 :
4904 : return NULL_TREE;
4905 : }
4906 :
4907 : /* Some expressions may have constant operands but are not constant
4908 : themselves, such as 1/0. Call this function to check for that
4909 : condition.
4910 :
4911 : We only call this in places that require an arithmetic constant, not in
4912 : places where we might have a non-constant expression that can be a
4913 : component of a constant expression, such as the address of a constexpr
4914 : variable that might be dereferenced later. */
4915 :
4916 : static bool
4917 652199509 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4918 : bool *overflow_p)
4919 : {
4920 641454301 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4921 654785534 : && t != void_node)
4922 : {
4923 2585608 : if (!allow_non_constant)
4924 : {
4925 392 : auto_diagnostic_group d;
4926 517 : error_at (cp_expr_loc_or_input_loc (t),
4927 : "%q+E is not a constant expression", t);
4928 392 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4929 : nullptr);
4930 392 : }
4931 2585608 : *non_constant_p = true;
4932 : }
4933 652199509 : if (TREE_OVERFLOW_P (t))
4934 : {
4935 678 : if (!allow_non_constant)
4936 : {
4937 155 : permerror (input_location, "overflow in constant expression");
4938 : /* If we're being permissive (and are in an enforcing
4939 : context), ignore the overflow. */
4940 155 : if (flag_permissive)
4941 75 : return *non_constant_p;
4942 : }
4943 603 : *overflow_p = true;
4944 : }
4945 652199434 : return *non_constant_p;
4946 : }
4947 :
4948 : /* Check whether the shift operation with code CODE and type TYPE on LHS
4949 : and RHS is undefined. If it is, give an error with an explanation,
4950 : and return true; return false otherwise. */
4951 :
4952 : static bool
4953 78183035 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4954 : enum tree_code code, tree type, tree lhs, tree rhs)
4955 : {
4956 78183035 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4957 5535752 : || TREE_CODE (lhs) != INTEGER_CST
4958 5535476 : || TREE_CODE (rhs) != INTEGER_CST)
4959 : return false;
4960 :
4961 5535476 : tree lhstype = TREE_TYPE (lhs);
4962 5535476 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4963 :
4964 : /* [expr.shift] The behavior is undefined if the right operand
4965 : is negative, or greater than or equal to the length in bits
4966 : of the promoted left operand. */
4967 5535476 : if (tree_int_cst_sgn (rhs) == -1)
4968 : {
4969 126 : if (!ctx->quiet)
4970 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4971 : build2_loc (loc, code, type, lhs, rhs));
4972 126 : return (!flag_permissive || ctx->quiet);
4973 : }
4974 5535350 : if (compare_tree_int (rhs, uprec) >= 0)
4975 : {
4976 200 : if (!ctx->quiet)
4977 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4978 : "than or equal to the precision %wu of the left operand",
4979 : build2_loc (loc, code, type, lhs, rhs), uprec);
4980 200 : return (!flag_permissive || ctx->quiet);
4981 : }
4982 :
4983 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4984 : if E1 has a signed type and non-negative value, and E1x2^E2 is
4985 : representable in the corresponding unsigned type of the result type,
4986 : then that value, converted to the result type, is the resulting value;
4987 : otherwise, the behavior is undefined.
4988 : For C++20:
4989 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4990 : 2^N, where N is the range exponent of the type of the result. */
4991 5535150 : if (code == LSHIFT_EXPR
4992 4646698 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4993 2185180 : && cxx_dialect >= cxx11
4994 7700301 : && cxx_dialect < cxx20)
4995 : {
4996 54036 : if (tree_int_cst_sgn (lhs) == -1)
4997 : {
4998 74 : if (!ctx->quiet)
4999 18 : permerror (loc,
5000 : "left operand of shift expression %q+E is negative",
5001 : build2_loc (loc, code, type, lhs, rhs));
5002 74 : return (!flag_permissive || ctx->quiet);
5003 : }
5004 : /* For signed x << y the following:
5005 : (unsigned) x >> ((prec (lhs) - 1) - y)
5006 : if > 1, is undefined. The right-hand side of this formula
5007 : is the highest bit of the LHS that can be set (starting from 0),
5008 : so that the shift doesn't overflow. We then right-shift the LHS
5009 : to see whether any other bit is set making the original shift
5010 : undefined -- the result is not representable in the corresponding
5011 : unsigned type. */
5012 53962 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
5013 53962 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
5014 53962 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
5015 53962 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
5016 53962 : if (tree_int_cst_lt (integer_one_node, t))
5017 : {
5018 41 : if (!ctx->quiet)
5019 7 : permerror (loc, "shift expression %q+E overflows",
5020 : build2_loc (loc, code, type, lhs, rhs));
5021 41 : return (!flag_permissive || ctx->quiet);
5022 : }
5023 : }
5024 : return false;
5025 : }
5026 :
5027 : /* Subroutine of cxx_eval_constant_expression.
5028 : Attempt to reduce the unary expression tree T to a compile time value.
5029 : If successful, return the value. Otherwise issue a diagnostic
5030 : and return error_mark_node. */
5031 :
5032 : static tree
5033 24561343 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
5034 : bool /*lval*/,
5035 : bool *non_constant_p, bool *overflow_p,
5036 : tree *jump_target)
5037 : {
5038 24561343 : tree r;
5039 24561343 : tree orig_arg = TREE_OPERAND (t, 0);
5040 24561343 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
5041 : non_constant_p, overflow_p,
5042 : jump_target);
5043 24561341 : if (*jump_target)
5044 : return NULL_TREE;
5045 24561339 : VERIFY_CONSTANT (arg);
5046 20334168 : location_t loc = EXPR_LOCATION (t);
5047 20334168 : enum tree_code code = TREE_CODE (t);
5048 20334168 : tree type = TREE_TYPE (t);
5049 20334168 : r = fold_unary_loc (loc, code, type, arg);
5050 20334168 : if (r == NULL_TREE)
5051 : {
5052 17 : if (arg == orig_arg)
5053 : r = t;
5054 : else
5055 13 : r = build1_loc (loc, code, type, arg);
5056 : }
5057 20334168 : VERIFY_CONSTANT (r);
5058 : return r;
5059 : }
5060 :
5061 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5062 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5063 : generic folding should be used. */
5064 :
5065 : static tree
5066 10470876 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5067 : tree lhs, tree rhs, bool *non_constant_p,
5068 : bool *overflow_p, tree *jump_target)
5069 : {
5070 10470876 : STRIP_NOPS (lhs);
5071 10470876 : if (TREE_CODE (lhs) != ADDR_EXPR)
5072 : return NULL_TREE;
5073 :
5074 9618649 : lhs = TREE_OPERAND (lhs, 0);
5075 :
5076 : /* &A[i] p+ j => &A[i + j] */
5077 9618649 : if (TREE_CODE (lhs) == ARRAY_REF
5078 208329 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5079 208329 : && TREE_CODE (rhs) == INTEGER_CST
5080 208329 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5081 9826978 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5082 : {
5083 208329 : tree orig_type = TREE_TYPE (t);
5084 208329 : location_t loc = EXPR_LOCATION (t);
5085 208329 : tree type = TREE_TYPE (lhs);
5086 :
5087 208329 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5088 208329 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5089 208329 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5090 : non_constant_p, overflow_p,
5091 : jump_target);
5092 208329 : if (*non_constant_p)
5093 : return NULL_TREE;
5094 208317 : if (*jump_target)
5095 : return NULL_TREE;
5096 : /* Don't fold an out-of-bound access. */
5097 208317 : if (!tree_int_cst_le (t, nelts))
5098 : return NULL_TREE;
5099 208317 : rhs = cp_fold_convert (ssizetype, rhs);
5100 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5101 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5102 208317 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5103 208317 : rhs, TYPE_SIZE_UNIT (type))))
5104 : return NULL_TREE;
5105 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5106 : as signed. */
5107 208285 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5108 208285 : TYPE_SIZE_UNIT (type));
5109 208285 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5110 208285 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5111 : t, NULL_TREE, NULL_TREE);
5112 208285 : t = cp_build_addr_expr (t, tf_warning_or_error);
5113 208285 : t = cp_fold_convert (orig_type, t);
5114 208285 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5115 : non_constant_p, overflow_p,
5116 208285 : jump_target);
5117 : }
5118 :
5119 : return NULL_TREE;
5120 : }
5121 :
5122 : /* Try to fold expressions like
5123 : (struct S *) (&a[0].D.2378 + 12)
5124 : into
5125 : &MEM <struct T> [(void *)&a + 12B]
5126 : This is something normally done by gimple_fold_stmt_to_constant_1
5127 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5128 : dereference the address because some details are lost.
5129 : For pointer comparisons we want such folding though so that
5130 : match.pd address_compare optimization works. */
5131 :
5132 : static tree
5133 11639562 : cxx_maybe_fold_addr_pointer_plus (tree t)
5134 : {
5135 23279130 : while (CONVERT_EXPR_P (t)
5136 12273804 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5137 634236 : t = TREE_OPERAND (t, 0);
5138 11639562 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5139 : return NULL_TREE;
5140 10055234 : tree op0 = TREE_OPERAND (t, 0);
5141 10055234 : tree op1 = TREE_OPERAND (t, 1);
5142 10055234 : if (TREE_CODE (op1) != INTEGER_CST)
5143 : return NULL_TREE;
5144 10055234 : while (CONVERT_EXPR_P (op0)
5145 20106957 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5146 10051723 : op0 = TREE_OPERAND (op0, 0);
5147 10055234 : if (TREE_CODE (op0) != ADDR_EXPR)
5148 : return NULL_TREE;
5149 10055234 : op1 = fold_convert (ptr_type_node, op1);
5150 10055234 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5151 10055234 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5152 : }
5153 :
5154 : /* Subroutine of cxx_eval_constant_expression.
5155 : Like cxx_eval_unary_expression, except for binary expressions. */
5156 :
5157 : static tree
5158 106704755 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5159 : value_cat lval,
5160 : bool *non_constant_p, bool *overflow_p,
5161 : tree *jump_target)
5162 : {
5163 106704755 : tree r = NULL_TREE;
5164 106704755 : tree orig_lhs = TREE_OPERAND (t, 0);
5165 106704755 : tree orig_rhs = TREE_OPERAND (t, 1);
5166 106704755 : tree lhs, rhs;
5167 106704755 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5168 : non_constant_p, overflow_p,
5169 : jump_target);
5170 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5171 : subtraction. */
5172 106704753 : if (*non_constant_p)
5173 : return t;
5174 87878241 : if (*jump_target)
5175 : return NULL_TREE;
5176 87878146 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5177 : non_constant_p, overflow_p,
5178 : jump_target);
5179 87878146 : if (*non_constant_p)
5180 : return t;
5181 86874811 : if (*jump_target)
5182 : return NULL_TREE;
5183 :
5184 86874805 : location_t loc = EXPR_LOCATION (t);
5185 86874805 : enum tree_code code = TREE_CODE (t);
5186 86874805 : tree type = TREE_TYPE (t);
5187 :
5188 86874805 : if (code == EQ_EXPR || code == NE_EXPR)
5189 : {
5190 17942418 : bool is_code_eq = (code == EQ_EXPR);
5191 :
5192 17942418 : if (TREE_CODE (lhs) == PTRMEM_CST
5193 209 : && TREE_CODE (rhs) == PTRMEM_CST)
5194 : {
5195 68 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5196 68 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5197 68 : bool eq;
5198 68 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5199 68 : && TREE_CODE (lmem) == FIELD_DECL
5200 68 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5201 95 : && same_type_p (DECL_CONTEXT (lmem),
5202 : DECL_CONTEXT (rmem)))
5203 : /* If both refer to (possibly different) members of the same union
5204 : (12.3), they compare equal. */
5205 : eq = true;
5206 : else
5207 41 : eq = cp_tree_equal (lhs, rhs);
5208 68 : r = constant_boolean_node (eq == is_code_eq, type);
5209 68 : }
5210 17942350 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5211 17942209 : || TREE_CODE (rhs) == PTRMEM_CST)
5212 17942350 : && (null_member_pointer_value_p (lhs)
5213 141 : || null_member_pointer_value_p (rhs)))
5214 141 : r = constant_boolean_node (!is_code_eq, type);
5215 17942209 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5216 0 : lhs = cplus_expand_constant (lhs);
5217 17942209 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5218 0 : rhs = cplus_expand_constant (rhs);
5219 17942209 : else if (REFLECT_EXPR_P (lhs) && REFLECT_EXPR_P (rhs))
5220 : {
5221 3904 : const bool eq = compare_reflections (lhs, rhs);
5222 3904 : r = constant_boolean_node (eq == is_code_eq, type);
5223 : }
5224 : }
5225 0 : if (r == NULL_TREE
5226 86870692 : && TREE_CODE_CLASS (code) == tcc_comparison
5227 38205240 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5228 : {
5229 5819781 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5230 4893886 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5231 5819781 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5232 5161348 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5233 : }
5234 10471031 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5235 97345836 : && integer_zerop (lhs) && !integer_zerop (rhs))
5236 : {
5237 155 : if (!ctx->quiet)
5238 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5239 155 : *non_constant_p = true;
5240 155 : return t;
5241 : }
5242 86874650 : else if (code == POINTER_PLUS_EXPR)
5243 : {
5244 10470876 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5245 : overflow_p, jump_target);
5246 10470876 : if (*jump_target)
5247 : return NULL_TREE;
5248 : }
5249 76403774 : else if (code == SPACESHIP_EXPR)
5250 : {
5251 27174 : r = genericize_spaceship (loc, type, lhs, rhs);
5252 27174 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5253 27174 : overflow_p, jump_target);
5254 : }
5255 :
5256 86847476 : if (r == NULL_TREE)
5257 : {
5258 86635078 : if (ctx->manifestly_const_eval == mce_true
5259 66672607 : && (flag_constexpr_fp_except
5260 66672604 : || TREE_CODE (type) != REAL_TYPE))
5261 : {
5262 66619423 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5263 66619423 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5264 66619423 : }
5265 : else
5266 20015655 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5267 : }
5268 :
5269 86635078 : if (r == NULL_TREE
5270 8664539 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5271 100 : && TREE_CODE (lhs) == INTEGER_CST
5272 98 : && TREE_CODE (rhs) == INTEGER_CST
5273 95512015 : && wi::neg_p (wi::to_wide (rhs)))
5274 : {
5275 : /* For diagnostics and -fpermissive emulate previous behavior of
5276 : handling shifts by negative amount. */
5277 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5278 98 : if (nrhs)
5279 119 : r = fold_binary_loc (loc,
5280 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5281 : type, lhs, nrhs);
5282 : }
5283 :
5284 86847476 : if (r == NULL_TREE)
5285 : {
5286 8664441 : if (lhs == orig_lhs && rhs == orig_rhs)
5287 : r = t;
5288 : else
5289 3034599 : r = build2_loc (loc, code, type, lhs, rhs);
5290 : }
5291 78183035 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5292 433 : *non_constant_p = true;
5293 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5294 : a local array in a constexpr function. */
5295 86847476 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5296 69908600 : if (!ptr)
5297 69908600 : VERIFY_CONSTANT (r);
5298 : return r;
5299 : }
5300 :
5301 : /* Subroutine of cxx_eval_constant_expression.
5302 : Attempt to evaluate condition expressions. */
5303 :
5304 : static tree
5305 37467583 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5306 : value_cat lval,
5307 : bool *non_constant_p, bool *overflow_p,
5308 : tree *jump_target)
5309 : {
5310 37467583 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5311 : vc_prvalue,
5312 : non_constant_p, overflow_p,
5313 : jump_target);
5314 37467583 : if (*jump_target)
5315 : return NULL_TREE;
5316 37467559 : VERIFY_CONSTANT (val);
5317 35755877 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5318 : {
5319 : /* Evaluate the condition as if it was
5320 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5321 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5322 : without manifestly_const_eval even expressions or parts thereof which
5323 : will later be manifestly const_eval evaluated), otherwise fold it to
5324 : true. */
5325 1019405 : if (ctx->manifestly_const_eval == mce_unknown)
5326 : {
5327 918428 : *non_constant_p = true;
5328 918428 : return t;
5329 : }
5330 100977 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5331 : boolean_type_node);
5332 : }
5333 : /* Don't VERIFY_CONSTANT the other operands. */
5334 34837449 : const bool zero_p = integer_zerop (val);
5335 34837449 : if (zero_p)
5336 22018320 : val = TREE_OPERAND (t, 2);
5337 : else
5338 12819129 : val = TREE_OPERAND (t, 1);
5339 34837449 : if (TREE_CODE (t) == IF_STMT && !val)
5340 9485881 : val = void_node;
5341 :
5342 : /* P2564: If we aren't in immediate function context (including a manifestly
5343 : constant-evaluated expression), check any uses of immediate functions in
5344 : the arm we're discarding. But don't do this inside a call; we already
5345 : checked when parsing the function. */
5346 34837449 : if (ctx->manifestly_const_eval != mce_true
5347 4474208 : && !in_immediate_context ()
5348 4377354 : && !ctx->call
5349 35639383 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5350 801934 : ctx->manifestly_const_eval))
5351 : {
5352 7 : *non_constant_p = true;
5353 7 : return t;
5354 : }
5355 :
5356 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5357 : serve as the initializer for the same object as the outer TARGET_EXPR,
5358 : as in
5359 : A a = true ? A{} : A{};
5360 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5361 34837442 : if (TREE_CODE (val) == TARGET_EXPR)
5362 5159 : val = TARGET_EXPR_INITIAL (val);
5363 34837442 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5364 34837442 : overflow_p, jump_target);
5365 : }
5366 :
5367 : /* Subroutine of cxx_eval_constant_expression.
5368 : Attempt to evaluate vector condition expressions. Unlike
5369 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5370 : ternary arithmetics operation, where all 3 arguments have to be
5371 : evaluated as constants and then folding computes the result from
5372 : them. */
5373 :
5374 : static tree
5375 9374 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5376 : bool *non_constant_p, bool *overflow_p,
5377 : tree *jump_target)
5378 : {
5379 9374 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5380 : vc_prvalue,
5381 : non_constant_p, overflow_p,
5382 : jump_target);
5383 9374 : if (*jump_target)
5384 : return NULL_TREE;
5385 9374 : VERIFY_CONSTANT (arg1);
5386 6727 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5387 : vc_prvalue,
5388 : non_constant_p, overflow_p,
5389 : jump_target);
5390 6727 : if (*jump_target)
5391 : return NULL_TREE;
5392 6727 : VERIFY_CONSTANT (arg2);
5393 6593 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5394 : vc_prvalue,
5395 : non_constant_p, overflow_p,
5396 : jump_target);
5397 6593 : if (*jump_target)
5398 : return NULL_TREE;
5399 6593 : VERIFY_CONSTANT (arg3);
5400 6593 : location_t loc = EXPR_LOCATION (t);
5401 6593 : tree type = TREE_TYPE (t);
5402 6593 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5403 6593 : if (r == NULL_TREE)
5404 : {
5405 0 : if (arg1 == TREE_OPERAND (t, 0)
5406 0 : && arg2 == TREE_OPERAND (t, 1)
5407 0 : && arg3 == TREE_OPERAND (t, 2))
5408 : r = t;
5409 : else
5410 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5411 : }
5412 6593 : VERIFY_CONSTANT (r);
5413 : return r;
5414 : }
5415 :
5416 : /* Returns less than, equal to, or greater than zero if KEY is found to be
5417 : less than, to match, or to be greater than the constructor_elt's INDEX. */
5418 :
5419 : static int
5420 431502 : array_index_cmp (tree key, tree index)
5421 : {
5422 431502 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5423 :
5424 431502 : switch (TREE_CODE (index))
5425 : {
5426 428611 : case INTEGER_CST:
5427 428611 : return tree_int_cst_compare (key, index);
5428 2891 : case RANGE_EXPR:
5429 2891 : {
5430 2891 : tree lo = TREE_OPERAND (index, 0);
5431 2891 : tree hi = TREE_OPERAND (index, 1);
5432 2891 : if (tree_int_cst_lt (key, lo))
5433 : return -1;
5434 2605 : else if (tree_int_cst_lt (hi, key))
5435 : return 1;
5436 : else
5437 2605 : return 0;
5438 : }
5439 0 : default:
5440 0 : gcc_unreachable ();
5441 : }
5442 : }
5443 :
5444 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5445 : relative index OFF. */
5446 :
5447 : static tree
5448 29559 : raw_data_cst_elt (tree raw_data, unsigned int off)
5449 : {
5450 29559 : return build_int_cst (TREE_TYPE (raw_data),
5451 29559 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5452 59118 : ? (HOST_WIDE_INT)
5453 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
5454 : : (HOST_WIDE_INT)
5455 486 : RAW_DATA_SCHAR_ELT (raw_data, off));
5456 : }
5457 :
5458 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5459 : if none. If INSERT is true, insert a matching element rather than fail. */
5460 :
5461 : static HOST_WIDE_INT
5462 12099022 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5463 : {
5464 12099022 : if (tree_int_cst_sgn (dindex) < 0)
5465 : return -1;
5466 :
5467 12099022 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5468 12099022 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5469 12099022 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5470 :
5471 16298554 : unsigned HOST_WIDE_INT end = len;
5472 16298554 : unsigned HOST_WIDE_INT begin = 0;
5473 :
5474 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5475 : that the same is true of the other elements and index directly. */
5476 11571355 : if (end > 0)
5477 : {
5478 11344431 : tree cindex = (*elts)[end - 1].index;
5479 11344431 : if (cindex == NULL_TREE)
5480 : {
5481 : /* Verify that if the last index is missing, all indexes
5482 : are missing and there is no RAW_DATA_CST. */
5483 21652 : if (flag_checking)
5484 219358 : for (unsigned int j = 0; j < len - 1; ++j)
5485 197706 : gcc_assert ((*elts)[j].index == NULL_TREE
5486 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5487 21652 : if (i < end)
5488 21652 : return i;
5489 : else
5490 : {
5491 0 : begin = end;
5492 0 : if (i == end)
5493 : /* If the element is to be added right at the end,
5494 : make sure it is added with cleared index too. */
5495 4727199 : dindex = NULL_TREE;
5496 0 : else if (insert)
5497 : /* Otherwise, in order not to break the assumption
5498 : that CONSTRUCTOR either has all indexes or none,
5499 : we need to add indexes to all elements. */
5500 0 : for (unsigned int j = 0; j < len; ++j)
5501 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5502 : }
5503 : }
5504 11322779 : else if (TREE_CODE (cindex) == INTEGER_CST
5505 11322779 : && compare_tree_int (cindex, end - 1) == 0)
5506 : {
5507 11118537 : tree value = (*elts)[end - 1].value;
5508 11118537 : if (i < end)
5509 : {
5510 7350261 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5511 : begin = end - 1;
5512 : else
5513 7350171 : return i;
5514 : }
5515 3768276 : else if (TREE_CODE (value) == RAW_DATA_CST
5516 3800432 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5517 32156 : + RAW_DATA_LENGTH (value)))
5518 16078 : begin = end - 1;
5519 : else
5520 3752198 : begin = end;
5521 : }
5522 : }
5523 :
5524 : /* Otherwise, find a matching index by means of a binary search. */
5525 4930553 : while (begin != end)
5526 : {
5527 354094 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5528 354094 : constructor_elt &elt = (*elts)[middle];
5529 354094 : tree idx = elt.index;
5530 :
5531 354094 : int cmp = array_index_cmp (dindex, idx);
5532 354094 : if (cmp > 0
5533 66283 : && TREE_CODE (elt.value) == RAW_DATA_CST
5534 437902 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5535 83808 : + RAW_DATA_LENGTH (elt.value)))
5536 29417 : cmp = 0;
5537 354094 : if (cmp < 0)
5538 : end = middle;
5539 187606 : else if (cmp > 0)
5540 36866 : begin = middle + 1;
5541 : else
5542 : {
5543 150740 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5544 : {
5545 : /* We need to split the RAW_DATA_CST elt. */
5546 122 : constructor_elt e;
5547 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5548 244 : unsigned int off = (wi::to_offset (dindex)
5549 244 : - wi::to_offset (idx)).to_uhwi ();
5550 122 : tree value = elt.value;
5551 122 : unsigned int len = RAW_DATA_LENGTH (value);
5552 122 : if (off > 1 && len >= off + 3)
5553 22 : value = copy_node (elt.value);
5554 122 : if (off)
5555 : {
5556 33 : if (off > 1)
5557 33 : RAW_DATA_LENGTH (elt.value) = off;
5558 : else
5559 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5560 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5561 : build_int_cst (TREE_TYPE (elt.index),
5562 : off));
5563 33 : e.value = NULL_TREE;
5564 33 : ++middle;
5565 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5566 : }
5567 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5568 122 : if (len >= off + 2)
5569 : {
5570 111 : e.index = (*elts)[middle].index;
5571 111 : e.index = size_binop (PLUS_EXPR, e.index,
5572 : build_one_cst (TREE_TYPE (e.index)));
5573 111 : if (len >= off + 3)
5574 : {
5575 111 : RAW_DATA_LENGTH (value) -= off + 1;
5576 111 : RAW_DATA_POINTER (value) += off + 1;
5577 111 : e.value = value;
5578 : }
5579 : else
5580 0 : e.value = raw_data_cst_elt (value, off + 1);
5581 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5582 : }
5583 122 : return middle;
5584 : }
5585 35671 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5586 : {
5587 : /* We need to split the range. */
5588 343 : constructor_elt e;
5589 343 : tree lo = TREE_OPERAND (idx, 0);
5590 343 : tree hi = TREE_OPERAND (idx, 1);
5591 343 : tree value = elt.value;
5592 343 : dindex = fold_convert (sizetype, dindex);
5593 343 : if (tree_int_cst_lt (lo, dindex))
5594 : {
5595 : /* There are still some lower elts; shorten the range. */
5596 170 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5597 85 : size_one_node);
5598 85 : if (tree_int_cst_equal (lo, new_hi))
5599 : /* Only one element left, no longer a range. */
5600 33 : elt.index = lo;
5601 : else
5602 52 : TREE_OPERAND (idx, 1) = new_hi;
5603 : /* Append the element we want to insert. */
5604 85 : ++middle;
5605 85 : e.index = dindex;
5606 85 : e.value = unshare_constructor (value);
5607 85 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5608 : }
5609 : else
5610 : /* No lower elts, the range elt is now ours. */
5611 258 : elt.index = dindex;
5612 :
5613 343 : if (tree_int_cst_lt (dindex, hi))
5614 : {
5615 : /* There are still some higher elts; append a range. */
5616 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5617 239 : size_one_node);
5618 239 : if (tree_int_cst_equal (new_lo, hi))
5619 98 : e.index = hi;
5620 : else
5621 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5622 239 : e.value = unshare_constructor (value);
5623 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5624 : }
5625 : }
5626 150618 : return middle;
5627 : }
5628 : }
5629 :
5630 4576459 : if (insert)
5631 : {
5632 4447353 : constructor_elt e = { dindex, NULL_TREE };
5633 4447353 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5634 4447353 : return end;
5635 : }
5636 :
5637 : return -1;
5638 : }
5639 :
5640 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5641 : matching constructor_elt exists, then add one to CTOR.
5642 :
5643 : As an optimization, if POS_HINT is non-negative then it is used as a guess
5644 : for the (integer) index of the matching constructor_elt within CTOR.
5645 :
5646 : If POS_HINT is -2, it means do not insert. */
5647 :
5648 : static constructor_elt *
5649 40609621 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5650 : {
5651 : /* Check the hint first. */
5652 4364177 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5653 44973798 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5654 : return CONSTRUCTOR_ELT (ctor, pos_hint);
5655 :
5656 36245532 : bool insertp = (pos_hint != -2);
5657 36245532 : tree type = TREE_TYPE (ctor);
5658 36245532 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5659 : {
5660 24944 : gcc_assert (insertp);
5661 24944 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5662 24944 : return &CONSTRUCTOR_ELTS (ctor)->last();
5663 : }
5664 36220588 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5665 : {
5666 6096188 : if (TREE_CODE (index) == RANGE_EXPR)
5667 : {
5668 : /* Support for RANGE_EXPR index lookups is currently limited to
5669 : accessing an existing element via POS_HINT, or appending a new
5670 : element to the end of CTOR. ??? Support for other access
5671 : patterns may also be needed. */
5672 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5673 3 : if (vec_safe_length (elts))
5674 : {
5675 3 : tree lo = TREE_OPERAND (index, 0);
5676 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5677 : }
5678 3 : gcc_assert (insertp);
5679 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5680 3 : return &elts->last();
5681 : }
5682 :
5683 6096185 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5684 6096185 : if (i < 0)
5685 : {
5686 1726 : gcc_assert (!insertp);
5687 : return nullptr;
5688 : }
5689 6094459 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5690 6094459 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5691 : {
5692 : /* Split a range even if we aren't inserting new entries. */
5693 0 : gcc_assert (!insertp);
5694 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5695 0 : gcc_assert (i >= 0);
5696 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5697 : }
5698 6094459 : gcc_assert (cep->index == NULL_TREE
5699 : || TREE_CODE (cep->index) != RANGE_EXPR);
5700 : return cep;
5701 : }
5702 : else
5703 : {
5704 30124400 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5705 : && (same_type_ignoring_top_level_qualifiers_p
5706 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5707 :
5708 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5709 : Usually we meet initializers in that order, but it is
5710 : possible for base types to be placed not in program
5711 : order. */
5712 30124400 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5713 30124400 : unsigned HOST_WIDE_INT idx = 0;
5714 30124400 : constructor_elt *cep = NULL;
5715 :
5716 : /* Check if we're changing the active member of a union. */
5717 565306 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5718 30497193 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5719 12109 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5720 : /* If the bit offset of INDEX is larger than that of the last
5721 : constructor_elt, then we can just immediately append a new
5722 : constructor_elt to the end of CTOR. */
5723 30112291 : else if (CONSTRUCTOR_NELTS (ctor)
5724 42597223 : && tree_int_cst_compare (bit_position (index),
5725 41666562 : bit_position (CONSTRUCTOR_ELTS (ctor)
5726 20833281 : ->last().index)) > 0)
5727 : {
5728 5720698 : idx = CONSTRUCTOR_NELTS (ctor);
5729 5720698 : goto insert;
5730 : }
5731 :
5732 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5733 : appropriately. */
5734 :
5735 29029816 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5736 4626114 : idx++, fields = DECL_CHAIN (fields))
5737 : {
5738 19738697 : if (index == cep->index)
5739 15071465 : goto found;
5740 :
5741 : /* The field we're initializing must be on the field
5742 : list. Look to see if it is present before the
5743 : field the current ELT initializes. */
5744 50379271 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5745 45753157 : if (index == fields)
5746 41118 : goto insert;
5747 : }
5748 : /* We fell off the end of the CONSTRUCTOR, so insert a new
5749 : entry at the end. */
5750 :
5751 15052935 : insert:
5752 15052935 : if (!insertp)
5753 : return nullptr;
5754 :
5755 15052932 : {
5756 15052932 : constructor_elt ce = { index, NULL_TREE };
5757 :
5758 15052932 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5759 15052932 : cep = CONSTRUCTOR_ELT (ctor, idx);
5760 : }
5761 30124397 : found:;
5762 :
5763 30124397 : return cep;
5764 : }
5765 : }
5766 :
5767 : /* Under the control of CTX, issue a detailed diagnostic for
5768 : an out-of-bounds subscript INDEX into the expression ARRAY. */
5769 :
5770 : static void
5771 576 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5772 : {
5773 576 : if (!ctx->quiet)
5774 : {
5775 117 : tree arraytype = TREE_TYPE (array);
5776 :
5777 : /* Convert the unsigned array subscript to a signed integer to avoid
5778 : printing huge numbers for small negative values. */
5779 117 : tree sidx = fold_convert (ssizetype, index);
5780 117 : STRIP_ANY_LOCATION_WRAPPER (array);
5781 117 : if (DECL_P (array))
5782 : {
5783 76 : auto_diagnostic_group d;
5784 76 : if (TYPE_DOMAIN (arraytype))
5785 70 : error_at (loc, "array subscript value %qE is outside the bounds "
5786 : "of array %qD of type %qT", sidx, array, arraytype);
5787 : else
5788 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5789 : "type %qT with unknown bounds", sidx, array, arraytype);
5790 76 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5791 76 : }
5792 41 : else if (TYPE_DOMAIN (arraytype))
5793 38 : error_at (loc, "array subscript value %qE is outside the bounds "
5794 : "of array type %qT", sidx, arraytype);
5795 : else
5796 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5797 : "with unknown bounds", sidx, arraytype);
5798 : }
5799 576 : }
5800 :
5801 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5802 : a VECTOR_TYPE). */
5803 :
5804 : static tree
5805 20471932 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5806 : bool *non_constant_p, bool *overflow_p,
5807 : tree *jump_target)
5808 : {
5809 20471932 : tree nelts;
5810 20471932 : if (TREE_CODE (type) == ARRAY_TYPE)
5811 : {
5812 20471932 : if (TYPE_DOMAIN (type))
5813 20471684 : nelts = array_type_nelts_top (type);
5814 : else
5815 248 : nelts = size_zero_node;
5816 : }
5817 0 : else if (VECTOR_TYPE_P (type))
5818 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5819 : else
5820 0 : gcc_unreachable ();
5821 :
5822 : /* For VLAs, the number of elements won't be an integer constant. */
5823 20471932 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5824 : non_constant_p, overflow_p,
5825 : jump_target);
5826 20471932 : return nelts;
5827 : }
5828 :
5829 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5830 : STRING_CST STRING. */
5831 :
5832 : static tree
5833 1953618 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5834 : {
5835 1953618 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5836 1953618 : tree r;
5837 :
5838 1953618 : if (chars_per_elt == 1)
5839 1742140 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5840 : else
5841 : {
5842 211478 : const unsigned char *ptr
5843 211478 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5844 211478 : + index * chars_per_elt);
5845 211478 : r = native_interpret_expr (type, ptr, chars_per_elt);
5846 : }
5847 1953618 : return r;
5848 : }
5849 :
5850 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5851 : subscript, diagnose any problems with it, and return the result. */
5852 :
5853 : static tree
5854 20671328 : eval_and_check_array_index (const constexpr_ctx *ctx,
5855 : tree t, bool allow_one_past,
5856 : bool *non_constant_p, bool *overflow_p,
5857 : tree *jump_target)
5858 : {
5859 20671328 : location_t loc = cp_expr_loc_or_input_loc (t);
5860 20671328 : tree ary = TREE_OPERAND (t, 0);
5861 20671328 : t = TREE_OPERAND (t, 1);
5862 20671328 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5863 : non_constant_p, overflow_p,
5864 : jump_target);
5865 20671328 : if (*jump_target)
5866 : return NULL_TREE;
5867 20671328 : VERIFY_CONSTANT (index);
5868 :
5869 20471370 : if (!tree_fits_shwi_p (index)
5870 20471370 : || tree_int_cst_sgn (index) < 0)
5871 : {
5872 112 : diag_array_subscript (loc, ctx, ary, index);
5873 112 : *non_constant_p = true;
5874 112 : return t;
5875 : }
5876 :
5877 20471258 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5878 : overflow_p, jump_target);
5879 20471258 : if (*jump_target)
5880 : return NULL_TREE;
5881 20471258 : VERIFY_CONSTANT (nelts);
5882 20471195 : if (allow_one_past
5883 20471195 : ? !tree_int_cst_le (index, nelts)
5884 13827081 : : !tree_int_cst_lt (index, nelts))
5885 : {
5886 464 : diag_array_subscript (loc, ctx, ary, index);
5887 464 : *non_constant_p = true;
5888 464 : return t;
5889 : }
5890 :
5891 : return index;
5892 : }
5893 :
5894 : /* Subroutine of cxx_eval_constant_expression.
5895 : Attempt to reduce a reference to an array slot. */
5896 :
5897 : static tree
5898 15137162 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5899 : value_cat lval,
5900 : bool *non_constant_p, bool *overflow_p,
5901 : tree *jump_target)
5902 : {
5903 15137162 : tree oldary = TREE_OPERAND (t, 0);
5904 15137162 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5905 : lval,
5906 : non_constant_p, overflow_p,
5907 : jump_target);
5908 15137162 : if (*non_constant_p)
5909 : return t;
5910 14928430 : if (*jump_target)
5911 : return NULL_TREE;
5912 14928430 : if (!lval
5913 8282959 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5914 144206 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5915 15072636 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5916 144206 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5917 144206 : ary = TREE_OPERAND (ary, 0);
5918 :
5919 14928430 : tree oldidx = TREE_OPERAND (t, 1);
5920 14928430 : tree index = eval_and_check_array_index (ctx, t, lval,
5921 : non_constant_p, overflow_p,
5922 : jump_target);
5923 14928430 : if (*non_constant_p)
5924 : return t;
5925 14727902 : if (*jump_target)
5926 : return NULL_TREE;
5927 :
5928 14727902 : if (lval && ary == oldary && index == oldidx)
5929 : return t;
5930 9813149 : else if (lval == vc_discard)
5931 : return t;
5932 9813149 : else if (lval)
5933 1729119 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5934 :
5935 8084030 : unsigned len = 0, elem_nchars = 1;
5936 8084030 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5937 8084030 : if (TREE_CODE (ary) == CONSTRUCTOR)
5938 5997281 : len = CONSTRUCTOR_NELTS (ary);
5939 2086749 : else if (TREE_CODE (ary) == STRING_CST)
5940 : {
5941 1942551 : elem_nchars = (TYPE_PRECISION (elem_type)
5942 1942551 : / TYPE_PRECISION (char_type_node));
5943 1942551 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5944 : }
5945 144198 : else if (TREE_CODE (ary) == VECTOR_CST)
5946 : /* We don't create variable-length VECTOR_CSTs. */
5947 144198 : len = VECTOR_CST_NELTS (ary).to_constant ();
5948 : else
5949 : {
5950 : /* We can't do anything with other tree codes, so use
5951 : VERIFY_CONSTANT to complain and fail. */
5952 0 : VERIFY_CONSTANT (ary);
5953 0 : gcc_unreachable ();
5954 : }
5955 :
5956 8084030 : bool found;
5957 8084030 : HOST_WIDE_INT i = 0;
5958 8084030 : if (TREE_CODE (ary) == CONSTRUCTOR)
5959 : {
5960 5997281 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5961 5997281 : found = (ix >= 0);
5962 5997281 : if (found)
5963 5869989 : i = ix;
5964 : }
5965 : else
5966 : {
5967 2086749 : i = tree_to_shwi (index);
5968 2086749 : found = (i < len);
5969 : }
5970 :
5971 8084030 : if (found)
5972 : {
5973 7956287 : tree r;
5974 7956287 : if (TREE_CODE (ary) == CONSTRUCTOR)
5975 : {
5976 5869989 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5977 5869989 : if (TREE_CODE (r) == RAW_DATA_CST)
5978 : {
5979 29437 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5980 29437 : gcc_checking_assert (ridx);
5981 29437 : unsigned int off
5982 29437 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5983 29437 : r = raw_data_cst_elt (r, off);
5984 : }
5985 : }
5986 2086298 : else if (TREE_CODE (ary) == VECTOR_CST)
5987 144198 : r = VECTOR_CST_ELT (ary, i);
5988 : else
5989 1942100 : r = extract_string_elt (ary, elem_nchars, i);
5990 :
5991 2115735 : if (r)
5992 : /* Don't VERIFY_CONSTANT here. */
5993 7956287 : return r;
5994 :
5995 : /* Otherwise the element doesn't have a value yet. */
5996 : }
5997 :
5998 : /* Not found. */
5999 :
6000 127743 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
6001 90 : return build_constructor (elem_type, NULL);
6002 :
6003 127653 : if (TREE_CODE (ary) == CONSTRUCTOR
6004 127653 : && CONSTRUCTOR_NO_CLEARING (ary))
6005 : {
6006 : /* 'ary' is part of the aggregate initializer we're currently
6007 : building; if there's no initializer for this element yet,
6008 : that's an error. */
6009 51 : if (!ctx->quiet)
6010 16 : error ("accessing uninitialized array element");
6011 51 : *non_constant_p = true;
6012 51 : return t;
6013 : }
6014 :
6015 : /* If it's within the array bounds but doesn't have an explicit
6016 : initializer, it's initialized from {}. But use build_value_init
6017 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
6018 127602 : tree val;
6019 127602 : constexpr_ctx new_ctx;
6020 127602 : if (CP_AGGREGATE_TYPE_P (elem_type))
6021 : {
6022 500 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
6023 500 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
6024 : }
6025 : else
6026 127102 : val = build_value_init (elem_type, tf_warning_or_error);
6027 :
6028 : /* Create a new constructor only if we don't already have a suitable one. */
6029 127602 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
6030 128134 : && (!ctx->ctor
6031 45 : || !same_type_ignoring_top_level_qualifiers_p
6032 45 : (elem_type, TREE_TYPE (ctx->ctor))));
6033 523 : if (new_ctor)
6034 : {
6035 523 : new_ctx = *ctx;
6036 : /* We clear the object here. We used to replace it with T, but that
6037 : caused problems (101371, 108158); and anyway, T is the initializer,
6038 : not the target object. */
6039 523 : new_ctx.object = NULL_TREE;
6040 523 : new_ctx.ctor = build_constructor (elem_type, NULL);
6041 523 : ctx = &new_ctx;
6042 : }
6043 127602 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6044 : overflow_p, jump_target);
6045 127602 : if (new_ctor && t != ctx->ctor)
6046 502 : free_constructor (ctx->ctor);
6047 : return t;
6048 : }
6049 :
6050 : /* Subroutine of cxx_eval_constant_expression.
6051 : Attempt to reduce a field access of a value of class type. */
6052 :
6053 : static tree
6054 105793524 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
6055 : value_cat lval,
6056 : bool *non_constant_p, bool *overflow_p,
6057 : tree *jump_target)
6058 : {
6059 105793524 : unsigned HOST_WIDE_INT i;
6060 105793524 : tree field;
6061 105793524 : tree value;
6062 105793524 : tree part = TREE_OPERAND (t, 1);
6063 105793524 : tree orig_whole = TREE_OPERAND (t, 0);
6064 105793524 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6065 : lval,
6066 : non_constant_p, overflow_p,
6067 : jump_target);
6068 105793524 : if (*non_constant_p)
6069 : return t;
6070 84107518 : if (*jump_target)
6071 : return NULL_TREE;
6072 84107512 : if (INDIRECT_REF_P (whole)
6073 84107512 : && integer_zerop (TREE_OPERAND (whole, 0)))
6074 : {
6075 177 : if (!ctx->quiet)
6076 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6077 177 : *non_constant_p = true;
6078 177 : return t;
6079 : }
6080 :
6081 84107335 : if (TREE_CODE (whole) == PTRMEM_CST)
6082 1556 : whole = cplus_expand_constant (whole);
6083 84107335 : if (whole == orig_whole)
6084 : return t;
6085 56299864 : if (lval == vc_discard)
6086 : return t;
6087 56299840 : if (lval)
6088 19666363 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6089 : whole, part, NULL_TREE);
6090 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6091 : CONSTRUCTOR. */
6092 36633477 : if (TREE_CODE (whole) != CONSTRUCTOR)
6093 : {
6094 0 : if (!ctx->quiet)
6095 0 : error ("%qE is not a constant expression", orig_whole);
6096 0 : *non_constant_p = true;
6097 0 : return t;
6098 : }
6099 36631983 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6100 36635153 : && DECL_MUTABLE_P (part))
6101 : {
6102 144 : if (!ctx->quiet)
6103 16 : error ("mutable %qD is not usable in a constant expression", part);
6104 144 : *non_constant_p = true;
6105 144 : return t;
6106 : }
6107 :
6108 36633333 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6109 54912195 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6110 : {
6111 : /* Use name match for PMF fields, as a variant will have a
6112 : different FIELD_DECL with a different type. */
6113 54720060 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6114 : : field == part)
6115 : {
6116 36441198 : if (value == void_node)
6117 6 : goto uninit;
6118 36441192 : if (value)
6119 : {
6120 36441174 : STRIP_ANY_LOCATION_WRAPPER (value);
6121 36441174 : return value;
6122 : }
6123 : else
6124 : /* We're in the middle of initializing it. */
6125 : break;
6126 : }
6127 : }
6128 192153 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6129 : {
6130 109 : if (CONSTRUCTOR_NELTS (whole) > 0)
6131 : {
6132 : /* DR 1188 says we don't have to deal with this. */
6133 94 : if (!ctx->quiet)
6134 : {
6135 20 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6136 20 : if (cep->value == NULL_TREE)
6137 9 : error ("accessing uninitialized member %qD", part);
6138 : else
6139 11 : error ("accessing %qD member instead of active %qD member "
6140 : "in constant expression", part, cep->index);
6141 : }
6142 94 : *non_constant_p = true;
6143 94 : return t;
6144 : }
6145 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6146 : {
6147 : /* Value-initialized union, check if looking at the first member. */
6148 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6149 12 : if (first != part)
6150 : {
6151 9 : if (!ctx->quiet)
6152 3 : error ("accessing %qD member instead of initialized %qD "
6153 : "member in constant expression", part, first);
6154 9 : *non_constant_p = true;
6155 9 : return t;
6156 : }
6157 : }
6158 : }
6159 :
6160 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6161 : classes never get represented; throw together a value now. */
6162 192050 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6163 175564 : return build_constructor (TREE_TYPE (t), NULL);
6164 :
6165 16486 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6166 :
6167 16486 : if (CONSTRUCTOR_NO_CLEARING (whole))
6168 : {
6169 109 : uninit:
6170 : /* 'whole' is part of the aggregate initializer we're currently
6171 : building; if there's no initializer for this member yet, that's an
6172 : error. */
6173 115 : if (!ctx->quiet)
6174 23 : error ("accessing uninitialized member %qD", part);
6175 115 : *non_constant_p = true;
6176 115 : return t;
6177 : }
6178 :
6179 : /* If there's no explicit init for this field, it's value-initialized. */
6180 :
6181 16377 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6182 : {
6183 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6184 : and copy the flags. */
6185 15859 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6186 15859 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6187 15859 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6188 15859 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6189 15859 : return value;
6190 : }
6191 :
6192 518 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6193 518 : return cxx_eval_constant_expression (ctx, value,
6194 : lval,
6195 : non_constant_p, overflow_p,
6196 518 : jump_target);
6197 : }
6198 :
6199 : /* Subroutine of cxx_eval_constant_expression.
6200 : Attempt to reduce a field access of a value of class type that is
6201 : expressed as a BIT_FIELD_REF. */
6202 :
6203 : static tree
6204 42353 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6205 : value_cat lval,
6206 : bool *non_constant_p, bool *overflow_p,
6207 : tree *jump_target)
6208 : {
6209 42353 : tree orig_whole = TREE_OPERAND (t, 0);
6210 42353 : tree retval, fldval, utype, mask;
6211 42353 : bool fld_seen = false;
6212 42353 : HOST_WIDE_INT istart, isize;
6213 42353 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6214 : lval,
6215 : non_constant_p, overflow_p,
6216 : jump_target);
6217 42353 : tree start, field, value;
6218 42353 : unsigned HOST_WIDE_INT i;
6219 :
6220 42353 : if (*jump_target)
6221 : return NULL_TREE;
6222 42353 : if (whole == orig_whole)
6223 : return t;
6224 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6225 : CONSTRUCTOR. */
6226 42090 : if (!*non_constant_p
6227 42090 : && TREE_CODE (whole) != VECTOR_CST
6228 1498 : && TREE_CODE (whole) != CONSTRUCTOR)
6229 : {
6230 0 : if (!ctx->quiet)
6231 0 : error ("%qE is not a constant expression", orig_whole);
6232 0 : *non_constant_p = true;
6233 : }
6234 42090 : if (*non_constant_p)
6235 : return t;
6236 :
6237 42090 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6238 : {
6239 40592 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6240 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6241 : return r;
6242 0 : if (!ctx->quiet)
6243 0 : error ("%qE is not a constant expression", orig_whole);
6244 0 : *non_constant_p = true;
6245 0 : return t;
6246 : }
6247 :
6248 1498 : start = TREE_OPERAND (t, 2);
6249 1498 : istart = tree_to_shwi (start);
6250 1498 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6251 1498 : utype = TREE_TYPE (t);
6252 1498 : if (!TYPE_UNSIGNED (utype))
6253 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6254 1498 : retval = build_int_cst (utype, 0);
6255 20972 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6256 : {
6257 19474 : tree bitpos = bit_position (field);
6258 19474 : STRIP_ANY_LOCATION_WRAPPER (value);
6259 19474 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6260 : return value;
6261 19474 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6262 19474 : && TREE_CODE (value) == INTEGER_CST
6263 19474 : && tree_fits_shwi_p (bitpos)
6264 38948 : && tree_fits_shwi_p (DECL_SIZE (field)))
6265 : {
6266 19474 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6267 19474 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6268 19474 : HOST_WIDE_INT shift;
6269 19474 : if (bit >= istart && bit + sz <= istart + isize)
6270 : {
6271 4494 : fldval = fold_convert (utype, value);
6272 4494 : mask = build_int_cst_type (utype, -1);
6273 4494 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6274 : size_int (TYPE_PRECISION (utype) - sz));
6275 4494 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6276 : size_int (TYPE_PRECISION (utype) - sz));
6277 4494 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6278 4494 : shift = bit - istart;
6279 4494 : if (BYTES_BIG_ENDIAN)
6280 : shift = TYPE_PRECISION (utype) - shift - sz;
6281 4494 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6282 : size_int (shift));
6283 4494 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6284 4494 : fld_seen = true;
6285 : }
6286 : }
6287 : }
6288 1498 : if (fld_seen)
6289 1498 : return fold_convert (TREE_TYPE (t), retval);
6290 0 : gcc_unreachable ();
6291 : return error_mark_node;
6292 : }
6293 :
6294 : /* Helper for cxx_eval_bit_cast.
6295 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6296 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6297 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6298 : data members of reference type. */
6299 :
6300 : static bool
6301 54356 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6302 : tree orig_type)
6303 : {
6304 55267 : if (TREE_CODE (type) == UNION_TYPE)
6305 : {
6306 63 : if (!ctx->quiet)
6307 : {
6308 21 : if (type == orig_type)
6309 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6310 : "a union type", "__builtin_bit_cast", type);
6311 : else
6312 15 : error_at (loc, "%qs is not a constant expression because %qT "
6313 : "contains a union type", "__builtin_bit_cast",
6314 : orig_type);
6315 : }
6316 63 : return true;
6317 : }
6318 : if (TREE_CODE (type) == POINTER_TYPE)
6319 : {
6320 57 : if (!ctx->quiet)
6321 : {
6322 18 : if (type == orig_type)
6323 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6324 : "a pointer type", "__builtin_bit_cast", type);
6325 : else
6326 12 : error_at (loc, "%qs is not a constant expression because %qT "
6327 : "contains a pointer type", "__builtin_bit_cast",
6328 : orig_type);
6329 : }
6330 57 : return true;
6331 : }
6332 : if (TREE_CODE (type) == REFERENCE_TYPE)
6333 : {
6334 0 : if (!ctx->quiet)
6335 : {
6336 0 : if (type == orig_type)
6337 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6338 : "a reference type", "__builtin_bit_cast", type);
6339 : else
6340 0 : error_at (loc, "%qs is not a constant expression because %qT "
6341 : "contains a reference type", "__builtin_bit_cast",
6342 : orig_type);
6343 : }
6344 0 : return true;
6345 : }
6346 37937 : if (TYPE_PTRMEM_P (type))
6347 : {
6348 36 : if (!ctx->quiet)
6349 : {
6350 12 : if (type == orig_type)
6351 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6352 : "a pointer to member type", "__builtin_bit_cast",
6353 : type);
6354 : else
6355 0 : error_at (loc, "%qs is not a constant expression because %qT "
6356 : "contains a pointer to member type",
6357 : "__builtin_bit_cast", orig_type);
6358 : }
6359 36 : return true;
6360 : }
6361 55111 : if (TYPE_VOLATILE (type))
6362 : {
6363 0 : if (!ctx->quiet)
6364 : {
6365 0 : if (type == orig_type)
6366 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6367 : "volatile", "__builtin_bit_cast", type);
6368 : else
6369 0 : error_at (loc, "%qs is not a constant expression because %qT "
6370 : "contains a volatile subobject",
6371 : "__builtin_bit_cast", orig_type);
6372 : }
6373 0 : return true;
6374 : }
6375 55111 : if (TREE_CODE (type) == RECORD_TYPE)
6376 2215390 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6377 2177561 : if (TREE_CODE (field) == FIELD_DECL
6378 2177561 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6379 : return true;
6380 55021 : if (TREE_CODE (type) == ARRAY_TYPE)
6381 911 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6382 : return false;
6383 : }
6384 :
6385 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6386 : std::byte members of CONSTRUCTOR (recursively) if they contain
6387 : some indeterminate bits (as set in MASK), remove the ctor elts,
6388 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6389 : bits in MASK. */
6390 :
6391 : static void
6392 9631 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6393 : {
6394 9631 : if (TREE_CODE (t) != CONSTRUCTOR)
6395 : return;
6396 :
6397 : unsigned i, j = 0;
6398 : tree index, value;
6399 23564 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6400 : {
6401 13933 : tree type = TREE_TYPE (value);
6402 13933 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6403 27427 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6404 : {
6405 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6406 : {
6407 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6408 135 : gcc_assert (fldsz != 0);
6409 135 : HOST_WIDE_INT pos = int_byte_position (index);
6410 135 : HOST_WIDE_INT bpos
6411 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6412 135 : bpos %= BITS_PER_UNIT;
6413 135 : HOST_WIDE_INT end
6414 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6415 135 : gcc_assert (end == 1 || end == 2);
6416 135 : unsigned char *p = mask + pos;
6417 135 : unsigned char mask_save[2];
6418 135 : mask_save[0] = mask[pos];
6419 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6420 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6421 : sorry_at (loc, "PDP11 bit-field handling unsupported"
6422 : " in %qs", "__builtin_bit_cast");
6423 135 : else if (BYTES_BIG_ENDIAN)
6424 : {
6425 : /* Big endian. */
6426 : if (bpos + fldsz <= BITS_PER_UNIT)
6427 : *p &= ~(((1 << fldsz) - 1)
6428 : << (BITS_PER_UNIT - bpos - fldsz));
6429 : else
6430 : {
6431 : gcc_assert (bpos);
6432 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6433 : p++;
6434 : fldsz -= BITS_PER_UNIT - bpos;
6435 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6436 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6437 : }
6438 : }
6439 : else
6440 : {
6441 : /* Little endian. */
6442 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6443 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6444 : else
6445 : {
6446 0 : gcc_assert (bpos);
6447 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6448 0 : p++;
6449 0 : fldsz -= BITS_PER_UNIT - bpos;
6450 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6451 0 : *p &= ~((1 << fldsz) - 1);
6452 : }
6453 : }
6454 135 : if (mask_save[0] != mask[pos]
6455 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6456 : {
6457 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6458 36 : continue;
6459 : }
6460 : }
6461 : }
6462 13663 : else if (is_byte_access_type_not_plain_char (type))
6463 : {
6464 228 : HOST_WIDE_INT pos;
6465 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6466 132 : pos = tree_to_shwi (index);
6467 : else
6468 96 : pos = int_byte_position (index);
6469 228 : if (mask[pos])
6470 : {
6471 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6472 48 : mask[pos] = 0;
6473 48 : continue;
6474 : }
6475 : }
6476 13849 : if (TREE_CODE (value) == CONSTRUCTOR)
6477 : {
6478 7292 : HOST_WIDE_INT pos;
6479 7292 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6480 144 : pos = tree_to_shwi (index)
6481 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6482 : else
6483 7220 : pos = int_byte_position (index);
6484 7292 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6485 : }
6486 13849 : if (i != j)
6487 : {
6488 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6489 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6490 : }
6491 13849 : ++j;
6492 : }
6493 19262 : if (CONSTRUCTOR_NELTS (t) != j)
6494 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6495 : }
6496 :
6497 : /* Subroutine of cxx_eval_constant_expression.
6498 : Attempt to evaluate a BIT_CAST_EXPR. */
6499 :
6500 : static tree
6501 3844 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6502 : bool *overflow_p, tree *jump_target)
6503 : {
6504 3844 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6505 3844 : TREE_TYPE (t))
6506 12171 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6507 3763 : EXPR_LOCATION (t)),
6508 3763 : TREE_TYPE (TREE_OPERAND (t, 0)),
6509 3763 : TREE_TYPE (TREE_OPERAND (t, 0))))
6510 : {
6511 156 : *non_constant_p = true;
6512 156 : return t;
6513 : }
6514 :
6515 3688 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6516 : non_constant_p, overflow_p,
6517 : jump_target);
6518 3688 : if (*non_constant_p)
6519 : return t;
6520 2814 : if (*jump_target)
6521 : return NULL_TREE;
6522 :
6523 2814 : location_t loc = EXPR_LOCATION (t);
6524 2814 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6525 : {
6526 : if (!ctx->quiet)
6527 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6528 : "__builtin_bit_cast");
6529 : *non_constant_p = true;
6530 : return t;
6531 : }
6532 :
6533 2814 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6534 : {
6535 0 : if (!ctx->quiet)
6536 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6537 : "type is too large", "__builtin_bit_cast");
6538 0 : *non_constant_p = true;
6539 0 : return t;
6540 : }
6541 :
6542 2814 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6543 2814 : if (len < 0 || (int) len != len)
6544 : {
6545 0 : if (!ctx->quiet)
6546 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6547 : "type is too large", "__builtin_bit_cast");
6548 0 : *non_constant_p = true;
6549 0 : return t;
6550 : }
6551 :
6552 2814 : unsigned char buf[64];
6553 2814 : unsigned char *ptr, *mask;
6554 2814 : size_t alen = (size_t) len * 2;
6555 2814 : if (alen <= sizeof (buf))
6556 : ptr = buf;
6557 : else
6558 329 : ptr = XNEWVEC (unsigned char, alen);
6559 2814 : mask = ptr + (size_t) len;
6560 : /* At the beginning consider everything indeterminate. */
6561 2814 : memset (mask, ~0, (size_t) len);
6562 :
6563 2814 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6564 : {
6565 0 : if (!ctx->quiet)
6566 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6567 : "argument cannot be encoded", "__builtin_bit_cast");
6568 0 : *non_constant_p = true;
6569 0 : if (ptr != buf)
6570 0 : XDELETE (ptr);
6571 0 : return t;
6572 : }
6573 :
6574 2814 : tree r = NULL_TREE;
6575 2814 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6576 : {
6577 475 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6578 475 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6579 : {
6580 46 : gcc_assert (len == 1);
6581 46 : if (mask[0])
6582 : {
6583 24 : memset (mask, 0, len);
6584 24 : r = build_constructor (TREE_TYPE (r), NULL);
6585 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6586 : }
6587 : }
6588 : }
6589 2339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6590 : {
6591 2339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6592 2339 : if (r != NULL_TREE)
6593 : {
6594 2339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6595 2339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6596 2339 : if (CHECKING_P)
6597 : {
6598 2339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6599 : non_constant_p, overflow_p,
6600 : jump_target);
6601 2339 : gcc_checking_assert (e == r && !*jump_target);
6602 : r = e;
6603 : }
6604 : }
6605 : }
6606 :
6607 2814 : if (r != NULL_TREE)
6608 : {
6609 92029 : for (int i = 0; i < len; i++)
6610 89305 : if (mask[i])
6611 : {
6612 90 : if (!ctx->quiet)
6613 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6614 : "__builtin_bit_cast", i);
6615 90 : *non_constant_p = true;
6616 90 : r = t;
6617 90 : break;
6618 : }
6619 2814 : if (ptr != buf)
6620 329 : XDELETE (ptr);
6621 2814 : return r;
6622 : }
6623 :
6624 0 : if (!ctx->quiet)
6625 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6626 : "argument cannot be interpreted", "__builtin_bit_cast");
6627 0 : *non_constant_p = true;
6628 0 : if (ptr != buf)
6629 0 : XDELETE (ptr);
6630 : return t;
6631 : }
6632 :
6633 : /* Subroutine of cxx_eval_constant_expression.
6634 : Evaluate a short-circuited logical expression T in the context
6635 : of a given constexpr CALL. BAILOUT_VALUE is the value for
6636 : early return. CONTINUE_VALUE is used here purely for
6637 : sanity check purposes. */
6638 :
6639 : static tree
6640 17667067 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6641 : tree bailout_value, tree continue_value,
6642 : bool *non_constant_p, bool *overflow_p,
6643 : tree *jump_target)
6644 : {
6645 17667067 : tree r;
6646 17667067 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6647 : vc_prvalue, non_constant_p,
6648 : overflow_p, jump_target);
6649 17667065 : if (*jump_target)
6650 : return NULL_TREE;
6651 17667056 : VERIFY_CONSTANT (lhs);
6652 13384914 : if (tree_int_cst_equal (lhs, bailout_value))
6653 : return lhs;
6654 11472967 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6655 11472967 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6656 : vc_prvalue, non_constant_p,
6657 : overflow_p, jump_target);
6658 11472967 : if (*jump_target)
6659 : return NULL_TREE;
6660 11472967 : VERIFY_CONSTANT (r);
6661 : return r;
6662 : }
6663 :
6664 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6665 : CONSTRUCTOR elements to initialize (part of) an object containing that
6666 : field. Return a pointer to the constructor_elt corresponding to the
6667 : initialization of the field. */
6668 :
6669 : static constructor_elt *
6670 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6671 : {
6672 0 : tree aggr = TREE_OPERAND (ref, 0);
6673 0 : tree field = TREE_OPERAND (ref, 1);
6674 0 : HOST_WIDE_INT i;
6675 0 : constructor_elt *ce;
6676 :
6677 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6678 :
6679 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6680 : {
6681 0 : constructor_elt *base_ce
6682 0 : = base_field_constructor_elt (v, aggr);
6683 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6684 : }
6685 :
6686 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6687 0 : if (ce->index == field)
6688 0 : return ce;
6689 :
6690 0 : gcc_unreachable ();
6691 : return NULL;
6692 : }
6693 :
6694 : /* Some of the expressions fed to the constexpr mechanism are calls to
6695 : constructors, which have type void. In that case, return the type being
6696 : initialized by the constructor. */
6697 :
6698 : static tree
6699 590323909 : initialized_type (tree t)
6700 : {
6701 591823681 : if (TYPE_P (t))
6702 : return t;
6703 591823224 : tree type = TREE_TYPE (t);
6704 591823224 : if (TREE_CODE (t) == CALL_EXPR)
6705 : {
6706 : /* A constructor call has void type, so we need to look deeper. */
6707 72947572 : tree fn = get_function_named_in_call (t);
6708 72941105 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6709 145762929 : && DECL_CXX_CONSTRUCTOR_P (fn))
6710 4053217 : type = DECL_CONTEXT (fn);
6711 : }
6712 518875652 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6713 1499772 : return initialized_type (TREE_OPERAND (t, 1));
6714 517375880 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6715 851521 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6716 590323452 : return cv_unqualified (type);
6717 : }
6718 :
6719 : /* We're about to initialize element INDEX of an array or class from VALUE.
6720 : Set up NEW_CTX appropriately by adjusting .object to refer to the
6721 : subobject and creating a new CONSTRUCTOR if the element is itself
6722 : a class or array. */
6723 :
6724 : static void
6725 3750974 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6726 : tree index, tree &value)
6727 : {
6728 3750974 : new_ctx = *ctx;
6729 :
6730 3750974 : if (index && TREE_CODE (index) != INTEGER_CST
6731 3333157 : && TREE_CODE (index) != FIELD_DECL
6732 3 : && TREE_CODE (index) != RANGE_EXPR)
6733 : /* This won't have an element in the new CONSTRUCTOR. */
6734 : return;
6735 :
6736 3750974 : tree type = initialized_type (value);
6737 3750974 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6738 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6739 : return;
6740 :
6741 1632275 : tree ctxtype = NULL_TREE;
6742 1632275 : if (ctx->ctor)
6743 1632258 : ctxtype = TREE_TYPE (ctx->ctor);
6744 17 : else if (ctx->object)
6745 14 : ctxtype = TREE_TYPE (ctx->object);
6746 : else
6747 : {
6748 : /* This can happen if the enclosing object is also an empty subobject
6749 : (c++/125315). */
6750 3 : gcc_checking_assert (is_empty_field (index));
6751 : return;
6752 : }
6753 :
6754 1632272 : if (VECTOR_TYPE_P (type)
6755 7332 : && VECTOR_TYPE_P (ctxtype)
6756 2590 : && index == NULL_TREE)
6757 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6758 : vector is constructed from smaller vectors, doesn't get its own
6759 : CONSTRUCTOR either. */
6760 : return;
6761 :
6762 : /* The sub-aggregate initializer might contain a placeholder;
6763 : update object to refer to the subobject and ctor to refer to
6764 : the (newly created) sub-initializer. */
6765 1629682 : if (ctx->object)
6766 : {
6767 1470609 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6768 : /* There's no well-defined subobject for this index. */
6769 3 : new_ctx.object = NULL_TREE;
6770 : else
6771 1470606 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6772 : }
6773 :
6774 1629682 : if (is_empty_field (index)
6775 1629682 : && TREE_CODE (ctxtype) != UNION_TYPE)
6776 : /* Leave ctor null for an empty subobject of a non-union class, they aren't
6777 : represented in the result of evaluation. */
6778 1464143 : new_ctx.ctor = NULL_TREE;
6779 : else
6780 : {
6781 165539 : tree elt = build_constructor (type, NULL);
6782 165539 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6783 165539 : new_ctx.ctor = elt;
6784 : }
6785 :
6786 1629682 : if (TREE_CODE (value) == TARGET_EXPR)
6787 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6788 110460 : value = TARGET_EXPR_INITIAL (value);
6789 : }
6790 :
6791 : /* We're about to process an initializer for a class or array TYPE. Make
6792 : sure that CTX is set up appropriately. */
6793 :
6794 : static void
6795 2994823 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6796 : {
6797 : /* We don't bother building a ctor for an empty base subobject. */
6798 2994823 : if (is_empty_class (type))
6799 : return;
6800 :
6801 : /* We're in the middle of an initializer that might involve placeholders;
6802 : our caller should have created a CONSTRUCTOR for us to put the
6803 : initializer into. We will either return that constructor or T. */
6804 1547782 : gcc_assert (ctx->ctor);
6805 1547782 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6806 : (type, TREE_TYPE (ctx->ctor)));
6807 : /* We used to check that ctx->ctor was empty, but that isn't the case when
6808 : the object is zero-initialized before calling the constructor. */
6809 1547782 : if (ctx->object)
6810 : {
6811 1357393 : tree otype = TREE_TYPE (ctx->object);
6812 1357393 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6813 : /* Handle flexible array members. */
6814 : || (TREE_CODE (otype) == ARRAY_TYPE
6815 : && TYPE_DOMAIN (otype) == NULL_TREE
6816 : && TREE_CODE (type) == ARRAY_TYPE
6817 : && (same_type_ignoring_top_level_qualifiers_p
6818 : (TREE_TYPE (type), TREE_TYPE (otype)))));
6819 : }
6820 1547782 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6821 : || ctx->global->get_value (ctx->object) == ctx->ctor);
6822 : }
6823 :
6824 : /* Subroutine of cxx_eval_constant_expression.
6825 : The expression tree T denotes a C-style array or a C-style
6826 : aggregate. Reduce it to a constant expression. */
6827 :
6828 : static tree
6829 2994149 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6830 : value_cat lval,
6831 : bool *non_constant_p, bool *overflow_p,
6832 : tree *jump_target)
6833 : {
6834 2994149 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6835 2994149 : bool changed = false;
6836 2994149 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6837 2994149 : tree type = TREE_TYPE (t);
6838 :
6839 2994149 : constexpr_ctx new_ctx;
6840 2994149 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6841 : {
6842 : /* We don't really need the ctx->ctor business for a PMF or
6843 : vector, but it's simpler to use the same code. */
6844 41758 : new_ctx = *ctx;
6845 41758 : new_ctx.ctor = build_constructor (type, NULL);
6846 41758 : new_ctx.object = NULL_TREE;
6847 41758 : ctx = &new_ctx;
6848 2994149 : };
6849 2994149 : verify_ctor_sanity (ctx, type);
6850 2994149 : vec<constructor_elt, va_gc> **p = nullptr;
6851 2994149 : if (ctx->ctor)
6852 : {
6853 2994132 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6854 5984859 : vec_alloc (*p, vec_safe_length (v));
6855 2994132 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6856 16050 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6857 : }
6858 :
6859 2994149 : unsigned i;
6860 2994149 : tree index, value;
6861 2994149 : bool constant_p = true;
6862 2994149 : bool side_effects_p = false;
6863 6328487 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6864 : {
6865 3744767 : tree orig_value = value;
6866 3744767 : init_subob_ctx (ctx, new_ctx, index, value);
6867 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6868 3744767 : bool no_slot = new_ctx.ctor == NULL_TREE;
6869 3744767 : int pos_hint = -1;
6870 3744767 : if (!ctx->ctor)
6871 : /* The enclosing object could be an empty subobject so we have no
6872 : CONSTRUCTOR (c++/125336). */
6873 17 : gcc_checking_assert (is_empty_class (type));
6874 3744750 : else if (new_ctx.ctor != ctx->ctor && !no_slot)
6875 : {
6876 : /* If we built a new CONSTRUCTOR, attach it now so that other
6877 : initializers can refer to it. */
6878 159469 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6879 159469 : cep->value = new_ctx.ctor;
6880 159469 : pos_hint = cep - (*p)->begin();
6881 159469 : }
6882 3585281 : else if (TREE_CODE (type) == UNION_TYPE)
6883 : /* Otherwise if we're constructing a non-aggregate union member, set
6884 : the active union member now so that we can later detect and diagnose
6885 : if its initializer attempts to activate another member. */
6886 1089 : get_or_insert_ctor_field (ctx->ctor, index);
6887 3744767 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6888 : lval,
6889 : non_constant_p, overflow_p,
6890 : jump_target);
6891 3744767 : if (*jump_target)
6892 : return NULL_TREE;
6893 : /* Don't VERIFY_CONSTANT here. */
6894 3744761 : if (ctx->quiet && *non_constant_p)
6895 : break;
6896 3334338 : if (elt != orig_value)
6897 1216855 : changed = true;
6898 :
6899 3334338 : if (!TREE_CONSTANT (elt))
6900 885181 : constant_p = false;
6901 3334338 : if (TREE_SIDE_EFFECTS (elt))
6902 281 : side_effects_p = true;
6903 3334338 : if (index && TREE_CODE (index) == COMPONENT_REF)
6904 : {
6905 : /* This is an initialization of a vfield inside a base
6906 : subaggregate that we already initialized; push this
6907 : initialization into the previous initialization. */
6908 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6909 0 : inner->value = elt;
6910 0 : changed = true;
6911 0 : }
6912 3334338 : else if (no_slot)
6913 : /* This is an initializer for an empty field; now that we've
6914 : checked that it's constant, we can ignore it. */
6915 : changed = true;
6916 1870868 : else if (ctx->ctor)
6917 : {
6918 1870868 : if (TREE_CODE (type) == UNION_TYPE
6919 1870868 : && (*p)->last().index != index)
6920 : /* The initializer erroneously changed the active union member that
6921 : we're initializing. */
6922 7 : gcc_assert (*non_constant_p);
6923 : else
6924 : {
6925 : /* The initializer might have mutated the underlying CONSTRUCTOR,
6926 : so recompute the location of the target constructer_elt. */
6927 1870861 : constructor_elt *cep
6928 1870861 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6929 1870861 : cep->value = elt;
6930 : }
6931 :
6932 : /* Adding or replacing an element might change the ctor's flags. */
6933 1870868 : TREE_CONSTANT (ctx->ctor) = constant_p;
6934 1870868 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6935 : }
6936 : }
6937 2994143 : if (*non_constant_p)
6938 : return t;
6939 2583653 : if (!changed)
6940 : {
6941 255114 : if (VECTOR_TYPE_P (type))
6942 14406 : t = fold (t);
6943 255114 : return t;
6944 : }
6945 2328539 : t = ctx->ctor;
6946 2328539 : if (!t)
6947 8 : t = build_constructor (type, NULL);
6948 : /* We're done building this CONSTRUCTOR, so now we can interpret an
6949 : element without an explicit initializer as value-initialized. */
6950 2328539 : CONSTRUCTOR_NO_CLEARING (t) = false;
6951 2328539 : TREE_CONSTANT (t) = constant_p;
6952 2328539 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6953 2328539 : if (VECTOR_TYPE_P (type))
6954 10942 : t = fold (t);
6955 : return t;
6956 : }
6957 :
6958 : /* Subroutine of cxx_eval_constant_expression.
6959 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6960 : initialization of a non-static data member of array type. Reduce it to a
6961 : CONSTRUCTOR.
6962 :
6963 : Note that apart from value-initialization (when VALUE_INIT is true),
6964 : this is only intended to support value-initialization and the
6965 : initializations done by defaulted constructors for classes with
6966 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6967 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6968 : for the copy/move constructor. */
6969 :
6970 : static tree
6971 674 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6972 : bool value_init, value_cat lval,
6973 : bool *non_constant_p, bool *overflow_p,
6974 : tree *jump_target)
6975 : {
6976 674 : tree elttype = TREE_TYPE (atype);
6977 674 : verify_ctor_sanity (ctx, atype);
6978 674 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6979 674 : bool pre_init = false;
6980 674 : unsigned HOST_WIDE_INT i;
6981 674 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6982 :
6983 674 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6984 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6985 0 : non_constant_p, overflow_p, jump_target);
6986 :
6987 : /* We already checked access when building the VEC_INIT_EXPR. */
6988 674 : deferring_access_check_sentinel acs (dk_deferred);
6989 :
6990 : /* For the default constructor, build up a call to the default
6991 : constructor of the element type. We only need to handle class types
6992 : here, as for a constructor to be constexpr, all members must be
6993 : initialized, which for a defaulted default constructor means they must
6994 : be of a class type with a constexpr default constructor. */
6995 674 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6996 : /* We only do this at the lowest level. */;
6997 625 : else if (value_init)
6998 : {
6999 174 : init = build_value_init (elttype, complain);
7000 174 : pre_init = true;
7001 : }
7002 451 : else if (!init)
7003 : {
7004 341 : releasing_vec argvec;
7005 341 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7006 : &argvec, elttype, LOOKUP_NORMAL,
7007 : complain);
7008 341 : init = build_aggr_init_expr (elttype, init);
7009 341 : pre_init = true;
7010 341 : }
7011 :
7012 674 : bool zeroed_out = false;
7013 674 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
7014 : {
7015 : /* We're initializing an array object that had been zero-initialized
7016 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
7017 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
7018 : initializers we append to it. */
7019 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
7020 156 : zeroed_out = true;
7021 156 : vec_safe_truncate (*p, 0);
7022 : }
7023 :
7024 674 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
7025 : overflow_p, jump_target);
7026 674 : if (*jump_target)
7027 : return NULL_TREE;
7028 674 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
7029 6436 : for (i = 0; i < max; ++i)
7030 : {
7031 6207 : tree idx = build_int_cst (size_type_node, i);
7032 6207 : tree eltinit;
7033 6207 : bool reuse = false;
7034 6207 : constexpr_ctx new_ctx;
7035 6664 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
7036 6207 : bool no_slot = new_ctx.ctor == NULL_TREE;
7037 6207 : if (new_ctx.ctor != ctx->ctor && !no_slot)
7038 : {
7039 6070 : if (zeroed_out)
7040 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
7041 6070 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
7042 : }
7043 6207 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7044 : {
7045 : /* A multidimensional array; recurse. */
7046 181 : if (value_init || init == NULL_TREE)
7047 : {
7048 171 : eltinit = NULL_TREE;
7049 171 : reuse = i == 0;
7050 : }
7051 : else
7052 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7053 181 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
7054 : value_init, lval, non_constant_p,
7055 : overflow_p, jump_target);
7056 : }
7057 6026 : else if (pre_init)
7058 : {
7059 : /* Initializing an element using value or default initialization
7060 : we just pre-built above. */
7061 5750 : if (init == void_node)
7062 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
7063 3 : return ctx->ctor;
7064 5747 : eltinit = init;
7065 5747 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7066 : /* Clarify what object is being initialized (118285). */
7067 5611 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7068 5747 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7069 : non_constant_p, overflow_p,
7070 : jump_target);
7071 5747 : reuse = i == 0;
7072 : }
7073 : else
7074 : {
7075 : /* Copying an element. */
7076 276 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7077 276 : if (!lvalue_p (init))
7078 232 : eltinit = move (eltinit);
7079 276 : eltinit = (perform_implicit_conversion_flags
7080 276 : (elttype, eltinit, complain,
7081 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7082 269 : if (CLASS_TYPE_P (elttype)
7083 269 : && new_ctx.object
7084 522 : && !error_operand_p (eltinit))
7085 : /* Clarify what object is being initialized (118285). */
7086 244 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7087 276 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7088 : non_constant_p, overflow_p,
7089 : jump_target);
7090 : }
7091 6204 : if (*jump_target)
7092 : return NULL_TREE;
7093 6204 : if (*non_constant_p)
7094 : break;
7095 6079 : if (no_slot)
7096 : {
7097 : /* This is an initializer for an empty subobject; now that we've
7098 : checked that it's constant, we can ignore it. */
7099 0 : gcc_checking_assert (i == 0);
7100 : break;
7101 : }
7102 6079 : else if (new_ctx.ctor != ctx->ctor)
7103 : {
7104 : /* We appended this element above; update the value. */
7105 5949 : gcc_assert ((*p)->last().index == idx);
7106 5949 : (*p)->last().value = eltinit;
7107 : }
7108 : else
7109 130 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7110 : /* Reuse the result of cxx_eval_constant_expression call
7111 : from the first iteration to all others if it is a constant
7112 : initializer that doesn't require relocations. */
7113 6079 : if (reuse
7114 6079 : && max > 1
7115 6079 : && (eltinit == NULL_TREE
7116 470 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7117 470 : == null_pointer_node)))
7118 : {
7119 317 : if (new_ctx.ctor != ctx->ctor)
7120 193 : eltinit = new_ctx.ctor;
7121 317 : tree range = build2 (RANGE_EXPR, size_type_node,
7122 : build_int_cst (size_type_node, 1),
7123 317 : build_int_cst (size_type_node, max - 1));
7124 317 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7125 317 : break;
7126 : }
7127 5762 : else if (i == 0)
7128 226 : vec_safe_reserve (*p, max);
7129 : }
7130 :
7131 671 : if (!*non_constant_p)
7132 : {
7133 546 : init = ctx->ctor;
7134 546 : CONSTRUCTOR_NO_CLEARING (init) = false;
7135 : }
7136 671 : return init;
7137 : }
7138 :
7139 : static tree
7140 565 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7141 : value_cat lval,
7142 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7143 : {
7144 565 : tree atype = TREE_TYPE (t);
7145 565 : tree init = VEC_INIT_EXPR_INIT (t);
7146 565 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7147 565 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7148 : ;
7149 83 : else if (CONSTRUCTOR_NELTS (init) == 0
7150 83 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7151 : {
7152 : /* Handle {} as value-init. */
7153 : init = NULL_TREE;
7154 : value_init = true;
7155 : }
7156 : else
7157 : {
7158 : /* This is a more complicated case, like needing to loop over trailing
7159 : elements; call build_vec_init and evaluate the result. */
7160 72 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7161 72 : constexpr_ctx new_ctx = *ctx;
7162 72 : if (!ctx->object)
7163 : {
7164 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7165 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7166 57 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7167 57 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7168 57 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7169 57 : ctx->global->put_value (new_ctx.object, ctor);
7170 57 : ctx = &new_ctx;
7171 : }
7172 72 : init = expand_vec_init_expr (ctx->object, t, complain);
7173 72 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7174 : overflow_p, jump_target);
7175 : }
7176 493 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7177 : lval, non_constant_p, overflow_p, jump_target);
7178 493 : if (*non_constant_p)
7179 : return t;
7180 : else
7181 391 : return r;
7182 : }
7183 :
7184 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7185 : where the desired type is an array of unknown bounds because the variable
7186 : has had its bounds deduced since the wrapping expression was created. */
7187 :
7188 : static bool
7189 418660834 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7190 : {
7191 418660834 : while (TREE_CODE (type1) == ARRAY_TYPE
7192 144749 : && TREE_CODE (type2) == ARRAY_TYPE
7193 418660838 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7194 : {
7195 2 : type1 = TREE_TYPE (type1);
7196 2 : type2 = TREE_TYPE (type2);
7197 : }
7198 418660834 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7199 : }
7200 :
7201 : /* Try to determine the currently active union member for an expression
7202 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7203 : otherwise return NULL_TREE. */
7204 :
7205 : static tree
7206 80 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7207 : {
7208 80 : constexpr_ctx new_ctx = *ctx;
7209 80 : new_ctx.quiet = true;
7210 80 : bool non_constant_p = false, overflow_p = false;
7211 80 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7212 : &non_constant_p,
7213 : &overflow_p, jump_target);
7214 80 : if (*jump_target)
7215 : return NULL_TREE;
7216 80 : if (TREE_CODE (ctor) == CONSTRUCTOR
7217 32 : && CONSTRUCTOR_NELTS (ctor) == 1
7218 20 : && CONSTRUCTOR_ELT (ctor, 0)->index
7219 100 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7220 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7221 : return NULL_TREE;
7222 : }
7223 :
7224 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7225 :
7226 : static tree
7227 14213364 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7228 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7229 : tree *jump_target)
7230 : {
7231 23411240 : tree optype = TREE_TYPE (op);
7232 23411240 : unsigned HOST_WIDE_INT const_nunits;
7233 23411240 : if (off == 0 && similar_type_p (optype, type))
7234 : return op;
7235 14209615 : else if (cxx_dialect >= cxx26
7236 8800501 : && VAR_P (op)
7237 5102141 : && DECL_VTABLE_OR_VTT_P (op)
7238 15286 : && same_type_ignoring_top_level_qualifiers_p (type,
7239 : ptrdiff_type_node)
7240 14210947 : && POINTER_TYPE_P (strip_array_types (optype)))
7241 : {
7242 : /* We often read some virtual table elements using ptrdiff_t rather
7243 : than pointer type. */
7244 1332 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7245 : strip_array_types (optype),
7246 : op, off, empty_base,
7247 : jump_target))
7248 1332 : return fold_convert (type, ret);
7249 : }
7250 14208283 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7251 14208283 : && similar_type_p (type, TREE_TYPE (optype)))
7252 : {
7253 : /* *(foo *)&complexfoo => __real__ complexfoo */
7254 0 : if (off == 0)
7255 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7256 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7257 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7258 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7259 : }
7260 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7261 14208283 : else if (VECTOR_TYPE_P (optype)
7262 0 : && similar_type_p (type, TREE_TYPE (optype))
7263 14208283 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7264 : {
7265 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7266 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7267 0 : if (off < max_offset && off % part_width == 0)
7268 : {
7269 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7270 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7271 0 : TYPE_SIZE (type), index);
7272 : }
7273 : }
7274 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7275 14208283 : else if (TREE_CODE (optype) == ARRAY_TYPE
7276 9197876 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7277 23406159 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7278 : {
7279 9197876 : tree type_domain = TYPE_DOMAIN (optype);
7280 9197876 : tree min_val = size_zero_node;
7281 9197876 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7282 9197737 : min_val = TYPE_MIN_VALUE (type_domain);
7283 9197876 : unsigned HOST_WIDE_INT el_sz
7284 9197876 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7285 9197876 : unsigned HOST_WIDE_INT idx = off / el_sz;
7286 9197876 : unsigned HOST_WIDE_INT rem = off % el_sz;
7287 9197876 : if (tree_fits_uhwi_p (min_val))
7288 : {
7289 9197876 : tree index = size_int (idx + tree_to_uhwi (min_val));
7290 9197876 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7291 : NULL_TREE, NULL_TREE);
7292 9197876 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7293 9197876 : empty_base, jump_target);
7294 : }
7295 : }
7296 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7297 5010407 : else if (TREE_CODE (optype) == RECORD_TYPE
7298 5010407 : || TREE_CODE (optype) == UNION_TYPE)
7299 : {
7300 5006981 : if (TREE_CODE (optype) == UNION_TYPE)
7301 : /* For unions prefer the currently active member. */
7302 80 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7303 : {
7304 20 : unsigned HOST_WIDE_INT el_sz
7305 20 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7306 20 : if (off < el_sz)
7307 : {
7308 20 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7309 : op, field, NULL_TREE);
7310 20 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7311 : off, empty_base,
7312 : jump_target))
7313 : return ret;
7314 : }
7315 : }
7316 :
7317 : /* Handle conversion to "as base" type. */
7318 5006967 : if (CLASS_TYPE_P (optype)
7319 10013770 : && CLASSTYPE_AS_BASE (optype) == type)
7320 : return op;
7321 :
7322 : /* Handle conversion to an empty base class, which is represented with a
7323 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7324 : which is likely to be a waste of time (109678). */
7325 5001602 : if (is_empty_class (type)
7326 4750702 : && CLASS_TYPE_P (optype)
7327 9752298 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
7328 : {
7329 2287181 : if (empty_base)
7330 2287181 : *empty_base = true;
7331 2287181 : return op;
7332 : }
7333 :
7334 2714421 : for (tree field = TYPE_FIELDS (optype);
7335 39770104 : field; field = DECL_CHAIN (field))
7336 39747757 : if (TREE_CODE (field) == FIELD_DECL
7337 2722745 : && TREE_TYPE (field) != error_mark_node
7338 42470502 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
7339 : {
7340 2722741 : tree pos = byte_position (field);
7341 2722741 : if (!tree_fits_uhwi_p (pos))
7342 0 : continue;
7343 2722741 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
7344 2722741 : unsigned HOST_WIDE_INT el_sz;
7345 2722741 : if (DECL_FIELD_IS_BASE (field)
7346 756866 : && CLASS_TYPE_P (optype)
7347 3479607 : && CLASSTYPE_VBASECLASSES (optype))
7348 7701 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
7349 : else
7350 2715040 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7351 2722741 : if (upos <= off && off < upos + el_sz)
7352 : {
7353 2714318 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7354 : op, field, NULL_TREE);
7355 2714318 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7356 : off - upos,
7357 : empty_base,
7358 : jump_target))
7359 : return ret;
7360 : }
7361 : }
7362 : }
7363 :
7364 : return NULL_TREE;
7365 : }
7366 :
7367 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7368 : match. We want to be less strict for simple *& folding; if we have a
7369 : non-const temporary that we access through a const pointer, that should
7370 : work. We handle this here rather than change fold_indirect_ref_1
7371 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7372 : don't really make sense outside of constant expression evaluation. Also
7373 : we want to allow folding to COMPONENT_REF, which could cause trouble
7374 : with TBAA in fold_indirect_ref_1. */
7375 :
7376 : static tree
7377 179806104 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7378 : tree op0, bool *empty_base, tree *jump_target)
7379 : {
7380 179806104 : tree sub = op0;
7381 179806104 : tree subtype;
7382 :
7383 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7384 368879031 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7385 480178157 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7386 : {
7387 118384296 : if (TREE_CODE (sub) == NOP_EXPR
7388 118384296 : && REINTERPRET_CAST_P (sub))
7389 : return NULL_TREE;
7390 118384296 : sub = TREE_OPERAND (sub, 0);
7391 : }
7392 :
7393 179806104 : subtype = TREE_TYPE (sub);
7394 179806104 : if (!INDIRECT_TYPE_P (subtype))
7395 : return NULL_TREE;
7396 :
7397 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7398 : the innermost component into the offset until it would make the
7399 : offset positive, so that cxx_fold_indirect_ref_1 can identify
7400 : more folding opportunities. */
7401 191303749 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7402 11497694 : if (cxx_dialect >= cxx26)
7403 : {
7404 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7405 : to access virtual base members. */
7406 6896536 : tree nobj = obj;
7407 6896536 : while (TREE_CODE (nobj) == COMPONENT_REF
7408 6931124 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7409 34588 : nobj = TREE_OPERAND (nobj, 0);
7410 6896536 : if (nobj != obj
7411 27080 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7412 6923616 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7413 2509 : while (obj != nobj)
7414 : {
7415 1409 : tree field = TREE_OPERAND (obj, 1);
7416 1409 : tree pos = byte_position (field);
7417 1409 : off = int_const_binop (PLUS_EXPR, off, pos);
7418 1409 : obj = TREE_OPERAND (obj, 0);
7419 : }
7420 : }
7421 14223047 : while (TREE_CODE (obj) == COMPONENT_REF
7422 : /* We need to preserve union member accesses so that we can
7423 : later properly diagnose accessing the wrong member. */
7424 5978578 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7425 19997564 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7426 : {
7427 2908571 : tree field = TREE_OPERAND (obj, 1);
7428 2908571 : tree pos = byte_position (field);
7429 2908571 : if (integer_zerop (off) && integer_nonzerop (pos))
7430 : /* If the offset is already 0, keep going as long as the
7431 : component is at position 0. */
7432 : break;
7433 2725353 : off = int_const_binop (PLUS_EXPR, off, pos);
7434 2725353 : obj = TREE_OPERAND (obj, 0);
7435 : }
7436 11497694 : };
7437 :
7438 179806055 : if (TREE_CODE (sub) == ADDR_EXPR)
7439 : {
7440 80162229 : tree op = TREE_OPERAND (sub, 0);
7441 80162229 : tree optype = TREE_TYPE (op);
7442 :
7443 : /* *&CONST_DECL -> to the value of the const decl. */
7444 80162229 : if (TREE_CODE (op) == CONST_DECL)
7445 0 : return DECL_INITIAL (op);
7446 : /* *&p => p; make sure to handle *&"str"[cst] here. */
7447 80162229 : if (similar_type_p (optype, type))
7448 : {
7449 76685865 : tree fop = fold_read_from_constant_string (op);
7450 76685865 : if (fop)
7451 : return fop;
7452 : else
7453 76478190 : return op;
7454 : }
7455 : else
7456 : {
7457 3476364 : tree off = integer_zero_node;
7458 3476364 : canonicalize_obj_off (op, off);
7459 3476364 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7460 : tree_to_uhwi (off), empty_base,
7461 : jump_target);
7462 : }
7463 : }
7464 99643826 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7465 99643826 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7466 : {
7467 8438086 : tree op00 = TREE_OPERAND (sub, 0);
7468 8438086 : tree off = TREE_OPERAND (sub, 1);
7469 :
7470 8438086 : STRIP_NOPS (op00);
7471 8438086 : if (TREE_CODE (op00) == ADDR_EXPR)
7472 : {
7473 8021330 : tree obj = TREE_OPERAND (op00, 0);
7474 8021330 : canonicalize_obj_off (obj, off);
7475 8021330 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7476 : tree_to_uhwi (off), empty_base,
7477 : jump_target);
7478 : }
7479 : }
7480 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7481 91205740 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7482 91205740 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7483 : {
7484 0 : tree type_domain;
7485 0 : tree min_val = size_zero_node;
7486 0 : tree newsub
7487 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7488 : jump_target);
7489 0 : if (*jump_target)
7490 : return NULL_TREE;
7491 0 : if (newsub)
7492 : sub = newsub;
7493 : else
7494 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7495 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7496 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7497 0 : min_val = TYPE_MIN_VALUE (type_domain);
7498 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7499 0 : NULL_TREE);
7500 : }
7501 :
7502 : return NULL_TREE;
7503 : }
7504 :
7505 : static tree
7506 93719446 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7507 : value_cat lval,
7508 : bool *non_constant_p, bool *overflow_p,
7509 : tree *jump_target)
7510 : {
7511 93719446 : tree orig_op0 = TREE_OPERAND (t, 0);
7512 93719446 : bool empty_base = false;
7513 :
7514 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7515 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7516 :
7517 93719446 : if (TREE_CODE (t) == MEM_REF
7518 93719446 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7519 : {
7520 9 : gcc_assert (ctx->quiet);
7521 9 : *non_constant_p = true;
7522 9 : return t;
7523 : }
7524 :
7525 : /* First try to simplify it directly. */
7526 93719437 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7527 : orig_op0, &empty_base, jump_target);
7528 93719437 : if (*jump_target)
7529 : return NULL_TREE;
7530 93719437 : if (!r)
7531 : {
7532 : /* If that didn't work, evaluate the operand first. */
7533 90929702 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7534 : vc_prvalue, non_constant_p,
7535 : overflow_p, jump_target);
7536 90929702 : if (*jump_target)
7537 : return NULL_TREE;
7538 : /* Don't VERIFY_CONSTANT here. */
7539 90929692 : if (*non_constant_p)
7540 : return t;
7541 :
7542 73458705 : if (!lval && integer_zerop (op0))
7543 : {
7544 70 : if (!ctx->quiet)
7545 12 : error ("dereferencing a null pointer");
7546 70 : *non_constant_p = true;
7547 70 : return t;
7548 : }
7549 :
7550 73458635 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7551 : &empty_base, jump_target);
7552 73458635 : if (*jump_target)
7553 : return NULL_TREE;
7554 73458635 : if (r == NULL_TREE)
7555 : {
7556 : /* We couldn't fold to a constant value. Make sure it's not
7557 : something we should have been able to fold. */
7558 696303 : tree sub = op0;
7559 696303 : STRIP_NOPS (sub);
7560 696303 : if (TREE_CODE (sub) == ADDR_EXPR)
7561 : {
7562 1679 : gcc_assert (!similar_type_p
7563 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7564 : /* DR 1188 says we don't have to deal with this. */
7565 1679 : if (!ctx->quiet)
7566 : {
7567 9 : auto_diagnostic_group d;
7568 15 : error_at (cp_expr_loc_or_input_loc (t),
7569 : "accessing value of %qT object through a %qT "
7570 : "glvalue in a constant expression",
7571 9 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7572 9 : tree ob = build_fold_indirect_ref (sub);
7573 9 : if (DECL_P (ob))
7574 : {
7575 9 : if (DECL_ARTIFICIAL (ob))
7576 2 : inform (DECL_SOURCE_LOCATION (ob),
7577 2 : "%qT object created here", TREE_TYPE (ob));
7578 : else
7579 7 : inform (DECL_SOURCE_LOCATION (ob),
7580 : "%q#D declared here", ob);
7581 : }
7582 9 : }
7583 1679 : *non_constant_p = true;
7584 1679 : return t;
7585 : }
7586 :
7587 694624 : if (lval == vc_glvalue && op0 != orig_op0)
7588 83189 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7589 611435 : if (!lval)
7590 498118 : VERIFY_CONSTANT (t);
7591 611435 : return t;
7592 : }
7593 : }
7594 :
7595 75552067 : r = cxx_eval_constant_expression (ctx, r,
7596 : lval, non_constant_p, overflow_p,
7597 : jump_target);
7598 75552065 : if (*jump_target)
7599 : return NULL_TREE;
7600 75552065 : if (*non_constant_p)
7601 : return t;
7602 :
7603 : /* If we're pulling out the value of an empty base, just return an empty
7604 : CONSTRUCTOR. */
7605 68689507 : if (empty_base && !lval)
7606 : {
7607 18286 : r = build_constructor (TREE_TYPE (t), NULL);
7608 18286 : TREE_CONSTANT (r) = true;
7609 : }
7610 :
7611 : return r;
7612 : }
7613 :
7614 : /* Complain about R, a DECL that is accessed outside its lifetime. */
7615 :
7616 : static void
7617 37 : outside_lifetime_error (location_t loc, tree r)
7618 : {
7619 37 : auto_diagnostic_group d;
7620 37 : if (DECL_NAME (r) == heap_deleted_identifier)
7621 : {
7622 : /* Provide a more accurate message for deleted variables. */
7623 6 : error_at (loc, "use of allocated storage after deallocation "
7624 : "in a constant expression");
7625 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7626 : }
7627 : else
7628 : {
7629 31 : error_at (loc, "accessing %qE outside its lifetime", r);
7630 31 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7631 : }
7632 37 : }
7633 :
7634 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7635 : FUNDEF_P is true if we're checking a constexpr function body.
7636 : Shared between potential_constant_expression and
7637 : cxx_eval_constant_expression. */
7638 :
7639 : static void
7640 350 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7641 : {
7642 350 : auto_diagnostic_group d;
7643 350 : tree type = TREE_TYPE (r);
7644 350 : if (DECL_NAME (r) == heap_uninit_identifier
7645 350 : || DECL_NAME (r) == heap_identifier
7646 347 : || DECL_NAME (r) == heap_vec_uninit_identifier
7647 697 : || DECL_NAME (r) == heap_vec_identifier)
7648 : {
7649 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7650 : "storage is not usable in a constant expression"))
7651 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7652 3 : return;
7653 : }
7654 347 : if (DECL_NAME (r) == heap_deleted_identifier)
7655 : {
7656 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7657 : "deallocation in a constant expression"))
7658 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7659 0 : return;
7660 : }
7661 347 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7662 : "a constant expression", r))
7663 : return;
7664 : /* Avoid error cascade. */
7665 344 : if (DECL_INITIAL (r) == error_mark_node)
7666 : return;
7667 330 : if (DECL_DECLARED_CONSTEXPR_P (r))
7668 3 : inform (DECL_SOURCE_LOCATION (r),
7669 : "%qD used in its own initializer", r);
7670 327 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7671 : {
7672 225 : if (!CP_TYPE_CONST_P (type))
7673 196 : inform (DECL_SOURCE_LOCATION (r),
7674 : "%q#D is not const", r);
7675 29 : else if (CP_TYPE_VOLATILE_P (type))
7676 0 : inform (DECL_SOURCE_LOCATION (r),
7677 : "%q#D is volatile", r);
7678 29 : else if (!DECL_INITIAL (r)
7679 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7680 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7681 29 : inform (DECL_SOURCE_LOCATION (r),
7682 : "%qD was not initialized with a constant "
7683 : "expression", r);
7684 : else
7685 0 : gcc_unreachable ();
7686 : }
7687 102 : else if (TYPE_REF_P (type))
7688 9 : inform (DECL_SOURCE_LOCATION (r),
7689 : "%qD was not initialized with a constant "
7690 : "expression", r);
7691 : else
7692 : {
7693 93 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7694 93 : inform (DECL_SOURCE_LOCATION (r),
7695 : "%qD was not declared %<constexpr%>", r);
7696 : else
7697 0 : inform (DECL_SOURCE_LOCATION (r),
7698 : "%qD does not have integral or enumeration type",
7699 : r);
7700 : }
7701 350 : }
7702 :
7703 : /* Subroutine of cxx_eval_constant_expression.
7704 : Like cxx_eval_unary_expression, except for trinary expressions. */
7705 :
7706 : static tree
7707 42410 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7708 : value_cat lval,
7709 : bool *non_constant_p, bool *overflow_p,
7710 : tree *jump_target)
7711 : {
7712 42410 : int i;
7713 42410 : tree args[3];
7714 42410 : tree val;
7715 :
7716 166679 : for (i = 0; i < 3; i++)
7717 : {
7718 125256 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7719 : lval,
7720 : non_constant_p, overflow_p,
7721 : jump_target);
7722 125256 : if (*jump_target)
7723 : return NULL_TREE;
7724 125256 : VERIFY_CONSTANT (args[i]);
7725 : }
7726 :
7727 41423 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7728 : args[0], args[1], args[2]);
7729 41423 : if (val == NULL_TREE)
7730 : return t;
7731 41423 : VERIFY_CONSTANT (val);
7732 : return val;
7733 : }
7734 :
7735 : /* True if T was declared in a function declared to be constexpr, and
7736 : therefore potentially constant in C++14. */
7737 :
7738 : bool
7739 88780081 : var_in_constexpr_fn (tree t)
7740 : {
7741 88780081 : tree ctx = DECL_CONTEXT (t);
7742 88780081 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7743 169112947 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7744 : }
7745 :
7746 : /* True if a function might be constexpr: either a function that was
7747 : declared constexpr, or a C++17 lambda op(). */
7748 :
7749 : bool
7750 657861625 : maybe_constexpr_fn (tree t)
7751 : {
7752 657861625 : return (DECL_DECLARED_CONSTEXPR_P (t)
7753 192919037 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7754 842953502 : || (flag_implicit_constexpr
7755 94 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7756 : }
7757 :
7758 : /* True if T was declared in a function that might be constexpr: either a
7759 : function that was declared constexpr, or a C++17 lambda op(). */
7760 :
7761 : bool
7762 63538208 : var_in_maybe_constexpr_fn (tree t)
7763 : {
7764 127075797 : return (DECL_FUNCTION_SCOPE_P (t)
7765 115773311 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7766 : }
7767 :
7768 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7769 : build_over_call we implement trivial copy of a class with tail padding using
7770 : assignment of character arrays, which is valid in normal code, but not in
7771 : constexpr evaluation. We don't need to worry about clobbering tail padding
7772 : in constexpr evaluation, so strip the type punning. */
7773 :
7774 : static void
7775 97752631 : maybe_simplify_trivial_copy (tree &target, tree &init)
7776 : {
7777 97752631 : if (TREE_CODE (target) == MEM_REF
7778 35999 : && TREE_CODE (init) == MEM_REF
7779 35835 : && TREE_TYPE (target) == TREE_TYPE (init)
7780 35835 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7781 97788466 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7782 : {
7783 35835 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7784 35835 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7785 : }
7786 97752631 : }
7787 :
7788 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7789 : of constant type. This does not check for 'mutable', so the
7790 : caller is expected to be mindful of that. */
7791 :
7792 : static bool
7793 419 : cref_has_const_field (tree ref)
7794 : {
7795 488 : while (TREE_CODE (ref) == COMPONENT_REF)
7796 : {
7797 479 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7798 : return true;
7799 69 : ref = TREE_OPERAND (ref, 0);
7800 : }
7801 : return false;
7802 : }
7803 :
7804 : /* Return true if we are modifying something that is const during constant
7805 : expression evaluation. CODE is the code of the statement, OBJ is the
7806 : object in question, MUTABLE_P is true if one of the subobjects were
7807 : declared mutable. */
7808 :
7809 : static bool
7810 101874726 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7811 : {
7812 : /* If this is initialization, there's no problem. */
7813 101874726 : if (code != MODIFY_EXPR)
7814 : return false;
7815 :
7816 : /* [basic.type.qualifier] "A const object is an object of type
7817 : const T or a non-mutable subobject of a const object." */
7818 29285390 : if (mutable_p)
7819 : return false;
7820 :
7821 29284654 : if (TREE_READONLY (obj))
7822 : return true;
7823 :
7824 29230497 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7825 : {
7826 : /* Although a COMPONENT_REF may have a const type, we should
7827 : only consider it modifying a const object when any of the
7828 : field components is const. This can happen when using
7829 : constructs such as const_cast<const T &>(m), making something
7830 : const even though it wasn't declared const. */
7831 33052 : if (TREE_CODE (obj) == COMPONENT_REF)
7832 419 : return cref_has_const_field (obj);
7833 : else
7834 : return true;
7835 : }
7836 :
7837 : return false;
7838 : }
7839 :
7840 : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7841 :
7842 : static tree
7843 97752631 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7844 : value_cat lval,
7845 : bool *non_constant_p, bool *overflow_p,
7846 : tree *jump_target)
7847 : {
7848 97752631 : constexpr_ctx new_ctx = *ctx;
7849 :
7850 97752631 : tree init = TREE_OPERAND (t, 1);
7851 :
7852 : /* First we figure out where we're storing to. */
7853 97752631 : tree target = TREE_OPERAND (t, 0);
7854 :
7855 97752631 : maybe_simplify_trivial_copy (target, init);
7856 :
7857 97752631 : tree type = TREE_TYPE (target);
7858 97752631 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7859 69141759 : if (preeval && !TREE_CLOBBER_P (init))
7860 : {
7861 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7862 68097242 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7863 39413792 : && TREE_CODE (init) == CALL_EXPR
7864 6415407 : && CALL_EXPR_FN (init) == NULL_TREE
7865 68097250 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7866 8 : return void_node;
7867 :
7868 : /* Evaluate the value to be stored without knowing what object it will be
7869 : stored in, so that any side-effects happen first. */
7870 68097234 : if (!SCALAR_TYPE_P (type))
7871 199172 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7872 68097234 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7873 : non_constant_p, overflow_p,
7874 : jump_target);
7875 68097230 : if (*jump_target)
7876 : return NULL_TREE;
7877 68096992 : if (*non_constant_p)
7878 : return t;
7879 : }
7880 :
7881 89507440 : bool evaluated = false;
7882 89507440 : if (lval == vc_glvalue)
7883 : {
7884 : /* If we want to return a reference to the target, we need to evaluate it
7885 : as a whole; otherwise, only evaluate the innermost piece to avoid
7886 : building up unnecessary *_REFs. */
7887 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7888 : non_constant_p, overflow_p,
7889 : jump_target);
7890 0 : evaluated = true;
7891 0 : if (*jump_target)
7892 : return NULL_TREE;
7893 0 : if (*non_constant_p)
7894 : return t;
7895 : }
7896 :
7897 : /* Find the underlying variable. */
7898 89507440 : releasing_vec refs;
7899 89507440 : tree object = NULL_TREE;
7900 : /* If we're modifying a const object, save it. */
7901 89507440 : tree const_object_being_modified = NULL_TREE;
7902 89507440 : bool mutable_p = false;
7903 : /* If we see a union, we can't ignore clobbers. */
7904 89507440 : int seen_union = 0;
7905 303169522 : for (tree probe = target; object == NULL_TREE; )
7906 : {
7907 213662466 : switch (TREE_CODE (probe))
7908 : {
7909 34647853 : case BIT_FIELD_REF:
7910 34647853 : case COMPONENT_REF:
7911 34647853 : case ARRAY_REF:
7912 34647853 : {
7913 34647853 : tree ob = TREE_OPERAND (probe, 0);
7914 34647853 : tree elt = TREE_OPERAND (probe, 1);
7915 34647853 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7916 : mutable_p = true;
7917 34647853 : if (TREE_CODE (probe) == ARRAY_REF)
7918 : {
7919 5742898 : elt = eval_and_check_array_index (ctx, probe, false,
7920 : non_constant_p, overflow_p,
7921 : jump_target);
7922 5742898 : if (*jump_target)
7923 69 : return NULL_TREE;
7924 5742898 : if (*non_constant_p)
7925 : return t;
7926 : }
7927 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7928 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7929 : the array isn't const. Instead, check "a" in the next iteration;
7930 : that will detect modifying "const int a[10]". */
7931 28904955 : else if (evaluated
7932 12367670 : && modifying_const_object_p (TREE_CODE (t), probe,
7933 : mutable_p)
7934 28905569 : && const_object_being_modified == NULL_TREE)
7935 : const_object_being_modified = probe;
7936 :
7937 : /* Track named member accesses for unions to validate modifications
7938 : that change active member. */
7939 34647784 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7940 16537285 : vec_safe_push (refs, probe);
7941 : else
7942 18110499 : vec_safe_push (refs, NULL_TREE);
7943 :
7944 34647784 : vec_safe_push (refs, elt);
7945 34647784 : vec_safe_push (refs, TREE_TYPE (probe));
7946 34647784 : probe = ob;
7947 34647784 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
7948 566653 : ++seen_union;
7949 : }
7950 34647784 : break;
7951 :
7952 24 : case REALPART_EXPR:
7953 24 : gcc_assert (refs->is_empty ());
7954 24 : vec_safe_push (refs, NULL_TREE);
7955 24 : vec_safe_push (refs, probe);
7956 24 : vec_safe_push (refs, TREE_TYPE (probe));
7957 24 : probe = TREE_OPERAND (probe, 0);
7958 24 : break;
7959 :
7960 25 : case IMAGPART_EXPR:
7961 25 : gcc_assert (refs->is_empty ());
7962 25 : vec_safe_push (refs, NULL_TREE);
7963 25 : vec_safe_push (refs, probe);
7964 25 : vec_safe_push (refs, TREE_TYPE (probe));
7965 25 : probe = TREE_OPERAND (probe, 0);
7966 25 : break;
7967 :
7968 179014564 : default:
7969 179014564 : if (evaluated)
7970 : object = probe;
7971 : else
7972 : {
7973 89507508 : tree pvar = tree_strip_any_location_wrapper (probe);
7974 89507508 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7975 : {
7976 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7977 : active union member. */
7978 101 : probe = DECL_VALUE_EXPR (pvar);
7979 101 : break;
7980 : }
7981 89507407 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7982 : non_constant_p, overflow_p,
7983 : jump_target);
7984 89507407 : evaluated = true;
7985 89507407 : if (*jump_target)
7986 : return NULL_TREE;
7987 89507407 : if (*non_constant_p)
7988 : return t;
7989 : }
7990 : break;
7991 : }
7992 : }
7993 :
7994 89507056 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7995 89507056 : && const_object_being_modified == NULL_TREE)
7996 89507056 : const_object_being_modified = object;
7997 :
7998 89507056 : if (DECL_P (object)
7999 89505171 : && TREE_CLOBBER_P (init)
8000 90567853 : && DECL_NAME (object) == heap_deleted_identifier)
8001 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
8002 : message later when operator delete is called. */
8003 15 : return void_node;
8004 :
8005 : /* And then find/build up our initializer for the path to the subobject
8006 : we're initializing. */
8007 89507041 : tree *valp;
8008 89507041 : if (DECL_P (object))
8009 89505156 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8010 : else
8011 1885 : valp = NULL;
8012 89507041 : if (!valp)
8013 : {
8014 : /* A constant-expression cannot modify objects from outside the
8015 : constant-expression. */
8016 8007 : if (!ctx->quiet)
8017 : {
8018 29 : auto_diagnostic_group d;
8019 29 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
8020 : {
8021 0 : error ("modification of allocated storage after deallocation "
8022 : "is not a constant expression");
8023 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
8024 : }
8025 29 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
8026 : {
8027 14 : if (TREE_CLOBBER_P (init))
8028 6 : error ("destroying %qE outside its lifetime", object);
8029 : else
8030 8 : error ("modification of %qE outside its lifetime "
8031 : "is not a constant expression", object);
8032 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
8033 : }
8034 : else
8035 : {
8036 15 : if (TREE_CLOBBER_P (init))
8037 6 : error ("destroying %qE from outside current evaluation "
8038 : "is not a constant expression", object);
8039 : else
8040 9 : error ("modification of %qE from outside current evaluation "
8041 : "is not a constant expression", object);
8042 : }
8043 29 : }
8044 8007 : *non_constant_p = true;
8045 8007 : return t;
8046 : }
8047 :
8048 : /* Handle explicit end-of-lifetime. */
8049 89499034 : if (TREE_CLOBBER_P (init))
8050 : {
8051 1060732 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
8052 1060732 : && refs->is_empty ())
8053 : {
8054 298131 : ctx->global->destroy_value (object);
8055 298131 : return void_node;
8056 : }
8057 :
8058 749993 : if (!seen_union && !*valp
8059 766926 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
8060 4284 : return void_node;
8061 :
8062 : /* Ending the lifetime of a const object is OK. */
8063 : const_object_being_modified = NULL_TREE;
8064 : }
8065 :
8066 89196619 : type = TREE_TYPE (object);
8067 89196619 : bool no_zero_init = true;
8068 89196619 : bool zero_padding_bits = false;
8069 :
8070 178393238 : auto_vec<tree *> ctors;
8071 178393238 : releasing_vec indexes;
8072 178393238 : auto_vec<int> index_pos_hints;
8073 89196619 : bool activated_union_member_p = false;
8074 89196619 : bool empty_base = false;
8075 123540221 : while (!refs->is_empty ())
8076 : {
8077 34625513 : if (*valp == NULL_TREE)
8078 : {
8079 2773449 : *valp = build_constructor (type, NULL);
8080 2773449 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8081 2773449 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8082 : }
8083 31852064 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8084 31852064 : TREE_CODE (*valp) == STRING_CST)
8085 : {
8086 : /* An array was initialized with a string constant, and now
8087 : we're writing into one of its elements. Explode the
8088 : single initialization into a set of element
8089 : initializations. */
8090 11364 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8091 :
8092 11364 : tree string = *valp;
8093 11364 : tree elt_type = TREE_TYPE (type);
8094 11364 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8095 11364 : / TYPE_PRECISION (char_type_node));
8096 11364 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8097 11364 : tree ary_ctor = build_constructor (type, NULL);
8098 :
8099 11364 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8100 22882 : for (unsigned ix = 0; ix != num_elts; ix++)
8101 : {
8102 11518 : constructor_elt elt =
8103 : {
8104 11518 : build_int_cst (size_type_node, ix),
8105 11518 : extract_string_elt (string, chars_per_elt, ix)
8106 11518 : };
8107 11518 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8108 : }
8109 :
8110 11364 : *valp = ary_ctor;
8111 : }
8112 :
8113 34625513 : enum tree_code code = TREE_CODE (type);
8114 34625513 : tree reftype = refs->pop();
8115 34625513 : tree index = refs->pop();
8116 34625513 : bool is_access_expr = refs->pop() != NULL_TREE;
8117 :
8118 34625513 : if (code == COMPLEX_TYPE)
8119 : {
8120 49 : if (TREE_CODE (*valp) == COMPLEX_CST)
8121 45 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8122 45 : TREE_IMAGPART (*valp));
8123 4 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8124 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8125 6 : && CONSTRUCTOR_NO_CLEARING (*valp))
8126 : {
8127 2 : tree r = build_constructor (reftype, NULL);
8128 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8129 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8130 : }
8131 49 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8132 49 : ctors.safe_push (valp);
8133 49 : vec_safe_push (indexes, index);
8134 49 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8135 49 : gcc_checking_assert (refs->is_empty ());
8136 : type = reftype;
8137 278208 : break;
8138 : }
8139 :
8140 : /* If the value of object is already zero-initialized, any new ctors for
8141 : subobjects will also be zero-initialized. Similarly with zeroing of
8142 : padding bits. */
8143 34625464 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8144 34625464 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8145 :
8146 34625464 : if (code == RECORD_TYPE && is_empty_field (index))
8147 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8148 : have no data and might have an offset lower than previously declared
8149 : fields, which confuses the middle-end. The code below will notice
8150 : that we don't have a CONSTRUCTOR for our inner target and just
8151 : return init. */
8152 : {
8153 : empty_base = true;
8154 : break;
8155 : }
8156 :
8157 : /* If a union is zero-initialized, its first non-static named data member
8158 : is zero-initialized (and therefore active). */
8159 34347305 : if (code == UNION_TYPE
8160 34347305 : && !no_zero_init
8161 34347305 : && CONSTRUCTOR_NELTS (*valp) == 0)
8162 4059 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8163 4059 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8164 :
8165 : /* Check for implicit change of active member for a union. */
8166 :
8167 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8168 : now allow initializing an array element to activate the array. */
8169 34398771 : auto only_array_refs = [](const releasing_vec &refs)
8170 : {
8171 51471 : for (unsigned i = 1; i < refs->length(); i += 3)
8172 35 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8173 : return false;
8174 : return true;
8175 : };
8176 :
8177 34347305 : if (code == UNION_TYPE
8178 565141 : && (CONSTRUCTOR_NELTS (*valp) == 0
8179 374738 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8180 : /* An INIT_EXPR of the last member in an access chain is always OK,
8181 : but still check implicit change of members earlier on; see
8182 : cpp2a/constexpr-union6.C. */
8183 34549811 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8184 : {
8185 151070 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8186 151070 : tree inner = strip_array_types (reftype);
8187 :
8188 151070 : if (has_active_member && cxx_dialect < cxx20)
8189 : {
8190 58 : if (!ctx->quiet)
8191 19 : error_at (cp_expr_loc_or_input_loc (t),
8192 : "change of the active member of a union "
8193 : "from %qD to %qD is not a constant expression "
8194 : "before C++20",
8195 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8196 : index);
8197 58 : *non_constant_p = true;
8198 : }
8199 151012 : else if (!is_access_expr
8200 45243 : || (TREE_CLOBBER_P (init)
8201 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8202 196255 : || (TREE_CODE (t) == MODIFY_EXPR
8203 45231 : && CLASS_TYPE_P (inner)
8204 19 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8205 : {
8206 : /* Diagnose changing active union member after initialization
8207 : without a valid member access expression, as described in
8208 : [class.union.general] p5. */
8209 105778 : if (!ctx->quiet)
8210 : {
8211 33 : auto_diagnostic_group d;
8212 33 : if (has_active_member)
8213 24 : error_at (cp_expr_loc_or_input_loc (t),
8214 : "accessing %qD member instead of initialized "
8215 : "%qD member in constant expression",
8216 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8217 : else
8218 9 : error_at (cp_expr_loc_or_input_loc (t),
8219 : "accessing uninitialized member %qD",
8220 : index);
8221 33 : if (is_access_expr)
8222 3 : inform (DECL_SOURCE_LOCATION (index),
8223 : "%qD does not implicitly begin its lifetime "
8224 : "because %qT does not have a non-deleted "
8225 : "trivial default constructor, use "
8226 : "%<std::construct_at%> instead",
8227 : index, inner);
8228 : else
8229 30 : inform (DECL_SOURCE_LOCATION (index),
8230 : "initializing %qD requires a member access "
8231 : "expression as the left operand of the assignment",
8232 : index);
8233 33 : }
8234 105778 : *non_constant_p = true;
8235 : }
8236 45234 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8237 : {
8238 : /* Diagnose changing the active union member while the union
8239 : is in the process of being initialized. */
8240 18 : if (!ctx->quiet)
8241 6 : error_at (cp_expr_loc_or_input_loc (t),
8242 : "change of the active member of a union "
8243 : "from %qD to %qD during initialization",
8244 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8245 : index);
8246 18 : *non_constant_p = true;
8247 : }
8248 : no_zero_init = true;
8249 : }
8250 :
8251 : /* Ending the lifetime of the active union member means the union no
8252 : longer has an active member. */
8253 565141 : if (code == UNION_TYPE && refs->is_empty ()
8254 97312 : && TREE_CLOBBER_P (init)
8255 34355171 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8256 : {
8257 1974 : vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
8258 3703 : return void_node;
8259 : }
8260 :
8261 34345331 : ctors.safe_push (valp);
8262 34345331 : vec_safe_push (indexes, index);
8263 :
8264 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8265 : uninitialized. */
8266 32476129 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8267 1754054 : && CONSTRUCTOR_NO_CLEARING (*valp)
8268 35908151 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8269 34345331 : constructor_elt *cep
8270 34345331 : = get_or_insert_ctor_field (*valp, index, pos);
8271 34345331 : if (cep == nullptr)
8272 1729 : return void_node;
8273 34343602 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8274 :
8275 34343602 : if (code == UNION_TYPE)
8276 : {
8277 563167 : activated_union_member_p = true;
8278 563167 : --seen_union;
8279 : }
8280 :
8281 34343602 : valp = &cep->value;
8282 34343602 : type = reftype;
8283 : }
8284 :
8285 : /* Change an "as-base" clobber to the real type;
8286 : we don't need to worry about padding in constexpr. */
8287 89192916 : tree itype = initialized_type (init);
8288 89192916 : if (IS_FAKE_BASE_TYPE (itype))
8289 4971 : itype = TYPE_CONTEXT (itype);
8290 :
8291 : /* For initialization of an empty base, the original target will be
8292 : *(base*)this, evaluation of which resolves to the object
8293 : argument, which has the derived type rather than the base type. */
8294 178107673 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8295 88914757 : (itype, type)))
8296 : {
8297 13543 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8298 : empty_base = true;
8299 : }
8300 :
8301 : /* Detect modifying a constant object in constexpr evaluation.
8302 : We have found a const object that is being modified. Figure out
8303 : if we need to issue an error. Consider
8304 :
8305 : struct A {
8306 : int n;
8307 : constexpr A() : n(1) { n = 2; } // #1
8308 : };
8309 : struct B {
8310 : const A a;
8311 : constexpr B() { a.n = 3; } // #2
8312 : };
8313 : constexpr B b{};
8314 :
8315 : #1 is OK, since we're modifying an object under construction, but
8316 : #2 is wrong, since "a" is const and has been fully constructed.
8317 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8318 : which means that the object is read-only. For the example above, the
8319 : *ctors stack at the point of #2 will look like:
8320 :
8321 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
8322 : ctors[1] = {.n=2} TREE_READONLY = 1
8323 :
8324 : and we're modifying "b.a", so we search the stack and see if the
8325 : constructor for "b.a" has already run. */
8326 89192916 : if (const_object_being_modified)
8327 : {
8328 85722 : bool fail = false;
8329 85722 : tree const_objtype
8330 85722 : = strip_array_types (TREE_TYPE (const_object_being_modified));
8331 85722 : if (!CLASS_TYPE_P (const_objtype))
8332 : fail = true;
8333 : else
8334 : {
8335 : /* [class.ctor]p5 "A constructor can be invoked for a const,
8336 : volatile, or const volatile object. const and volatile
8337 : semantics are not applied on an object under construction.
8338 : They come into effect when the constructor for the most
8339 : derived object ends." */
8340 257365 : for (tree *elt : ctors)
8341 86131 : if (same_type_ignoring_top_level_qualifiers_p
8342 86131 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
8343 : {
8344 85617 : fail = TREE_READONLY (*elt);
8345 85617 : break;
8346 : }
8347 : }
8348 85617 : if (fail)
8349 : {
8350 198 : if (!ctx->quiet)
8351 63 : modifying_const_object_error (t, const_object_being_modified);
8352 198 : *non_constant_p = true;
8353 198 : return t;
8354 : }
8355 : }
8356 :
8357 89192718 : if (!preeval)
8358 : {
8359 : /* We're handling an INIT_EXPR of class type, so the value of the
8360 : initializer can depend on the object it's initializing. */
8361 :
8362 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
8363 : wants to modify it. */
8364 28599300 : if (*valp == NULL_TREE)
8365 : {
8366 27581537 : *valp = build_constructor (type, NULL);
8367 27581537 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8368 27581537 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8369 : }
8370 28599300 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8371 28599300 : new_ctx.object = target;
8372 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8373 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8374 : expansion of those trees uses ctx instead. */
8375 28599300 : if (TREE_CODE (init) == TARGET_EXPR)
8376 3074375 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8377 3074375 : init = tinit;
8378 28599300 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8379 : non_constant_p, overflow_p,
8380 : jump_target);
8381 28599300 : if (*jump_target)
8382 : return NULL_TREE;
8383 : /* The hash table might have moved since the get earlier, and the
8384 : initializer might have mutated the underlying CONSTRUCTORs, so we must
8385 : recompute VALP. */
8386 28599158 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8387 32816170 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8388 : {
8389 4217012 : ctors[i] = valp;
8390 4217012 : constructor_elt *cep
8391 4217012 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8392 4217012 : valp = &cep->value;
8393 : }
8394 : }
8395 :
8396 89192576 : if (*non_constant_p)
8397 : return t;
8398 :
8399 : /* Don't share a CONSTRUCTOR that might be changed later. */
8400 87581418 : init = unshare_constructor (init);
8401 :
8402 87581418 : gcc_checking_assert (!*valp
8403 : || *valp == void_node
8404 : || (same_type_ignoring_top_level_qualifiers_p
8405 : (TREE_TYPE (*valp), type)));
8406 87581418 : if (empty_base)
8407 : {
8408 : /* Just evaluate the initializer and return, since there's no actual data
8409 : to store, and we didn't build a CONSTRUCTOR. */
8410 284016 : if (!*valp)
8411 : {
8412 : /* But do make sure we have something in *valp. */
8413 0 : *valp = build_constructor (type, nullptr);
8414 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8415 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8416 : }
8417 : }
8418 87297402 : else if (TREE_CLOBBER_P (init))
8419 : {
8420 754614 : if (AGGREGATE_TYPE_P (type))
8421 : {
8422 535148 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
8423 535105 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8424 : else
8425 43 : *valp = build_constructor (type, nullptr);
8426 535148 : TREE_CONSTANT (*valp) = true;
8427 535148 : TREE_SIDE_EFFECTS (*valp) = false;
8428 535148 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8429 535148 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8430 : }
8431 : else
8432 219466 : *valp = void_node;
8433 : }
8434 86542788 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8435 26978561 : && TREE_CODE (init) == CONSTRUCTOR)
8436 : {
8437 : /* An outer ctx->ctor might be pointing to *valp, so replace
8438 : its contents. */
8439 4499452 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8440 4499452 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8441 4499452 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8442 13498356 : CONSTRUCTOR_NO_CLEARING (*valp)
8443 4499452 : = CONSTRUCTOR_NO_CLEARING (init);
8444 13498356 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8445 4499452 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8446 : }
8447 : else
8448 82043336 : *valp = init;
8449 :
8450 : /* After initialization, 'const' semantics apply to the value of the
8451 : object. Make a note of this fact by marking the CONSTRUCTOR
8452 : TREE_READONLY. */
8453 87581418 : if (TREE_CODE (t) == INIT_EXPR
8454 65656030 : && !empty_base
8455 65372014 : && TREE_CODE (*valp) == CONSTRUCTOR
8456 91912160 : && TYPE_READONLY (type))
8457 : {
8458 34702 : tree target_type = TREE_TYPE (target);
8459 34702 : if (IS_FAKE_BASE_TYPE (target_type))
8460 0 : target_type = TYPE_CONTEXT (target_type);
8461 34702 : if (INDIRECT_REF_P (target)
8462 49774 : && (is_this_parameter
8463 15072 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8464 : /* We've just initialized '*this' (perhaps via the target
8465 : constructor of a delegating constructor). Leave it up to the
8466 : caller that set 'this' to set TREE_READONLY appropriately. */
8467 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8468 : (target_type, type) || empty_base);
8469 : else
8470 34679 : TREE_READONLY (*valp) = true;
8471 : }
8472 :
8473 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8474 : CONSTRUCTORs, if any. */
8475 87581418 : bool c = TREE_CONSTANT (init);
8476 87581418 : bool s = TREE_SIDE_EFFECTS (init);
8477 87581418 : if (!indexes->is_empty ())
8478 : {
8479 19397996 : tree last = indexes->last ();
8480 19397996 : if (TREE_CODE (last) == REALPART_EXPR
8481 19397996 : || TREE_CODE (last) == IMAGPART_EXPR)
8482 : {
8483 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8484 : possible. */
8485 49 : tree *cexpr = ctors.last ();
8486 49 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8487 49 : TREE_OPERAND (*cexpr, 0),
8488 49 : TREE_OPERAND (*cexpr, 1)))
8489 47 : *cexpr = c;
8490 : else
8491 : {
8492 4 : TREE_CONSTANT (*cexpr)
8493 2 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8494 2 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8495 4 : TREE_SIDE_EFFECTS (*cexpr)
8496 4 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8497 2 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8498 : }
8499 49 : c = TREE_CONSTANT (*cexpr);
8500 49 : s = TREE_SIDE_EFFECTS (*cexpr);
8501 : }
8502 : }
8503 87581418 : if (!c || s || activated_union_member_p)
8504 58377959 : for (tree *elt : ctors)
8505 : {
8506 11507951 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8507 0 : continue;
8508 11507951 : if (!c)
8509 9639137 : TREE_CONSTANT (*elt) = false;
8510 11507951 : if (s)
8511 0 : TREE_SIDE_EFFECTS (*elt) = true;
8512 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8513 : this union. */
8514 11507951 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8515 457127 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8516 : }
8517 :
8518 87581418 : if (lval)
8519 : return target;
8520 : else
8521 584865 : return init;
8522 89507440 : }
8523 :
8524 : /* Evaluate a ++ or -- expression. */
8525 :
8526 : static tree
8527 8435439 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8528 : value_cat lval,
8529 : bool *non_constant_p, bool *overflow_p,
8530 : tree *jump_target)
8531 : {
8532 8435439 : enum tree_code code = TREE_CODE (t);
8533 8435439 : tree type = TREE_TYPE (t);
8534 8435439 : tree op = TREE_OPERAND (t, 0);
8535 8435439 : tree offset = TREE_OPERAND (t, 1);
8536 8435439 : gcc_assert (TREE_CONSTANT (offset));
8537 :
8538 : /* OFFSET is constant, but perhaps not constant enough. We need to
8539 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8540 8435439 : offset = fold_simple (offset);
8541 :
8542 : /* The operand as an lvalue. */
8543 8435439 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8544 : non_constant_p, overflow_p,
8545 : jump_target);
8546 8435439 : if (*jump_target)
8547 : return NULL_TREE;
8548 :
8549 : /* The operand as an rvalue. */
8550 8435439 : tree val
8551 8435439 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8552 : non_constant_p, overflow_p,
8553 : jump_target);
8554 8435439 : if (*jump_target)
8555 : return NULL_TREE;
8556 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8557 : a local array in a constexpr function. */
8558 8435439 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8559 3382048 : if (!ptr)
8560 3382048 : VERIFY_CONSTANT (val);
8561 :
8562 : /* The modified value. */
8563 8376802 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8564 8376802 : tree mod;
8565 8376802 : if (INDIRECT_TYPE_P (type))
8566 : {
8567 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8568 5053391 : offset = convert_to_ptrofftype (offset);
8569 5053391 : if (!inc)
8570 63003 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8571 5053391 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8572 : }
8573 3323411 : else if (c_promoting_integer_type_p (type)
8574 39607 : && !TYPE_UNSIGNED (type)
8575 3323620 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8576 : {
8577 209 : offset = fold_convert (integer_type_node, offset);
8578 209 : mod = fold_convert (integer_type_node, val);
8579 239 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8580 : mod, offset);
8581 209 : mod = fold_convert (type, t);
8582 209 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8583 9 : TREE_OVERFLOW (mod) = false;
8584 : }
8585 : else
8586 3739761 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8587 8376802 : if (!ptr)
8588 3323411 : VERIFY_CONSTANT (mod);
8589 :
8590 : /* Storing the modified value. */
8591 13956547 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8592 : MODIFY_EXPR, type, op, mod);
8593 8376802 : mod = cxx_eval_constant_expression (ctx, store, lval,
8594 : non_constant_p, overflow_p,
8595 : jump_target);
8596 8376802 : ggc_free (store);
8597 8376802 : if (*jump_target)
8598 : return NULL_TREE;
8599 8376802 : if (*non_constant_p)
8600 : return t;
8601 :
8602 : /* And the value of the expression. */
8603 8329125 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8604 : /* Prefix ops are lvalues, but the caller might want an rvalue;
8605 : lval has already been taken into account in the store above. */
8606 : return mod;
8607 : else
8608 : /* Postfix ops are rvalues. */
8609 410944 : return val;
8610 : }
8611 :
8612 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8613 : STMT matches *jump_target. If we're looking for a case label and we see
8614 : the default label, note it in ctx->css_state. */
8615 :
8616 : static bool
8617 504896 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8618 : {
8619 504896 : switch (TREE_CODE (*jump_target))
8620 : {
8621 45 : case LABEL_DECL:
8622 45 : if (TREE_CODE (stmt) == LABEL_EXPR
8623 45 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8624 : return true;
8625 : break;
8626 :
8627 502934 : case INTEGER_CST:
8628 502934 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8629 : {
8630 502907 : gcc_assert (ctx->css_state != NULL);
8631 502907 : if (!CASE_LOW (stmt))
8632 : {
8633 : /* default: should appear just once in a SWITCH_EXPR
8634 : body (excluding nested SWITCH_EXPR). */
8635 76167 : gcc_assert (*ctx->css_state != css_default_seen);
8636 : /* When evaluating SWITCH_EXPR body for the second time,
8637 : return true for the default: label. */
8638 76167 : if (*ctx->css_state == css_default_processing)
8639 : return true;
8640 38889 : *ctx->css_state = css_default_seen;
8641 : }
8642 426740 : else if (CASE_HIGH (stmt))
8643 : {
8644 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8645 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8646 : return true;
8647 : }
8648 426720 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8649 : return true;
8650 : }
8651 : break;
8652 :
8653 : case BREAK_STMT:
8654 : case CONTINUE_STMT:
8655 : /* These two are handled directly in cxx_eval_loop_expr by testing
8656 : breaks (jump_target) or continues (jump_target). */
8657 : break;
8658 :
8659 : case VAR_DECL:
8660 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8661 : and other places by testing throws (jump_target). */
8662 : break;
8663 :
8664 0 : default:
8665 0 : gcc_unreachable ();
8666 : }
8667 : return false;
8668 : }
8669 :
8670 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8671 : semantics, for switch, break, continue, and return. */
8672 :
8673 : static tree
8674 45917154 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8675 : bool *non_constant_p, bool *overflow_p,
8676 : tree *jump_target)
8677 : {
8678 : /* In a statement-expression we want to return the last value.
8679 : For empty statement expression return void_node. */
8680 45917154 : tree r = void_node;
8681 121124927 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8682 : {
8683 99476248 : tree stmt = *i;
8684 :
8685 : /* We've found a continue, so skip everything until we reach
8686 : the label its jumping to. */
8687 99476248 : if (continues (jump_target))
8688 : {
8689 1962 : if (label_matches (ctx, jump_target, stmt))
8690 : /* Found it. */
8691 9 : *jump_target = NULL_TREE;
8692 : else
8693 1953 : continue;
8694 : }
8695 99474295 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8696 10967301 : continue;
8697 :
8698 88506994 : value_cat lval = vc_discard;
8699 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8700 124822701 : if (tsi_one_before_end_p (i)
8701 36316925 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8702 : lval = vc_prvalue;
8703 :
8704 88506994 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8705 : non_constant_p, overflow_p,
8706 : jump_target);
8707 88506990 : if (*non_constant_p)
8708 : break;
8709 79456064 : if (returns (jump_target)
8710 64326618 : || breaks (jump_target)
8711 75207773 : || throws (jump_target))
8712 : break;
8713 : }
8714 45917150 : return r;
8715 : }
8716 :
8717 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8718 : semantics; continue semantics are covered by cxx_eval_statement_list. */
8719 :
8720 : static tree
8721 6344386 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8722 : bool *non_constant_p, bool *overflow_p,
8723 : tree *jump_target)
8724 : {
8725 6344386 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8726 6344386 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8727 6344386 : unsigned cond_cleanup_depth = 0;
8728 6344386 : int count = 0;
8729 6344386 : switch (TREE_CODE (t))
8730 : {
8731 3225 : case LOOP_EXPR:
8732 3225 : body = LOOP_EXPR_BODY (t);
8733 3225 : break;
8734 5128519 : case DO_STMT:
8735 5128519 : body = DO_BODY (t);
8736 5128519 : cond = DO_COND (t);
8737 5128519 : break;
8738 334391 : case WHILE_STMT:
8739 334391 : body = WHILE_BODY (t);
8740 334391 : cond = WHILE_COND (t);
8741 334391 : cond_prep = WHILE_COND_PREP (t);
8742 334391 : cond_cleanup = WHILE_COND_CLEANUP (t);
8743 334391 : count = -1;
8744 334391 : break;
8745 878251 : case FOR_STMT:
8746 878251 : if (FOR_INIT_STMT (t))
8747 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8748 : non_constant_p, overflow_p, jump_target);
8749 878251 : if (*non_constant_p)
8750 : return NULL_TREE;
8751 878251 : body = FOR_BODY (t);
8752 878251 : cond = FOR_COND (t);
8753 878251 : expr = FOR_EXPR (t);
8754 878251 : cond_prep = FOR_COND_PREP (t);
8755 878251 : cond_cleanup = FOR_COND_CLEANUP (t);
8756 878251 : count = -1;
8757 878251 : break;
8758 0 : default:
8759 0 : gcc_unreachable ();
8760 : }
8761 6344386 : if (cond_prep)
8762 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8763 30322785 : auto cleanup_cond = [&] {
8764 : /* Clean up the condition variable after each iteration. */
8765 23978399 : if (cond_cleanup_depth && !*non_constant_p)
8766 : {
8767 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8768 105 : tree s = BIND_EXPR_BODY (cond_prep);
8769 105 : unsigned i;
8770 210 : for (i = cond_cleanup_depth; i; --i)
8771 : {
8772 105 : tree_stmt_iterator iter = tsi_last (s);
8773 105 : s = tsi_stmt (iter);
8774 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8775 105 : s = CLEANUP_BODY (s);
8776 : }
8777 105 : tree c;
8778 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8779 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8780 : overflow_p, jump_target);
8781 105 : }
8782 23978399 : if (cond_prep)
8783 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8784 222 : decl; decl = DECL_CHAIN (decl))
8785 111 : destroy_value_checked (ctx, decl, non_constant_p);
8786 6344386 : };
8787 18881800 : do
8788 : {
8789 18881800 : if (count != -1)
8790 : {
8791 17669158 : if (body)
8792 17669158 : cxx_eval_constant_expression (ctx, body, vc_discard,
8793 : non_constant_p, overflow_p,
8794 : jump_target);
8795 17669158 : if (breaks (jump_target))
8796 : {
8797 35145 : *jump_target = NULL_TREE;
8798 35145 : break;
8799 : }
8800 :
8801 17634013 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8802 570 : *jump_target = NULL_TREE;
8803 :
8804 17634013 : if (expr)
8805 5290272 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8806 : non_constant_p, overflow_p,
8807 : jump_target);
8808 17634013 : cleanup_cond ();
8809 : }
8810 :
8811 18846655 : if (cond_prep)
8812 : {
8813 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8814 222 : decl; decl = DECL_CHAIN (decl))
8815 111 : ctx->global->clear_value (decl);
8816 111 : if (cond_cleanup)
8817 : {
8818 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8819 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8820 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8821 : at the end of those STATEMENT_LISTs. */
8822 111 : cond_cleanup_depth = 0;
8823 111 : tree s = BIND_EXPR_BODY (cond_prep);
8824 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8825 222 : depth; --depth)
8826 : {
8827 111 : for (tree_stmt_iterator i = tsi_start (s);
8828 321 : !tsi_end_p (i); ++i)
8829 : {
8830 321 : tree stmt = *i;
8831 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8832 0 : continue;
8833 321 : if (tsi_one_before_end_p (i))
8834 : {
8835 : /* The last statement in the STATEMENT_LIST
8836 : has to be a CLEANUP_STMT (verified in
8837 : finish_loop_cond_prep). We want to
8838 : evaluate just its CLEANUP_BODY part but not
8839 : CLEANUP_EXPR part just yet. */
8840 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8841 : /* If the CLEANUP_STMT is not actually to be
8842 : evaluated, don't increment cond_cleanup_depth
8843 : so that we don't evaluate the CLEANUP_EXPR
8844 : for it later either. */
8845 105 : if (*jump_target)
8846 : {
8847 : depth = 1;
8848 : break;
8849 : }
8850 105 : ++cond_cleanup_depth;
8851 : /* If not in the innermost one, next iteration
8852 : will handle CLEANUP_BODY similarly. */
8853 105 : if (depth > 1)
8854 : {
8855 0 : s = CLEANUP_BODY (stmt);
8856 0 : break;
8857 : }
8858 : /* The innermost one can be evaluated normally. */
8859 105 : cxx_eval_constant_expression (ctx,
8860 105 : CLEANUP_BODY (stmt),
8861 : vc_discard,
8862 : non_constant_p,
8863 : overflow_p,
8864 : jump_target);
8865 105 : break;
8866 : }
8867 : /* And so should be evaluated statements which aren't
8868 : last in the STATEMENT_LIST. */
8869 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8870 : non_constant_p, overflow_p,
8871 : jump_target);
8872 216 : if (*non_constant_p
8873 210 : || returns (jump_target)
8874 210 : || breaks (jump_target)
8875 210 : || continues (jump_target)
8876 531 : || throws (jump_target))
8877 : {
8878 : depth = 1;
8879 : break;
8880 : }
8881 : }
8882 : }
8883 : }
8884 : else
8885 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8886 : vc_discard, non_constant_p,
8887 : overflow_p, jump_target);
8888 : }
8889 :
8890 18846655 : if (cond)
8891 : {
8892 18839558 : tree res
8893 18839558 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8894 : non_constant_p, overflow_p,
8895 : jump_target);
8896 18839558 : if (res)
8897 : {
8898 18784755 : if (verify_constant (res, ctx->quiet, non_constant_p,
8899 : overflow_p))
8900 : break;
8901 18633847 : if (integer_zerop (res))
8902 : break;
8903 : }
8904 : else
8905 54803 : gcc_assert (*jump_target);
8906 : }
8907 :
8908 12593985 : if (++count >= constexpr_loop_limit)
8909 : {
8910 27 : if (!ctx->quiet)
8911 9 : error_at (cp_expr_loc_or_input_loc (t),
8912 : "%<constexpr%> loop iteration count exceeds limit of %d "
8913 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8914 : constexpr_loop_limit);
8915 27 : *non_constant_p = true;
8916 27 : break;
8917 : }
8918 : }
8919 37725537 : while (!returns (jump_target)
8920 12537621 : && !breaks (jump_target)
8921 12537621 : && !continues (jump_target)
8922 12537714 : && (!switches (jump_target) || count == 0)
8923 12537591 : && !throws (jump_target)
8924 25188067 : && !*non_constant_p);
8925 :
8926 6344386 : cleanup_cond ();
8927 :
8928 6344386 : return NULL_TREE;
8929 : }
8930 :
8931 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8932 : semantics. */
8933 :
8934 : static tree
8935 49744 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8936 : bool *non_constant_p, bool *overflow_p,
8937 : tree *jump_target)
8938 : {
8939 49744 : tree cond
8940 49744 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8941 49744 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8942 : non_constant_p, overflow_p,
8943 : jump_target);
8944 49744 : if (*jump_target)
8945 : return NULL_TREE;
8946 49744 : VERIFY_CONSTANT (cond);
8947 49469 : if (TREE_CODE (cond) != INTEGER_CST)
8948 : {
8949 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8950 : switch condition even if it's constant enough for other things
8951 : (c++/113545). */
8952 0 : gcc_checking_assert (ctx->quiet);
8953 0 : *non_constant_p = true;
8954 0 : return t;
8955 : }
8956 :
8957 49469 : *jump_target = cond;
8958 :
8959 49469 : tree body
8960 49469 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8961 49469 : constexpr_ctx new_ctx = *ctx;
8962 49469 : constexpr_switch_state css = css_default_not_seen;
8963 49469 : new_ctx.css_state = &css;
8964 49469 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8965 : non_constant_p, overflow_p, jump_target);
8966 86786 : if (switches (jump_target) && css == css_default_seen)
8967 : {
8968 : /* If the SWITCH_EXPR body has default: label, process it once again,
8969 : this time instructing label_matches to return true for default:
8970 : label on switches (jump_target). */
8971 37278 : css = css_default_processing;
8972 37278 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8973 : non_constant_p, overflow_p, jump_target);
8974 : }
8975 49469 : if (breaks (jump_target) || switches (jump_target))
8976 21498 : *jump_target = NULL_TREE;
8977 : return NULL_TREE;
8978 : }
8979 :
8980 : /* Find the object of TYPE under initialization in CTX. */
8981 :
8982 : static tree
8983 16666 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8984 : {
8985 16666 : if (!ctx)
8986 : return NULL_TREE;
8987 :
8988 : /* Prefer the outermost matching object, but don't cross
8989 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8990 16291 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8991 553 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8992 : return outer_ob;
8993 :
8994 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8995 : can is a minor optimization. */
8996 16113 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8997 300 : return ctx->ctor;
8998 :
8999 15813 : if (!ctx->object)
9000 : return NULL_TREE;
9001 :
9002 : /* Since an object cannot have a field of its own type, we can search outward
9003 : from ctx->object to find the unique containing object of TYPE. */
9004 : tree ob = ctx->object;
9005 15804 : while (ob)
9006 : {
9007 15804 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
9008 : break;
9009 219 : if (handled_component_p (ob))
9010 217 : ob = TREE_OPERAND (ob, 0);
9011 : else
9012 : ob = NULL_TREE;
9013 : }
9014 :
9015 : return ob;
9016 : }
9017 :
9018 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
9019 : true, we're checking a constexpr function body. */
9020 :
9021 : static void
9022 35 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
9023 : {
9024 35 : auto_diagnostic_group d;
9025 35 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
9026 : "constant expression"))
9027 35 : inform (loc, "only unevaluated inline assembly is allowed in a "
9028 : "%<constexpr%> function in C++20");
9029 35 : }
9030 :
9031 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
9032 : context; maybe complain about that. */
9033 :
9034 : static void
9035 91735001 : maybe_warn_about_constant_value (location_t loc, tree decl)
9036 : {
9037 91735001 : static bool explained = false;
9038 91735001 : auto_diagnostic_group d;
9039 91735001 : if (cxx_dialect >= cxx17
9040 91566213 : && warn_interference_size
9041 91566213 : && !OPTION_SET_P (param_destruct_interfere_size)
9042 91566213 : && DECL_CONTEXT (decl) == std_node
9043 18866653 : && DECL_NAME (decl)
9044 18866647 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
9045 16 : && (LOCATION_FILE (input_location) != main_input_filename
9046 10 : || module_exporting_p ())
9047 91735004 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
9048 91735010 : && !explained)
9049 : {
9050 6 : explained = true;
9051 6 : inform (loc, "its value can vary between compiler versions or "
9052 : "with different %<-mtune%> or %<-mcpu%> flags");
9053 6 : inform (loc, "if this use is part of a public ABI, change it to "
9054 : "instead use a constant variable you define");
9055 6 : inform (loc, "the default value for the current CPU tuning "
9056 : "is %d bytes", param_destruct_interfere_size);
9057 6 : inform (loc, "you can stabilize this value with %<--param "
9058 : "hardware_destructive_interference_size=%d%>, or disable "
9059 : "this warning with %<-Wno-interference-size%>",
9060 : param_destruct_interfere_size);
9061 : }
9062 91735001 : }
9063 :
9064 : /* For element type ELT_TYPE, return the appropriate type of the heap object
9065 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
9066 : in bytes. If COOKIE_SIZE is NULL, return array type
9067 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
9068 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
9069 : where N is computed such that the size of the struct fits into FULL_SIZE.
9070 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
9071 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
9072 : will be also 0 and so it is not possible to determine the actual array
9073 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
9074 : expression evaluation of subexpressions of ARG_SIZE. */
9075 :
9076 : static tree
9077 105287 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9078 : tree cookie_size, tree full_size, tree arg_size,
9079 : bool *non_constant_p, bool *overflow_p,
9080 : tree *jump_target)
9081 : {
9082 105287 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9083 105287 : gcc_assert (tree_fits_uhwi_p (full_size));
9084 105287 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9085 105287 : if (arg_size)
9086 : {
9087 9 : STRIP_NOPS (arg_size);
9088 9 : if (cookie_size)
9089 : {
9090 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9091 : arg_size = NULL_TREE;
9092 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9093 0 : && tree_int_cst_equal (cookie_size,
9094 0 : TREE_OPERAND (arg_size, 0)))
9095 : {
9096 0 : arg_size = TREE_OPERAND (arg_size, 1);
9097 0 : STRIP_NOPS (arg_size);
9098 : }
9099 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9100 0 : && tree_int_cst_equal (cookie_size,
9101 0 : TREE_OPERAND (arg_size, 1)))
9102 : {
9103 0 : arg_size = TREE_OPERAND (arg_size, 0);
9104 0 : STRIP_NOPS (arg_size);
9105 : }
9106 : else
9107 : arg_size = NULL_TREE;
9108 : }
9109 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9110 : {
9111 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9112 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9113 9 : if (integer_zerop (op0))
9114 9 : arg_size
9115 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9116 : non_constant_p, overflow_p,
9117 : jump_target);
9118 0 : else if (integer_zerop (op1))
9119 0 : arg_size
9120 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9121 : non_constant_p, overflow_p,
9122 : jump_target);
9123 : else
9124 : arg_size = NULL_TREE;
9125 9 : if (*jump_target)
9126 : return NULL_TREE;
9127 : }
9128 : else
9129 : arg_size = NULL_TREE;
9130 : }
9131 :
9132 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9133 105287 : if (!arg_size)
9134 : {
9135 105278 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9136 105278 : gcc_assert (fsz >= csz);
9137 105278 : fsz -= csz;
9138 105278 : if (esz)
9139 105278 : fsz /= esz;
9140 : }
9141 105287 : tree itype2 = build_index_type (size_int (fsz - 1));
9142 105287 : if (!cookie_size)
9143 105278 : return build_cplus_array_type (elt_type, itype2);
9144 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9145 : }
9146 :
9147 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9148 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9149 : the expression which needed the cleanup threw. If both threw, diagnose
9150 : it and return NULL, otherwise return R. If only the cleanup threw, set
9151 : *JUMP_TARGET to the exception object from the cleanup. */
9152 :
9153 : static tree
9154 1735586 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9155 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9156 : {
9157 1735588 : if (!throws (&jmp_target))
9158 : return r;
9159 14 : if (throws (jump_target))
9160 : {
9161 : /* [except.throw]/9 - If the exception handling mechanism
9162 : handling an uncaught exception directly invokes a function
9163 : that exits via an exception, the function std::terminate is
9164 : invoked. */
9165 12 : if (!ctx->quiet)
9166 : {
9167 4 : auto_diagnostic_group d;
9168 4 : diagnose_std_terminate (loc, ctx, *jump_target);
9169 4 : inform (loc, "destructor exited with an exception");
9170 4 : }
9171 12 : *non_constant_p = true;
9172 12 : *jump_target = NULL_TREE;
9173 12 : return NULL_TREE;
9174 : }
9175 2 : *jump_target = jmp_target;
9176 2 : return r;
9177 : }
9178 :
9179 : /* Attempt to reduce the expression T to a constant value.
9180 : On failure, issue diagnostic and return error_mark_node. */
9181 : /* FIXME unify with c_fully_fold */
9182 : /* FIXME overflow_p is too global */
9183 :
9184 : tree
9185 2800848815 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9186 : value_cat lval,
9187 : bool *non_constant_p, bool *overflow_p,
9188 : tree *jump_target)
9189 : {
9190 2800848815 : if (*jump_target)
9191 : {
9192 : /* If we are jumping, ignore all statements/expressions except those
9193 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9194 1623784 : switch (TREE_CODE (t))
9195 : {
9196 : case BIND_EXPR:
9197 : case STATEMENT_LIST:
9198 : case LOOP_EXPR:
9199 : case COND_EXPR:
9200 : case IF_STMT:
9201 : case DO_STMT:
9202 : case WHILE_STMT:
9203 : case FOR_STMT:
9204 : break;
9205 502934 : case LABEL_EXPR:
9206 502934 : case CASE_LABEL_EXPR:
9207 502934 : if (label_matches (ctx, jump_target, t))
9208 : /* Found it. */
9209 49430 : *jump_target = NULL_TREE;
9210 502934 : return NULL_TREE;
9211 : default:
9212 : return NULL_TREE;
9213 : }
9214 : }
9215 2799470559 : if (error_operand_p (t))
9216 : {
9217 138 : *non_constant_p = true;
9218 138 : return t;
9219 : }
9220 :
9221 : /* Change the input location to the currently processed expression for
9222 : better error messages when a subexpression has no location. */
9223 2799470421 : location_t loc = cp_expr_loc_or_input_loc (t);
9224 5598935410 : iloc_sentinel sentinel (loc);
9225 :
9226 2799470421 : STRIP_ANY_LOCATION_WRAPPER (t);
9227 :
9228 2799470421 : if (CONSTANT_CLASS_P (t))
9229 : {
9230 444895097 : if (TREE_OVERFLOW (t))
9231 : {
9232 62 : if (!ctx->quiet)
9233 15 : permerror (input_location, "overflow in constant expression");
9234 62 : if (!flag_permissive || ctx->quiet)
9235 59 : *overflow_p = true;
9236 : }
9237 :
9238 444895097 : if (TREE_CODE (t) == INTEGER_CST
9239 423432037 : && TYPE_PTR_P (TREE_TYPE (t))
9240 : /* INTEGER_CST with pointer-to-method type is only used
9241 : for a virtual method in a pointer to member function.
9242 : Don't reject those. */
9243 1992297 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9244 446887093 : && !integer_zerop (t))
9245 : {
9246 5 : if (!ctx->quiet)
9247 0 : error ("value %qE of type %qT is not a constant expression",
9248 0 : t, TREE_TYPE (t));
9249 5 : *non_constant_p = true;
9250 : }
9251 :
9252 444895097 : return t;
9253 : }
9254 :
9255 : /* Avoid excessively long constexpr evaluations. */
9256 2354575324 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9257 : {
9258 9 : if (!ctx->quiet)
9259 3 : error_at (loc,
9260 : "%<constexpr%> evaluation operation count exceeds limit of "
9261 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9262 : constexpr_ops_limit);
9263 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9264 9 : *non_constant_p = true;
9265 9 : return t;
9266 : }
9267 :
9268 2354575315 : constexpr_ctx new_ctx;
9269 2354575315 : tree r = t;
9270 :
9271 2354575315 : tree_code tcode = TREE_CODE (t);
9272 2354575315 : switch (tcode)
9273 : {
9274 54264191 : case RESULT_DECL:
9275 54264191 : if (lval)
9276 : return t;
9277 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9278 : through an invisible reference, or in named return value
9279 : optimization. */
9280 45973 : if (tree v = ctx->global->get_value (t))
9281 : return v;
9282 : else
9283 : {
9284 3 : if (!ctx->quiet)
9285 0 : error ("%qE is not a constant expression", t);
9286 3 : *non_constant_p = true;
9287 : }
9288 3 : break;
9289 :
9290 285602099 : case VAR_DECL:
9291 285602099 : if (DECL_HAS_VALUE_EXPR_P (t))
9292 : {
9293 8652051 : if (is_normal_capture_proxy (t)
9294 8652051 : && current_function_decl == DECL_CONTEXT (t))
9295 : {
9296 : /* Function parms aren't constexpr within the function
9297 : definition, so don't try to look at the closure. But if the
9298 : captured variable is constant, try to evaluate it directly. */
9299 532110 : r = DECL_CAPTURED_VARIABLE (t);
9300 : }
9301 : else
9302 8119941 : r = DECL_VALUE_EXPR (t);
9303 :
9304 8652051 : tree type = TREE_TYPE (t);
9305 8652051 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
9306 : {
9307 : /* Adjust r to match the reference-ness of t. */
9308 219762 : if (TYPE_REF_P (type))
9309 215571 : r = build_address (r);
9310 : else
9311 4191 : r = convert_from_reference (r);
9312 : }
9313 8652051 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
9314 8652051 : overflow_p, jump_target);
9315 : }
9316 : /* fall through */
9317 287605498 : case CONST_DECL:
9318 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
9319 : CONST_DECL for aggregate constants. */
9320 287605498 : if (lval)
9321 : return t;
9322 205944903 : else if (t == ctx->object)
9323 1018552 : return ctx->ctor;
9324 204926351 : if (VAR_P (t))
9325 : {
9326 194270901 : if (tree v = ctx->global->get_value (t))
9327 : {
9328 : r = v;
9329 : break;
9330 : }
9331 131504821 : if (ctx->global->is_outside_lifetime (t))
9332 : {
9333 107 : if (!ctx->quiet)
9334 31 : outside_lifetime_error (loc, t);
9335 107 : *non_constant_p = true;
9336 107 : break;
9337 : }
9338 : }
9339 142160164 : if (ctx->manifestly_const_eval == mce_true)
9340 91735001 : maybe_warn_about_constant_value (loc, t);
9341 142160164 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
9342 142160164 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9343 : {
9344 : /* If the class is empty, we aren't actually loading anything. */
9345 242549 : r = build_constructor (TREE_TYPE (t), NULL);
9346 242549 : TREE_CONSTANT (r) = true;
9347 : }
9348 141917615 : else if (ctx->strict)
9349 141570589 : r = decl_really_constant_value (t, /*unshare_p=*/false);
9350 : else
9351 347026 : r = decl_constant_value (t, /*unshare_p=*/false);
9352 142157467 : if (TREE_CODE (r) == TARGET_EXPR
9353 142157467 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9354 0 : r = TARGET_EXPR_INITIAL (r);
9355 142157467 : if (DECL_P (r)
9356 : /* P2280 allows references to unknown. */
9357 142443144 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
9358 : {
9359 30414743 : if (!ctx->quiet)
9360 183 : non_const_var_error (loc, r, /*fundef_p*/false);
9361 30414743 : *non_constant_p = true;
9362 : }
9363 : break;
9364 :
9365 : case DEBUG_BEGIN_STMT:
9366 : /* ??? It might be nice to retain this information somehow, so
9367 : as to be able to step into a constexpr function call. */
9368 : /* Fall through. */
9369 :
9370 : case FUNCTION_DECL:
9371 : case TEMPLATE_DECL:
9372 : case LABEL_DECL:
9373 : case LABEL_EXPR:
9374 : case CASE_LABEL_EXPR:
9375 : case PREDICT_EXPR:
9376 : case OMP_DECLARE_MAPPER:
9377 : case REFLECT_EXPR:
9378 : return t;
9379 :
9380 270818050 : case PARM_DECL:
9381 270818050 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9382 : {
9383 : /* glvalue use. */
9384 19797053 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9385 152727 : if (tree v = ctx->global->get_value (t))
9386 1904102482 : r = v;
9387 : }
9388 251020997 : else if (tree v = ctx->global->get_value (t))
9389 : {
9390 154894375 : r = v;
9391 154894375 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9392 5378 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9393 : non_constant_p, overflow_p,
9394 : jump_target);
9395 154894375 : if (*jump_target)
9396 : return NULL_TREE;
9397 : }
9398 96126622 : else if (lval)
9399 : /* Defer in case this is only used for its type. */;
9400 96126622 : else if (ctx->global->is_outside_lifetime (t))
9401 : {
9402 18 : if (!ctx->quiet)
9403 6 : outside_lifetime_error (loc, t);
9404 18 : *non_constant_p = true;
9405 18 : break;
9406 : }
9407 96126604 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9408 96126604 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9409 : {
9410 : /* If the class is empty, we aren't actually loading anything. */
9411 29744 : r = build_constructor (TREE_TYPE (t), NULL);
9412 29744 : TREE_CONSTANT (r) = true;
9413 : }
9414 96096860 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9415 : /* P2280 allows references to unknown... */;
9416 95818289 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9417 : /* ...as well as the this pointer. */;
9418 : else
9419 : {
9420 95362816 : if (!ctx->quiet)
9421 196 : error ("%qE is not a constant expression", t);
9422 95362816 : *non_constant_p = true;
9423 : }
9424 : break;
9425 :
9426 183993615 : case CALL_EXPR:
9427 183993615 : case AGGR_INIT_EXPR:
9428 183993615 : r = cxx_eval_call_expression (ctx, t, lval,
9429 : non_constant_p, overflow_p, jump_target);
9430 183993615 : break;
9431 :
9432 20631185 : case DECL_EXPR:
9433 20631185 : {
9434 20631185 : r = DECL_EXPR_DECL (t);
9435 20631185 : if (TREE_CODE (r) == USING_DECL)
9436 : {
9437 9379 : r = void_node;
9438 9379 : break;
9439 : }
9440 :
9441 20621806 : if (VAR_P (r)
9442 20621806 : && (TREE_STATIC (r)
9443 20475471 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9444 : /* Allow __FUNCTION__ etc. */
9445 146335 : && !DECL_ARTIFICIAL (r)
9446 20622625 : && !decl_constant_var_p (r))
9447 : {
9448 7 : if (!ctx->quiet)
9449 : {
9450 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9451 1 : error_at (loc, "control passes through definition of %qD "
9452 : "with thread storage duration", r);
9453 : else
9454 1 : error_at (loc, "control passes through definition of %qD "
9455 : "with static storage duration", r);
9456 : }
9457 7 : *non_constant_p = true;
9458 7 : break;
9459 : }
9460 :
9461 : /* make_rtl_for_nonlocal_decl could have deferred emission of
9462 : a local static var, but if it appears in a statement expression
9463 : which is constant expression evaluated to e.g. just the address
9464 : of the variable, its DECL_EXPR will never be seen during
9465 : gimple lowering's record_vars_into as the statement expression
9466 : will not be in the IL at all. */
9467 20621799 : if (VAR_P (r)
9468 20621799 : && TREE_STATIC (r)
9469 146328 : && !DECL_REALLY_EXTERN (r)
9470 146306 : && DECL_FUNCTION_SCOPE_P (r)
9471 146306 : && !var_in_maybe_constexpr_fn (r)
9472 20621802 : && decl_constant_var_p (r))
9473 : {
9474 3 : varpool_node *node = varpool_node::get (r);
9475 3 : if (node == NULL || !node->definition)
9476 3 : rest_of_decl_compilation (r, 0, at_eof);
9477 : }
9478 :
9479 41021876 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9480 38512854 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9481 : {
9482 2733690 : new_ctx = *ctx;
9483 2733690 : new_ctx.object = r;
9484 2733690 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9485 2733690 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9486 2733690 : ctx->global->put_value (r, new_ctx.ctor);
9487 2733690 : ctx = &new_ctx;
9488 : }
9489 :
9490 20621799 : if (tree init = DECL_INITIAL (r))
9491 : {
9492 10902337 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9493 : non_constant_p, overflow_p,
9494 : jump_target);
9495 10902337 : if (*jump_target)
9496 : return NULL_TREE;
9497 : /* Don't share a CONSTRUCTOR that might be changed. */
9498 10902337 : init = unshare_constructor (init);
9499 : /* Remember that a constant object's constructor has already
9500 : run. */
9501 21804674 : if (CLASS_TYPE_P (TREE_TYPE (r))
9502 11417655 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9503 69927 : TREE_READONLY (init) = true;
9504 10902337 : ctx->global->put_value (r, init);
9505 : }
9506 9719462 : else if (ctx == &new_ctx)
9507 : /* We gave it a CONSTRUCTOR above. */;
9508 : else
9509 7574060 : ctx->global->put_value (r, NULL_TREE);
9510 : }
9511 : break;
9512 :
9513 23000284 : case TARGET_EXPR:
9514 23000284 : {
9515 23000284 : tree type = TREE_TYPE (t);
9516 :
9517 23000284 : if (!literal_type_p (type))
9518 : {
9519 5218 : if (!ctx->quiet)
9520 : {
9521 0 : auto_diagnostic_group d;
9522 0 : error ("temporary of non-literal type %qT in a "
9523 : "constant expression", type);
9524 0 : explain_non_literal_class (type);
9525 0 : }
9526 5218 : *non_constant_p = true;
9527 8574007 : break;
9528 : }
9529 22995066 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9530 : /* Avoid evaluating a TARGET_EXPR more than once. */
9531 22995066 : tree slot = TARGET_EXPR_SLOT (t);
9532 22995066 : if (tree v = ctx->global->get_value (slot))
9533 : {
9534 2114 : if (lval)
9535 10485177 : return slot;
9536 : r = v;
9537 : break;
9538 : }
9539 22992952 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9540 : {
9541 : /* We're being expanded without an explicit target, so start
9542 : initializing a new object; expansion with an explicit target
9543 : strips the TARGET_EXPR before we get here. */
9544 17001885 : new_ctx = *ctx;
9545 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9546 : any PLACEHOLDER_EXPR within the initializer that refers to the
9547 : former object under construction. */
9548 17001885 : new_ctx.parent = ctx;
9549 17001885 : new_ctx.ctor = build_constructor (type, NULL);
9550 17001885 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9551 17001885 : new_ctx.object = slot;
9552 17001885 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9553 17001885 : ctx = &new_ctx;
9554 : }
9555 :
9556 : /* If the initializer is complex, evaluate it to initialize slot. */
9557 22992952 : bool is_complex = target_expr_needs_replace (t);
9558 22992952 : if (is_complex)
9559 : /* In case no initialization actually happens, clear out any
9560 : void_node from a previous evaluation. */
9561 477 : ctx->global->put_value (slot, NULL_TREE);
9562 :
9563 : /* Pass vc_prvalue because this indicates
9564 : initialization of a temporary. */
9565 22992952 : r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t),
9566 : vc_prvalue, non_constant_p,
9567 : overflow_p, jump_target);
9568 22992952 : if (*non_constant_p)
9569 : break;
9570 14425227 : if (ctx->save_exprs)
9571 9645429 : ctx->save_exprs->safe_push (slot);
9572 14425227 : if (*jump_target)
9573 : {
9574 533 : if (!is_complex
9575 533 : && !(AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9576 : /* If TARGET_EXPR_INITIAL throws exception and slot's value
9577 : has not been changed yet, CLEANUP_POINT_EXPR handling
9578 : could see there void_node from a previous evaluation
9579 : and complain. */
9580 6 : ctx->global->put_value (slot, NULL_TREE);
9581 533 : return NULL_TREE;
9582 : }
9583 14424694 : if (!is_complex)
9584 : {
9585 14424289 : r = unshare_constructor (r);
9586 : /* Adjust the type of the result to the type of the temporary. */
9587 14424289 : r = adjust_temp_type (type, r);
9588 14424289 : ctx->global->put_value (slot, r);
9589 : }
9590 14424694 : if (TARGET_EXPR_CLEANUP (t)
9591 14424694 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9592 : {
9593 1403936 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9594 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9595 : them. */
9596 1403936 : if (CLEANUP_EH_ONLY (t))
9597 1806 : ctx->global->cleanups->safe_push (NULL_TREE);
9598 : }
9599 14424694 : if (lval)
9600 : return slot;
9601 3941100 : if (is_complex)
9602 0 : r = ctx->global->get_value (slot);
9603 : }
9604 3941100 : break;
9605 :
9606 97752631 : case INIT_EXPR:
9607 97752631 : case MODIFY_EXPR:
9608 97752631 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9609 97752631 : r = cxx_eval_store_expression (ctx, t, lval,
9610 : non_constant_p, overflow_p, jump_target);
9611 97752631 : break;
9612 :
9613 0 : case SCOPE_REF:
9614 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9615 : lval,
9616 : non_constant_p, overflow_p,
9617 : jump_target);
9618 0 : break;
9619 :
9620 60444188 : case RETURN_EXPR:
9621 60444188 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9622 60113959 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9623 : lval,
9624 : non_constant_p, overflow_p,
9625 : jump_target);
9626 60444184 : if (!throws (jump_target))
9627 60443888 : *jump_target = t;
9628 : break;
9629 53991 : case BREAK_STMT:
9630 53991 : case CONTINUE_STMT:
9631 53991 : *jump_target = t;
9632 53991 : break;
9633 :
9634 699375 : case SAVE_EXPR:
9635 : /* Avoid evaluating a SAVE_EXPR more than once. */
9636 699375 : if (tree v = ctx->global->get_value (t))
9637 : r = v;
9638 : else
9639 : {
9640 679041 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9641 : vc_prvalue, non_constant_p,
9642 : overflow_p, jump_target);
9643 679041 : if (*non_constant_p || *jump_target)
9644 : break;
9645 12266 : ctx->global->put_value (t, r);
9646 12266 : if (ctx->save_exprs)
9647 8764 : ctx->save_exprs->safe_push (t);
9648 : }
9649 : break;
9650 :
9651 57945188 : case NON_LVALUE_EXPR:
9652 57945188 : case EXPR_STMT:
9653 57945188 : case EH_SPEC_BLOCK:
9654 57945188 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9655 : lval,
9656 : non_constant_p, overflow_p,
9657 : jump_target);
9658 57945188 : break;
9659 :
9660 4320 : case TRY_BLOCK:
9661 4320 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9662 : non_constant_p, overflow_p,
9663 : jump_target);
9664 4320 : if (!*non_constant_p && throws (jump_target))
9665 3511 : if (tree h = TRY_HANDLERS (t))
9666 : {
9667 3511 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9668 3511 : if (TREE_CODE (h) == STATEMENT_LIST)
9669 : {
9670 938 : for (tree stmt : tsi_range (h))
9671 904 : if (TREE_CODE (stmt) == HANDLER
9672 904 : && handler_match_for_exception_type (stmt, type))
9673 : {
9674 : h = stmt;
9675 : break;
9676 : }
9677 462 : if (TREE_CODE (h) == STATEMENT_LIST)
9678 : h = NULL_TREE;
9679 : }
9680 3049 : else if (TREE_CODE (h) != HANDLER
9681 3049 : || !handler_match_for_exception_type (h, type))
9682 : h = NULL_TREE;
9683 : if (h)
9684 : {
9685 3477 : gcc_assert (VAR_P (*jump_target));
9686 3477 : ctx->global->caught_exceptions.safe_push (*jump_target);
9687 3477 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9688 3477 : *jump_target = NULL_TREE;
9689 3477 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9690 : vc_discard, non_constant_p,
9691 : overflow_p, jump_target);
9692 : }
9693 : }
9694 : break;
9695 :
9696 86623722 : case CLEANUP_POINT_EXPR:
9697 86623722 : {
9698 86623722 : auto_vec<tree, 2> cleanups;
9699 86623722 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9700 86623722 : ctx->global->cleanups = &cleanups;
9701 :
9702 86623722 : auto_vec<tree, 10> save_exprs;
9703 86623722 : constexpr_ctx new_ctx = *ctx;
9704 86623722 : new_ctx.save_exprs = &save_exprs;
9705 :
9706 86623722 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9707 : lval,
9708 : non_constant_p, overflow_p,
9709 : jump_target);
9710 :
9711 86623720 : ctx->global->cleanups = prev_cleanups;
9712 86623720 : unsigned int i;
9713 86623720 : tree cleanup, jmp_target = NULL_TREE;
9714 86623720 : bool eh = throws (jump_target);
9715 : /* Evaluate the cleanups. */
9716 174557760 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9717 1310320 : if (cleanup == NULL_TREE)
9718 : {
9719 : /* NULL_TREE cleanup is a marker that before it is
9720 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9721 : if the body didn't throw. */
9722 1783 : if (!eh)
9723 1781 : --i;
9724 : }
9725 : else
9726 1308537 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9727 : non_constant_p, overflow_p,
9728 : &jmp_target);
9729 :
9730 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9731 : full-expression. */
9732 269525353 : for (tree save_expr : save_exprs)
9733 9654193 : destroy_value_checked (ctx, save_expr, non_constant_p);
9734 86623720 : if (throws (&jmp_target))
9735 0 : *jump_target = jmp_target;
9736 86623720 : }
9737 86623720 : break;
9738 :
9739 54910808 : case MUST_NOT_THROW_EXPR:
9740 54910808 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9741 : lval,
9742 : non_constant_p, overflow_p,
9743 : jump_target);
9744 54910806 : if (throws (jump_target))
9745 : {
9746 : /* [except.handle]/7 - If the search for a handler exits the
9747 : function body of a function with a non-throwing exception
9748 : specification, the function std::terminate is invoked. */
9749 54 : if (!ctx->quiet)
9750 : {
9751 18 : auto_diagnostic_group d;
9752 18 : diagnose_std_terminate (loc, ctx, *jump_target);
9753 18 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9754 14 : && ctx->call
9755 32 : && ctx->call->fundef)
9756 14 : inform (loc, "uncaught exception exited from %<noexcept%> "
9757 : "function %qD",
9758 : ctx->call->fundef->decl);
9759 4 : else if (MUST_NOT_THROW_THROW_P (t))
9760 2 : inform (loc, "destructor exited with an exception after "
9761 : "initializing the exception object");
9762 2 : else if (MUST_NOT_THROW_CATCH_P (t))
9763 2 : inform (loc, "constructor exited with another exception while "
9764 : "entering handler");
9765 18 : }
9766 54 : *non_constant_p = true;
9767 54 : *jump_target = NULL_TREE;
9768 54 : r = NULL_TREE;
9769 : }
9770 : break;
9771 :
9772 0 : case TRY_CATCH_EXPR:
9773 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9774 : {
9775 0 : r = void_node;
9776 0 : break;
9777 : }
9778 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9779 : non_constant_p, overflow_p,
9780 : jump_target);
9781 0 : if (!*non_constant_p && throws (jump_target))
9782 : {
9783 0 : tree jmp_target = NULL_TREE;
9784 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9785 : non_constant_p, overflow_p,
9786 : &jmp_target);
9787 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9788 : jmp_target);
9789 : }
9790 : break;
9791 :
9792 830 : case TRY_FINALLY_EXPR:
9793 830 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9794 : non_constant_p, overflow_p,
9795 : jump_target);
9796 830 : if (!*non_constant_p)
9797 : {
9798 815 : tree jmp_target = NULL_TREE;
9799 : /* Also evaluate the cleanup. */
9800 815 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9801 815 : && throws (jump_target))
9802 0 : cxx_eval_constant_expression (ctx,
9803 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9804 : 1), vc_discard,
9805 : non_constant_p, overflow_p,
9806 : &jmp_target);
9807 : else
9808 815 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9809 : non_constant_p, overflow_p,
9810 : &jmp_target);
9811 815 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9812 : jmp_target);
9813 : }
9814 : break;
9815 :
9816 0 : case EH_ELSE_EXPR:
9817 : /* Evaluate any cleanup that applies to non-EH exits. */
9818 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9819 : non_constant_p, overflow_p,
9820 : jump_target);
9821 :
9822 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9823 0 : break;
9824 :
9825 3607909 : case CLEANUP_STMT:
9826 3607909 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9827 : non_constant_p, overflow_p,
9828 : jump_target);
9829 3607907 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9830 : {
9831 1734771 : iloc_sentinel ils (loc);
9832 1734771 : tree jmp_target = NULL_TREE;
9833 : /* Also evaluate the cleanup. */
9834 1734771 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9835 : non_constant_p, overflow_p,
9836 : &jmp_target);
9837 1734771 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9838 : jmp_target);
9839 1734771 : }
9840 : break;
9841 :
9842 : /* These differ from cxx_eval_unary_expression in that this doesn't
9843 : check for a constant operand or result; an address can be
9844 : constant without its operand being, and vice versa. */
9845 93719446 : case MEM_REF:
9846 93719446 : case INDIRECT_REF:
9847 93719446 : r = cxx_eval_indirect_ref (ctx, t, lval,
9848 : non_constant_p, overflow_p,
9849 : jump_target);
9850 93719446 : break;
9851 :
9852 90243342 : case ADDR_EXPR:
9853 90243342 : {
9854 90243342 : tree oldop = TREE_OPERAND (t, 0);
9855 90243342 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9856 : non_constant_p, overflow_p,
9857 : jump_target);
9858 90243342 : if (*jump_target)
9859 : return NULL_TREE;
9860 : /* Don't VERIFY_CONSTANT here. */
9861 90243337 : if (*non_constant_p)
9862 : return t;
9863 74222298 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9864 : /* This function does more aggressive folding than fold itself. */
9865 74222298 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9866 74222298 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9867 : {
9868 50898630 : ggc_free (r);
9869 50898630 : return t;
9870 : }
9871 : break;
9872 : }
9873 :
9874 544504 : case REALPART_EXPR:
9875 544504 : case IMAGPART_EXPR:
9876 544504 : if (lval)
9877 : {
9878 1150 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9879 : non_constant_p, overflow_p,
9880 : jump_target);
9881 1150 : if (*jump_target)
9882 : return NULL_TREE;
9883 1150 : if (r == error_mark_node)
9884 : ;
9885 1150 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9886 : r = t;
9887 : else
9888 1088 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9889 : break;
9890 : }
9891 : /* FALLTHRU */
9892 24561343 : case CONJ_EXPR:
9893 24561343 : case FIX_TRUNC_EXPR:
9894 24561343 : case FLOAT_EXPR:
9895 24561343 : case NEGATE_EXPR:
9896 24561343 : case ABS_EXPR:
9897 24561343 : case ABSU_EXPR:
9898 24561343 : case BIT_NOT_EXPR:
9899 24561343 : case TRUTH_NOT_EXPR:
9900 24561343 : case FIXED_CONVERT_EXPR:
9901 24561343 : case VEC_DUPLICATE_EXPR:
9902 24561343 : r = cxx_eval_unary_expression (ctx, t, lval,
9903 : non_constant_p, overflow_p,
9904 : jump_target);
9905 24561343 : break;
9906 :
9907 9194715 : case SIZEOF_EXPR:
9908 9194715 : r = fold_sizeof_expr (t);
9909 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9910 : which could lead to an infinite recursion. */
9911 9194715 : if (TREE_CODE (r) != SIZEOF_EXPR)
9912 9194715 : r = cxx_eval_constant_expression (ctx, r, lval,
9913 : non_constant_p, overflow_p,
9914 : jump_target);
9915 : else
9916 : {
9917 0 : *non_constant_p = true;
9918 0 : gcc_assert (ctx->quiet);
9919 : }
9920 :
9921 : break;
9922 :
9923 11847217 : case COMPOUND_EXPR:
9924 11847217 : {
9925 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9926 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9927 : introduced by build_call_a. */
9928 11847217 : tree op0 = TREE_OPERAND (t, 0);
9929 11847217 : tree op1 = TREE_OPERAND (t, 1);
9930 11847217 : STRIP_NOPS (op1);
9931 3943202 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9932 14356876 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9933 5210572 : r = cxx_eval_constant_expression (ctx, op0,
9934 : lval, non_constant_p, overflow_p,
9935 : jump_target);
9936 : else
9937 : {
9938 : /* Check that the LHS is constant and then discard it. */
9939 6636645 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9940 : non_constant_p, overflow_p,
9941 : jump_target);
9942 6636645 : if (*jump_target)
9943 : return NULL_TREE;
9944 6629790 : if (*non_constant_p)
9945 : return t;
9946 6371268 : op1 = TREE_OPERAND (t, 1);
9947 6371268 : r = cxx_eval_constant_expression (ctx, op1,
9948 : lval, non_constant_p, overflow_p,
9949 : jump_target);
9950 : }
9951 : }
9952 : break;
9953 :
9954 106704755 : case POINTER_PLUS_EXPR:
9955 106704755 : case POINTER_DIFF_EXPR:
9956 106704755 : case PLUS_EXPR:
9957 106704755 : case MINUS_EXPR:
9958 106704755 : case MULT_EXPR:
9959 106704755 : case TRUNC_DIV_EXPR:
9960 106704755 : case CEIL_DIV_EXPR:
9961 106704755 : case FLOOR_DIV_EXPR:
9962 106704755 : case ROUND_DIV_EXPR:
9963 106704755 : case TRUNC_MOD_EXPR:
9964 106704755 : case CEIL_MOD_EXPR:
9965 106704755 : case ROUND_MOD_EXPR:
9966 106704755 : case RDIV_EXPR:
9967 106704755 : case EXACT_DIV_EXPR:
9968 106704755 : case MIN_EXPR:
9969 106704755 : case MAX_EXPR:
9970 106704755 : case LSHIFT_EXPR:
9971 106704755 : case RSHIFT_EXPR:
9972 106704755 : case LROTATE_EXPR:
9973 106704755 : case RROTATE_EXPR:
9974 106704755 : case BIT_IOR_EXPR:
9975 106704755 : case BIT_XOR_EXPR:
9976 106704755 : case BIT_AND_EXPR:
9977 106704755 : case TRUTH_XOR_EXPR:
9978 106704755 : case LT_EXPR:
9979 106704755 : case LE_EXPR:
9980 106704755 : case GT_EXPR:
9981 106704755 : case GE_EXPR:
9982 106704755 : case EQ_EXPR:
9983 106704755 : case NE_EXPR:
9984 106704755 : case SPACESHIP_EXPR:
9985 106704755 : case UNORDERED_EXPR:
9986 106704755 : case ORDERED_EXPR:
9987 106704755 : case UNLT_EXPR:
9988 106704755 : case UNLE_EXPR:
9989 106704755 : case UNGT_EXPR:
9990 106704755 : case UNGE_EXPR:
9991 106704755 : case UNEQ_EXPR:
9992 106704755 : case LTGT_EXPR:
9993 106704755 : case RANGE_EXPR:
9994 106704755 : case COMPLEX_EXPR:
9995 106704755 : r = cxx_eval_binary_expression (ctx, t, lval,
9996 : non_constant_p, overflow_p,
9997 : jump_target);
9998 106704755 : break;
9999 :
10000 : /* fold can introduce non-IF versions of these; still treat them as
10001 : short-circuiting. */
10002 14735784 : case TRUTH_AND_EXPR:
10003 14735784 : case TRUTH_ANDIF_EXPR:
10004 14735784 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
10005 : boolean_true_node,
10006 : non_constant_p, overflow_p,
10007 : jump_target);
10008 14735784 : break;
10009 :
10010 2931283 : case TRUTH_OR_EXPR:
10011 2931283 : case TRUTH_ORIF_EXPR:
10012 2931283 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
10013 : boolean_false_node,
10014 : non_constant_p, overflow_p,
10015 : jump_target);
10016 2931283 : break;
10017 :
10018 15137162 : case ARRAY_REF:
10019 15137162 : r = cxx_eval_array_reference (ctx, t, lval,
10020 : non_constant_p, overflow_p,
10021 : jump_target);
10022 15137162 : break;
10023 :
10024 105793530 : case COMPONENT_REF:
10025 105793530 : if (is_overloaded_fn (t))
10026 : {
10027 : /* We can only get here in checking mode via
10028 : build_non_dependent_expr, because any expression that
10029 : calls or takes the address of the function will have
10030 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
10031 6 : gcc_checking_assert (ctx->quiet || errorcount);
10032 6 : *non_constant_p = true;
10033 6 : return t;
10034 : }
10035 105793524 : r = cxx_eval_component_reference (ctx, t, lval,
10036 : non_constant_p, overflow_p,
10037 : jump_target);
10038 105793524 : break;
10039 :
10040 42353 : case BIT_FIELD_REF:
10041 42353 : r = cxx_eval_bit_field_ref (ctx, t, lval,
10042 : non_constant_p, overflow_p,
10043 : jump_target);
10044 42353 : break;
10045 :
10046 37619209 : case COND_EXPR:
10047 37619209 : case IF_STMT:
10048 37619209 : if (*jump_target)
10049 : {
10050 151626 : tree orig_jump = *jump_target;
10051 151626 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
10052 303252 : ? TREE_OPERAND (t, 1) : void_node);
10053 : /* When jumping to a label, the label might be either in the
10054 : then or else blocks, so process then block first in skipping
10055 : mode first, and if we are still in the skipping mode at its end,
10056 : process the else block too. */
10057 151626 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10058 : overflow_p, jump_target);
10059 : /* It's possible that we found the label in the then block. But
10060 : it could have been followed by another jumping statement, e.g.
10061 : say we're looking for case 1:
10062 : if (cond)
10063 : {
10064 : // skipped statements
10065 : case 1:; // clears up *jump_target
10066 : return 1; // and sets it to a RETURN_EXPR
10067 : }
10068 : else { ... }
10069 : in which case we need not go looking to the else block.
10070 : (goto is not allowed in a constexpr function.) */
10071 151626 : if (*jump_target == orig_jump)
10072 : {
10073 151683 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
10074 151683 : ? TREE_OPERAND (t, 2) : void_node);
10075 151588 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10076 : overflow_p, jump_target);
10077 : }
10078 : break;
10079 : }
10080 37467583 : r = cxx_eval_conditional_expression (ctx, t, lval,
10081 : non_constant_p, overflow_p,
10082 : jump_target);
10083 37467583 : break;
10084 9374 : case VEC_COND_EXPR:
10085 9374 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10086 : overflow_p, jump_target);
10087 9374 : break;
10088 :
10089 22016092 : case CONSTRUCTOR:
10090 22016092 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10091 : {
10092 : /* Don't re-process a constant CONSTRUCTOR. */
10093 19024282 : verify_constructor_flags (t);
10094 19024282 : if (TREE_CONSTANT (t))
10095 : return t;
10096 : }
10097 2991810 : if (TREE_CLOBBER_P (t))
10098 : {
10099 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10100 : a clobber by itself isn't a constant-expression. */
10101 0 : gcc_assert (ctx->quiet);
10102 0 : *non_constant_p = true;
10103 0 : break;
10104 : }
10105 2991810 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10106 : non_constant_p, overflow_p, jump_target);
10107 2991810 : break;
10108 :
10109 565 : case VEC_INIT_EXPR:
10110 : /* We can get this in a defaulted constructor for a class with a
10111 : non-static data member of array type. Either the initializer will
10112 : be NULL, meaning default-initialization, or it will be an lvalue
10113 : or xvalue of the same type, meaning direct-initialization from the
10114 : corresponding member. */
10115 565 : r = cxx_eval_vec_init (ctx, t, lval,
10116 : non_constant_p, overflow_p, jump_target);
10117 565 : break;
10118 :
10119 42410 : case VEC_PERM_EXPR:
10120 42410 : r = cxx_eval_trinary_expression (ctx, t, lval,
10121 : non_constant_p, overflow_p,
10122 : jump_target);
10123 42410 : break;
10124 :
10125 4 : case PAREN_EXPR:
10126 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
10127 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10128 : constant expressions since it's unaffected by -fassociative-math. */
10129 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10130 : non_constant_p, overflow_p,
10131 : jump_target);
10132 4 : break;
10133 :
10134 405348144 : case NOP_EXPR:
10135 405348144 : if (REINTERPRET_CAST_P (t))
10136 : {
10137 105 : if (!ctx->quiet)
10138 6 : error_at (loc,
10139 : "%<reinterpret_cast%> is not a constant expression");
10140 105 : *non_constant_p = true;
10141 105 : return t;
10142 : }
10143 : /* FALLTHROUGH. */
10144 508133247 : case CONVERT_EXPR:
10145 508133247 : case VIEW_CONVERT_EXPR:
10146 508133247 : case UNARY_PLUS_EXPR:
10147 508133247 : {
10148 508133247 : tree oldop = TREE_OPERAND (t, 0);
10149 :
10150 508133247 : tree op = cxx_eval_constant_expression (ctx, oldop,
10151 508133247 : VOID_TYPE_P (TREE_TYPE (t))
10152 : ? vc_discard
10153 : : tcode == VIEW_CONVERT_EXPR
10154 475995142 : ? lval : vc_prvalue,
10155 : non_constant_p, overflow_p,
10156 : jump_target);
10157 508130548 : if (*jump_target)
10158 : return NULL_TREE;
10159 508126720 : if (*non_constant_p)
10160 : return t;
10161 420338690 : tree type = TREE_TYPE (t);
10162 :
10163 420338690 : if (VOID_TYPE_P (type))
10164 31191811 : return void_node;
10165 :
10166 389146879 : if (TREE_CODE (t) == CONVERT_EXPR
10167 55921961 : && ARITHMETIC_TYPE_P (type)
10168 4963913 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10169 389170384 : && ctx->strict)
10170 : {
10171 23410 : if (!ctx->quiet)
10172 54 : error_at (loc,
10173 : "conversion from pointer type %qT to arithmetic type "
10174 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10175 23410 : *non_constant_p = true;
10176 23410 : return t;
10177 : }
10178 :
10179 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10180 : type cannot be part of a core constant expression as a resolution to
10181 : DR 1312. */
10182 141705941 : if (TYPE_PTROB_P (type)
10183 138370522 : && TYPE_PTR_P (TREE_TYPE (op))
10184 87289380 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10185 : /* Inside a call to std::construct_at,
10186 : std::allocator<T>::{,de}allocate, or
10187 : std::source_location::current, we permit casting from void*
10188 : because that is compiler-generated code. */
10189 1884704 : && !is_std_construct_at (ctx->call)
10190 138392 : && !is_std_allocator_allocate (ctx->call)
10191 389183294 : && !is_std_source_location_current (ctx->call))
10192 : {
10193 : /* Likewise, don't error when casting from void* when OP is
10194 : &heap uninit and similar. */
10195 59666 : tree sop = tree_strip_nop_conversions (op);
10196 59666 : tree decl = NULL_TREE;
10197 59666 : if (TREE_CODE (sop) == ADDR_EXPR)
10198 40433 : decl = TREE_OPERAND (sop, 0);
10199 40433 : if (decl
10200 40433 : && VAR_P (decl)
10201 39627 : && DECL_ARTIFICIAL (decl)
10202 79694 : && (DECL_NAME (decl) == heap_identifier
10203 28460 : || DECL_NAME (decl) == heap_uninit_identifier
10204 3306 : || DECL_NAME (decl) == heap_vec_identifier
10205 1960 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
10206 : /* OK */;
10207 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10208 : cv void" to a pointer-to-object type T unless P is a null
10209 : pointer value or points to an object whose type is similar to
10210 : T. */
10211 20799 : else if (cxx_dialect > cxx23)
10212 : {
10213 20650 : if (integer_zerop (sop))
10214 29 : return build_int_cst (type, 0);
10215 20621 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10216 : NULL, jump_target);
10217 20621 : if (*jump_target)
10218 : return NULL_TREE;
10219 20621 : if (r)
10220 : {
10221 20580 : r = build1 (ADDR_EXPR, type, r);
10222 20580 : break;
10223 : }
10224 41 : if (!ctx->quiet)
10225 : {
10226 5 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10227 5 : auto_diagnostic_group d;
10228 5 : error_at (loc, "cast from %qT is not allowed in a "
10229 : "constant expression because "
10230 : "pointed-to type %qT is not similar to %qT",
10231 5 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10232 5 : TREE_TYPE (type));
10233 5 : tree obj = build_fold_indirect_ref (sop);
10234 5 : if (TREE_CODE (obj) == COMPONENT_REF)
10235 4 : obj = TREE_OPERAND (obj, 1);
10236 5 : if (DECL_P (obj))
10237 5 : inform (DECL_SOURCE_LOCATION (obj),
10238 : "pointed-to object declared here");
10239 5 : }
10240 41 : *non_constant_p = true;
10241 41 : return t;
10242 : }
10243 : else
10244 : {
10245 149 : if (!ctx->quiet)
10246 26 : error_at (loc, "cast from %qT is not allowed in a "
10247 : "constant expression before C++26",
10248 26 : TREE_TYPE (op));
10249 149 : *non_constant_p = true;
10250 149 : return t;
10251 : }
10252 : }
10253 :
10254 389102670 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
10255 : {
10256 1031 : op = cplus_expand_constant (op);
10257 1031 : if (TREE_CODE (op) == PTRMEM_CST)
10258 : {
10259 21 : if (!ctx->quiet)
10260 3 : error_at (loc, "%qE is not a constant expression when the "
10261 : "class %qT is still incomplete", op,
10262 3 : PTRMEM_CST_CLASS (op));
10263 21 : *non_constant_p = true;
10264 21 : return t;
10265 : }
10266 : }
10267 :
10268 389102649 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
10269 : {
10270 590 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
10271 590 : && !can_convert_qual (type, op))
10272 6 : op = cplus_expand_constant (op);
10273 590 : return cp_fold_convert (type, op);
10274 : }
10275 :
10276 389102059 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
10277 : {
10278 824062 : if (integer_zerop (op))
10279 : {
10280 767377 : if (TYPE_REF_P (type))
10281 : {
10282 35 : if (!ctx->quiet)
10283 4 : error_at (loc, "dereferencing a null pointer");
10284 35 : *non_constant_p = true;
10285 35 : return t;
10286 : }
10287 : }
10288 56685 : else if (TYPE_PTR_P (type)
10289 56685 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10290 : /* INTEGER_CST with pointer-to-method type is only used
10291 : for a virtual method in a pointer to member function.
10292 : Don't reject those. */
10293 : ;
10294 : else
10295 : {
10296 : /* This detects for example:
10297 : reinterpret_cast<void*>(sizeof 0)
10298 : */
10299 56682 : if (!ctx->quiet)
10300 15 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
10301 : "a constant expression",
10302 : type, op);
10303 56682 : *non_constant_p = true;
10304 56682 : return t;
10305 : }
10306 : }
10307 :
10308 389045342 : if (INDIRECT_TYPE_P (type)
10309 236526870 : && TREE_CODE (op) == NOP_EXPR
10310 111184638 : && TREE_TYPE (op) == ptr_type_node
10311 485829 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
10312 482701 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
10313 389419576 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10314 374234 : 0)) == heap_uninit_identifier
10315 270513 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10316 270513 : 0)) == heap_vec_uninit_identifier))
10317 : {
10318 105287 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
10319 105287 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
10320 105287 : tree elt_type = TREE_TYPE (type);
10321 105287 : tree cookie_size = NULL_TREE;
10322 105287 : tree arg_size = NULL_TREE;
10323 105287 : if (TREE_CODE (elt_type) == RECORD_TYPE
10324 105287 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
10325 : {
10326 9 : tree fld1 = TYPE_FIELDS (elt_type);
10327 9 : tree fld2 = DECL_CHAIN (fld1);
10328 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
10329 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
10330 : }
10331 105287 : DECL_NAME (var)
10332 105287 : = (DECL_NAME (var) == heap_uninit_identifier
10333 105287 : ? heap_identifier : heap_vec_identifier);
10334 : /* For zero sized elt_type, try to recover how many outer_nelts
10335 : it should have. */
10336 105287 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
10337 105278 : : integer_zerop (var_size))
10338 89 : && !int_size_in_bytes (elt_type)
10339 9 : && TREE_CODE (oldop) == CALL_EXPR
10340 105305 : && call_expr_nargs (oldop) >= 1)
10341 9 : if (tree fun = get_function_named_in_call (oldop))
10342 9 : if (cxx_replaceable_global_alloc_fn (fun)
10343 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
10344 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
10345 105287 : tree new_type
10346 105287 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
10347 : var_size, arg_size,
10348 : non_constant_p, overflow_p,
10349 : jump_target);
10350 105287 : if (*jump_target)
10351 : return NULL_TREE;
10352 105287 : TREE_TYPE (var) = new_type;
10353 105287 : TREE_TYPE (TREE_OPERAND (op, 0))
10354 210574 : = build_pointer_type (TREE_TYPE (var));
10355 : }
10356 :
10357 : /* This can happen for std::meta::info(^^int) where the cast has no
10358 : meaning. */
10359 389045342 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
10360 : {
10361 : r = op;
10362 : break;
10363 : }
10364 :
10365 388937760 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
10366 : /* We didn't fold at the top so we could check for ptr-int
10367 : conversion. */
10368 30247289 : return fold (t);
10369 :
10370 358690471 : tree sop;
10371 :
10372 : /* Handle an array's bounds having been deduced after we built
10373 : the wrapping expression. */
10374 358690471 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
10375 : r = op;
10376 154626855 : else if (sop = tree_strip_nop_conversions (op),
10377 214597218 : sop != op && (same_type_ignoring_tlq_and_bounds_p
10378 59970363 : (type, TREE_TYPE (sop))))
10379 : r = sop;
10380 127340255 : else if (tcode == UNARY_PLUS_EXPR)
10381 0 : r = fold_convert (TREE_TYPE (t), op);
10382 : else
10383 127340255 : r = fold_build1 (tcode, type, op);
10384 :
10385 : /* Conversion of an out-of-range value has implementation-defined
10386 : behavior; the language considers it different from arithmetic
10387 : overflow, which is undefined. */
10388 358690471 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10389 13107 : TREE_OVERFLOW (r) = false;
10390 : }
10391 : break;
10392 :
10393 9513 : case EXCESS_PRECISION_EXPR:
10394 9513 : {
10395 9513 : tree oldop = TREE_OPERAND (t, 0);
10396 :
10397 9513 : tree op = cxx_eval_constant_expression (ctx, oldop,
10398 : lval,
10399 : non_constant_p, overflow_p,
10400 : jump_target);
10401 9513 : if (*jump_target)
10402 : return NULL_TREE;
10403 9513 : if (*non_constant_p)
10404 : return t;
10405 9507 : r = fold_convert (TREE_TYPE (t), op);
10406 9507 : break;
10407 : }
10408 :
10409 51481 : case EMPTY_CLASS_EXPR:
10410 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10411 : it to an appropriate CONSTRUCTOR. */
10412 51481 : return build_constructor (TREE_TYPE (t), NULL);
10413 :
10414 45917154 : case STATEMENT_LIST:
10415 45917154 : new_ctx = *ctx;
10416 45917154 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10417 45917154 : return cxx_eval_statement_list (&new_ctx, t,
10418 45917150 : non_constant_p, overflow_p, jump_target);
10419 :
10420 31590760 : case BIND_EXPR:
10421 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10422 : map, so that when checking whether they're already destroyed later we
10423 : don't get confused by remnants of previous calls. */
10424 49989119 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10425 18398359 : ctx->global->clear_value (decl);
10426 31590760 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10427 : lval,
10428 : non_constant_p, overflow_p,
10429 : jump_target);
10430 49989115 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10431 18398357 : destroy_value_checked (ctx, decl, non_constant_p);
10432 : break;
10433 :
10434 8435439 : case PREINCREMENT_EXPR:
10435 8435439 : case POSTINCREMENT_EXPR:
10436 8435439 : case PREDECREMENT_EXPR:
10437 8435439 : case POSTDECREMENT_EXPR:
10438 8435439 : return cxx_eval_increment_expression (ctx, t,
10439 : lval, non_constant_p, overflow_p,
10440 8435439 : jump_target);
10441 :
10442 1816 : case THROW_EXPR:
10443 1816 : if (cxx_dialect >= cxx26)
10444 1489 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10445 : non_constant_p, overflow_p,
10446 1489 : jump_target);
10447 : /* FALLTHROUGH */
10448 335 : case LAMBDA_EXPR:
10449 335 : case NEW_EXPR:
10450 335 : case VEC_NEW_EXPR:
10451 335 : case DELETE_EXPR:
10452 335 : case VEC_DELETE_EXPR:
10453 335 : case MODOP_EXPR:
10454 : /* GCC internal stuff. */
10455 335 : case VA_ARG_EXPR:
10456 335 : case BASELINK:
10457 335 : case OFFSET_REF:
10458 335 : if (!ctx->quiet)
10459 86 : error_at (loc, "expression %qE is not a constant expression", t);
10460 335 : *non_constant_p = true;
10461 335 : break;
10462 :
10463 228149 : case OBJ_TYPE_REF:
10464 : /* Virtual function lookup. We don't need to do anything fancy. */
10465 228149 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10466 : lval, non_constant_p, overflow_p,
10467 228149 : jump_target);
10468 :
10469 16113 : case PLACEHOLDER_EXPR:
10470 : /* Use of the value or address of the current object. */
10471 16113 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10472 : {
10473 15885 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10474 : return ctor;
10475 : else
10476 15585 : return cxx_eval_constant_expression (ctx, ctor, lval,
10477 : non_constant_p, overflow_p,
10478 15585 : jump_target);
10479 : }
10480 : /* A placeholder without a referent. We can get here when
10481 : checking whether NSDMIs are noexcept, or in massage_init_elt;
10482 : just say it's non-constant for now. */
10483 228 : gcc_assert (ctx->quiet);
10484 228 : *non_constant_p = true;
10485 228 : break;
10486 :
10487 6556 : case EXIT_EXPR:
10488 6556 : {
10489 6556 : tree cond = TREE_OPERAND (t, 0);
10490 6556 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10491 : non_constant_p, overflow_p,
10492 : jump_target);
10493 6556 : if (*jump_target)
10494 : return NULL_TREE;
10495 6556 : VERIFY_CONSTANT (cond);
10496 6556 : if (integer_nonzerop (cond))
10497 3207 : *jump_target = t;
10498 : }
10499 : break;
10500 :
10501 21 : case GOTO_EXPR:
10502 21 : if (breaks (&TREE_OPERAND (t, 0))
10503 21 : || continues (&TREE_OPERAND (t, 0)))
10504 9 : *jump_target = TREE_OPERAND (t, 0);
10505 : else
10506 : {
10507 12 : if (!ctx->quiet)
10508 1 : error_at (loc, "%<goto%> is not a constant expression");
10509 12 : *non_constant_p = true;
10510 : }
10511 : break;
10512 :
10513 6344386 : case LOOP_EXPR:
10514 6344386 : case DO_STMT:
10515 6344386 : case WHILE_STMT:
10516 6344386 : case FOR_STMT:
10517 6344386 : cxx_eval_loop_expr (ctx, t,
10518 : non_constant_p, overflow_p, jump_target);
10519 6344386 : break;
10520 :
10521 49744 : case SWITCH_EXPR:
10522 49744 : case SWITCH_STMT:
10523 49744 : cxx_eval_switch_expr (ctx, t,
10524 : non_constant_p, overflow_p, jump_target);
10525 49744 : break;
10526 :
10527 132 : case REQUIRES_EXPR:
10528 : /* It's possible to get a requires-expression in a constant
10529 : expression. For example:
10530 :
10531 : template<typename T> concept bool C() {
10532 : return requires (T t) { t; };
10533 : }
10534 :
10535 : template<typename T> requires !C<T>() void f(T);
10536 :
10537 : Normalization leaves f with the associated constraint
10538 : '!requires (T t) { ... }' which is not transformed into
10539 : a constraint. */
10540 132 : if (!processing_template_decl)
10541 132 : return evaluate_requires_expr (t);
10542 : else
10543 0 : *non_constant_p = true;
10544 0 : return t;
10545 :
10546 1221343 : case ANNOTATE_EXPR:
10547 1221343 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10548 : lval,
10549 : non_constant_p, overflow_p,
10550 : jump_target);
10551 1221343 : break;
10552 :
10553 16348 : case USING_STMT:
10554 16348 : r = void_node;
10555 16348 : break;
10556 :
10557 30 : case ASSERTION_STMT:
10558 30 : case PRECONDITION_STMT:
10559 30 : case POSTCONDITION_STMT:
10560 30 : {
10561 30 : r = void_node;
10562 : /* Only record the first fail, and do not go further is the semantic
10563 : is 'ignore'. */
10564 30 : if (*non_constant_p || ctx->global->contract_statement
10565 60 : || contract_ignored_p (t))
10566 : break;
10567 :
10568 30 : tree cond = CONTRACT_CONDITION (t);
10569 30 : if (!potential_rvalue_constant_expression (cond))
10570 : {
10571 6 : ctx->global->contract_statement = t;
10572 6 : ctx->global->contract_condition_non_const = true;
10573 6 : break;
10574 : }
10575 :
10576 : /* We need to evaluate and stash the result of this here, since whether
10577 : it needs to be reported (and how) depends on whether the containing
10578 : expression is otherwise const. */
10579 24 : bool ctrct_non_const_p = false;
10580 24 : bool ctrct_overflow_p = false;
10581 24 : tree jmp_target = NULL_TREE;
10582 24 : constexpr_ctx new_ctx = *ctx;
10583 24 : new_ctx.quiet = true;
10584 : /* Avoid modification of existing values. */
10585 24 : modifiable_tracker ms (new_ctx.global);
10586 24 : tree eval =
10587 24 : cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
10588 : &ctrct_non_const_p,
10589 : &ctrct_overflow_p, &jmp_target);
10590 : /* Not a constant. */
10591 24 : if (ctrct_non_const_p)
10592 : {
10593 0 : ctx->global->contract_statement = t;
10594 0 : ctx->global->contract_condition_non_const = true;
10595 0 : break;
10596 : }
10597 : /* Constant, but check failed. */
10598 24 : if (integer_zerop (eval))
10599 24 : ctx->global->contract_statement = t;
10600 24 : }
10601 24 : break;
10602 :
10603 2725806 : case TEMPLATE_ID_EXPR:
10604 2725806 : {
10605 : /* We can evaluate template-id that refers to a concept only if
10606 : the template arguments are non-dependent. */
10607 2725806 : gcc_assert (concept_check_p (t));
10608 :
10609 2725806 : if (!value_dependent_expression_p (t)
10610 2725806 : && !uid_sensitive_constexpr_evaluation_p ())
10611 2724883 : r = evaluate_concept_check (t);
10612 : else
10613 923 : *non_constant_p = true;
10614 :
10615 : break;
10616 : }
10617 :
10618 51 : case ASM_EXPR:
10619 51 : if (!ctx->quiet)
10620 29 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10621 51 : *non_constant_p = true;
10622 51 : return t;
10623 :
10624 3844 : case BIT_CAST_EXPR:
10625 3844 : if (lval)
10626 : {
10627 0 : if (!ctx->quiet)
10628 0 : error_at (EXPR_LOCATION (t),
10629 : "address of a call to %qs is not a constant expression",
10630 : "__builtin_bit_cast");
10631 0 : *non_constant_p = true;
10632 0 : return t;
10633 : }
10634 3844 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10635 3844 : break;
10636 :
10637 0 : case OMP_PARALLEL:
10638 0 : case OMP_TASK:
10639 0 : case OMP_FOR:
10640 0 : case OMP_SIMD:
10641 0 : case OMP_DISTRIBUTE:
10642 0 : case OMP_TASKLOOP:
10643 0 : case OMP_LOOP:
10644 0 : case OMP_TEAMS:
10645 0 : case OMP_TARGET_DATA:
10646 0 : case OMP_TARGET:
10647 0 : case OMP_SECTIONS:
10648 0 : case OMP_ORDERED:
10649 0 : case OMP_CRITICAL:
10650 0 : case OMP_SINGLE:
10651 0 : case OMP_SCAN:
10652 0 : case OMP_SCOPE:
10653 0 : case OMP_SECTION:
10654 0 : case OMP_STRUCTURED_BLOCK:
10655 0 : case OMP_MASTER:
10656 0 : case OMP_MASKED:
10657 0 : case OMP_TASKGROUP:
10658 0 : case OMP_TARGET_UPDATE:
10659 0 : case OMP_TARGET_ENTER_DATA:
10660 0 : case OMP_TARGET_EXIT_DATA:
10661 0 : case OMP_ATOMIC:
10662 0 : case OMP_ATOMIC_READ:
10663 0 : case OMP_ATOMIC_CAPTURE_OLD:
10664 0 : case OMP_ATOMIC_CAPTURE_NEW:
10665 0 : case OMP_DEPOBJ:
10666 0 : case OACC_PARALLEL:
10667 0 : case OACC_KERNELS:
10668 0 : case OACC_SERIAL:
10669 0 : case OACC_DATA:
10670 0 : case OACC_HOST_DATA:
10671 0 : case OACC_LOOP:
10672 0 : case OACC_CACHE:
10673 0 : case OACC_DECLARE:
10674 0 : case OACC_ENTER_DATA:
10675 0 : case OACC_EXIT_DATA:
10676 0 : case OACC_UPDATE:
10677 0 : if (!ctx->quiet)
10678 0 : error_at (EXPR_LOCATION (t),
10679 : "statement is not a constant expression");
10680 0 : *non_constant_p = true;
10681 0 : break;
10682 :
10683 0 : default:
10684 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10685 : {
10686 : /* This function doesn't know how to deal with pre-genericize
10687 : statements; this can only happen with statement-expressions,
10688 : so for now just fail. */
10689 0 : if (!ctx->quiet)
10690 0 : error_at (EXPR_LOCATION (t),
10691 : "statement is not a constant expression");
10692 : }
10693 0 : else if (flag_checking)
10694 0 : internal_error ("unexpected expression %qE of kind %s", t,
10695 : get_tree_code_name (TREE_CODE (t)));
10696 0 : *non_constant_p = true;
10697 0 : break;
10698 : }
10699 :
10700 1904102482 : if (r == error_mark_node)
10701 12962535 : *non_constant_p = true;
10702 :
10703 128635084 : if (r == void_node && lval != vc_discard && !*jump_target
10704 1904120095 : && !VOID_TYPE_P (TREE_TYPE (t)))
10705 : {
10706 : /* For diagnostic quality we should have handled this sooner, where we
10707 : can be more specific about the out-of-lifetime object. But here we
10708 : can still be correct. */
10709 2 : gcc_checking_assert (false);
10710 : if (!ctx->quiet)
10711 : error_at (EXPR_LOCATION (t),
10712 : "%qE accesses an object outside its lifetime", t);
10713 : *non_constant_p = true;
10714 : }
10715 :
10716 1904102480 : if (*non_constant_p)
10717 351146429 : return t;
10718 : else
10719 : return r;
10720 : }
10721 :
10722 : /* P0859: A function is needed for constant evaluation if it is a constexpr
10723 : function that is named by an expression ([basic.def.odr]) that is
10724 : potentially constant evaluated.
10725 :
10726 : So we need to instantiate any constexpr functions mentioned by the
10727 : expression even if the definition isn't needed for evaluating the
10728 : expression. */
10729 :
10730 : static tree
10731 580759412 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10732 : {
10733 580759412 : if (TREE_CODE (*tp) == FUNCTION_DECL
10734 12152168 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10735 12015598 : && !DECL_INITIAL (*tp)
10736 3096738 : && !trivial_fn_p (*tp)
10737 3096589 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10738 580759412 : && !uid_sensitive_constexpr_evaluation_p ())
10739 : {
10740 3066569 : ++function_depth;
10741 3066569 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10742 3066542 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10743 : else
10744 27 : synthesize_method (*tp);
10745 3066569 : --function_depth;
10746 : }
10747 577692843 : else if (TREE_CODE (*tp) == CALL_EXPR
10748 566031588 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10749 : {
10750 12217306 : if (EXPR_HAS_LOCATION (*tp))
10751 12210535 : input_location = EXPR_LOCATION (*tp);
10752 : }
10753 :
10754 580759412 : if (!EXPR_P (*tp))
10755 328489245 : *walk_subtrees = 0;
10756 :
10757 580759412 : return NULL_TREE;
10758 : }
10759 :
10760 : static void
10761 278885937 : instantiate_constexpr_fns (tree t)
10762 : {
10763 278885937 : location_t loc = input_location;
10764 278885937 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10765 278885937 : input_location = loc;
10766 278885937 : }
10767 :
10768 : /* Look for heap variables in the expression *TP. */
10769 :
10770 : static tree
10771 365005 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10772 : {
10773 365005 : if (VAR_P (*tp)
10774 365005 : && (DECL_NAME (*tp) == heap_uninit_identifier
10775 7307 : || DECL_NAME (*tp) == heap_identifier
10776 2686 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10777 1932 : || DECL_NAME (*tp) == heap_vec_identifier
10778 719 : || DECL_NAME (*tp) == heap_deleted_identifier))
10779 : return *tp;
10780 :
10781 255714 : if (TYPE_P (*tp))
10782 166 : *walk_subtrees = 0;
10783 : return NULL_TREE;
10784 : }
10785 :
10786 : /* Find immediate function decls in *TP if any. */
10787 :
10788 : static tree
10789 603829738 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
10790 : {
10791 607738491 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10792 : return *tp;
10793 603822438 : if (TREE_CODE (*tp) == PTRMEM_CST
10794 29526 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10795 603851196 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10796 : return PTRMEM_CST_MEMBER (*tp);
10797 603822333 : if (REFLECT_EXPR_P (*tp))
10798 105344 : *walk_subtrees = 0;
10799 : return NULL_TREE;
10800 : }
10801 :
10802 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10803 : expression. Return a version of T that has TREE_CONSTANT cleared. */
10804 :
10805 : static tree
10806 146991 : mark_non_constant (tree t)
10807 : {
10808 146991 : gcc_checking_assert (TREE_CONSTANT (t));
10809 :
10810 : /* This isn't actually constant, so unset TREE_CONSTANT.
10811 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10812 : it to be set if it is invariant address, even when it is not
10813 : a valid C++ constant expression. Wrap it with a NOP_EXPR
10814 : instead. */
10815 146991 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10816 144486 : t = copy_node (t);
10817 2505 : else if (TREE_CODE (t) == CONSTRUCTOR)
10818 235 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10819 : else
10820 2270 : t = build_nop (TREE_TYPE (t), t);
10821 146991 : TREE_CONSTANT (t) = false;
10822 146991 : return t;
10823 : }
10824 :
10825 : /* If we have a successful constant evaluation, now check whether there is
10826 : a failed or non-constant contract that would invalidate this. */
10827 :
10828 : static bool
10829 530888448 : check_for_failed_contracts (constexpr_ctx *ctx)
10830 : {
10831 530888448 : constexpr_global_ctx *const global_ctx = ctx->global;
10832 530888448 : if (!flag_contracts || !global_ctx->contract_statement)
10833 : return false;
10834 :
10835 30 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
10836 30 : enum diagnostics::kind kind;
10837 30 : bool error = false;
10838 : /* [intro.compliance.general]/2.3.4. */
10839 : /* [basic.contract.eval]/8. */
10840 30 : if (ctx->manifestly_const_eval != mce_true)
10841 : {
10842 : /* When !MCE, silently return not constant. */
10843 : return true;
10844 : }
10845 27 : else if (contract_terminating_p (global_ctx->contract_statement))
10846 : {
10847 : kind = diagnostics::kind::error;
10848 : error = true;
10849 : }
10850 : else
10851 6 : kind = diagnostics::kind::warning;
10852 :
10853 : /* [basic.contract.eval]/7.3 */
10854 27 : if (global_ctx->contract_condition_non_const)
10855 : {
10856 6 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
10857 6 : return error;
10858 : }
10859 :
10860 : /* Otherwise, the evaluation was const, but determined to be false. */
10861 21 : emit_diagnostic (kind, loc, 0,
10862 : "contract predicate is false in constant expression");
10863 21 : return error;
10864 : }
10865 :
10866 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10867 : STRICT has the same sense as for constant_value_1: true if we only allow
10868 : conforming C++ constant expressions, or false if we want a constant value
10869 : even if it doesn't conform.
10870 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10871 : per P0595 even when ALLOW_NON_CONSTANT is true.
10872 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10873 : OBJECT must be non-NULL in that case. */
10874 :
10875 : static tree
10876 540846418 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10877 : bool strict = true,
10878 : mce_value manifestly_const_eval = mce_unknown,
10879 : bool constexpr_dtor = false,
10880 : tree object = NULL_TREE)
10881 : {
10882 540846418 : auto_timevar time (TV_CONSTEXPR);
10883 :
10884 540846418 : bool non_constant_p = false;
10885 540846418 : bool overflow_p = false;
10886 :
10887 540846418 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10888 : {
10889 0 : gcc_checking_assert (allow_non_constant);
10890 : return t;
10891 : }
10892 :
10893 540846418 : constexpr_global_ctx global_ctx;
10894 540846418 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10895 : allow_non_constant, strict,
10896 540846418 : !allow_non_constant ? mce_true : manifestly_const_eval };
10897 :
10898 : /* Turn off -frounding-math for manifestly constant evaluation. */
10899 540846418 : warning_sentinel rm (flag_rounding_math,
10900 540846418 : ctx.manifestly_const_eval == mce_true);
10901 540846418 : tree type = (object
10902 540846418 : ? cv_unqualified (TREE_TYPE (object))
10903 497380019 : : initialized_type (t));
10904 540846418 : tree r = t;
10905 540846418 : bool is_consteval = false;
10906 540846418 : if (VOID_TYPE_P (type))
10907 : {
10908 9951133 : if (!constexpr_dtor)
10909 : {
10910 9951133 : if (cxx_dialect < cxx20)
10911 : return t;
10912 : /* We could have a COMPOUND_EXPR here coming from
10913 : keep_unused_object_arg. */
10914 9940089 : tree x = extract_call_expr (t);
10915 9940089 : if (x == NULL_TREE || x == error_mark_node)
10916 : return t;
10917 : /* Calls to immediate functions returning void need to be
10918 : evaluated. */
10919 9940049 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10920 19880098 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10921 : return t;
10922 : else
10923 1130 : is_consteval = true;
10924 1130 : tree lam;
10925 1130 : if (manifestly_const_eval == mce_true
10926 1645 : && LAMBDA_FUNCTION_P (fndecl)
10927 509 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
10928 1639 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
10929 492 : global_ctx.consteval_block = fndecl;
10930 : }
10931 : }
10932 530895285 : else if (cxx_dialect >= cxx20
10933 522713689 : && (TREE_CODE (t) == CALL_EXPR
10934 452895288 : || TREE_CODE (t) == AGGR_INIT_EXPR
10935 448532919 : || TREE_CODE (t) == TARGET_EXPR))
10936 : {
10937 79606015 : tree x = t;
10938 79606015 : if (TREE_CODE (x) == TARGET_EXPR)
10939 5425245 : x = TARGET_EXPR_INITIAL (x);
10940 79606015 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10941 156625056 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10942 : is_consteval = true;
10943 : /* Don't try to evaluate a std::vector constructor taking an integer, it
10944 : will fail in the 'if (heap_var)' block below after doing all the work
10945 : (c++/113835). This will need adjustment if P3554 is accepted. Note
10946 : that evaluation of e.g. the vector default constructor can succeed, so
10947 : we don't shortcut all vector constructors. */
10948 154038082 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10949 10749699 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10950 79627735 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10951 : return t;
10952 : }
10953 530895046 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10954 : {
10955 : /* In C++14 an NSDMI can participate in aggregate initialization,
10956 : and can refer to the address of the object being initialized, so
10957 : we need to pass in the relevant VAR_DECL if we want to do the
10958 : evaluation in a single pass. The evaluation will dynamically
10959 : update ctx.values for the VAR_DECL. We use the same strategy
10960 : for C++11 constexpr constructors that refer to the object being
10961 : initialized. */
10962 41840547 : if (constexpr_dtor)
10963 : {
10964 200 : gcc_assert (object && VAR_P (object));
10965 200 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10966 200 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10967 200 : if (error_operand_p (DECL_INITIAL (object)))
10968 : return t;
10969 183 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10970 183 : TREE_READONLY (ctx.ctor) = false;
10971 : /* Temporarily force decl_really_constant_value to return false
10972 : for it, we want to use ctx.ctor for the current value instead. */
10973 183 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10974 : }
10975 : else
10976 : {
10977 41840347 : ctx.ctor = build_constructor (type, NULL);
10978 41840347 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10979 : }
10980 41840530 : if (!object)
10981 : {
10982 21781499 : if (TREE_CODE (t) == CALL_EXPR)
10983 : {
10984 : /* If T is calling a constructor to initialize an object, reframe
10985 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10986 : from outside the constant evaluation, which will fail even if
10987 : the value is actually constant (is_constant_evaluated3.C). */
10988 9871486 : tree fn = cp_get_callee_fndecl_nofold (t);
10989 19742776 : if (fn && DECL_CONSTRUCTOR_P (fn))
10990 : {
10991 4052511 : object = CALL_EXPR_ARG (t, 0);
10992 4052511 : object = build_fold_indirect_ref (object);
10993 4052511 : r = build_aggr_init_expr (type, r);
10994 : }
10995 : }
10996 11910013 : else if (TREE_CODE (t) == TARGET_EXPR)
10997 5321579 : object = TARGET_EXPR_SLOT (t);
10998 6588434 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10999 704701 : object = AGGR_INIT_EXPR_SLOT (t);
11000 : }
11001 41840530 : ctx.object = object;
11002 41840530 : if (object)
11003 30137822 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
11004 : (type, TREE_TYPE (object)));
11005 30137822 : if (object && DECL_P (object))
11006 25738293 : global_ctx.put_value (object, ctx.ctor);
11007 41840530 : if (TREE_CODE (r) == TARGET_EXPR)
11008 : /* Avoid creating another CONSTRUCTOR when we expand the
11009 : TARGET_EXPR. */
11010 5462021 : r = TARGET_EXPR_INITIAL (r);
11011 : }
11012 :
11013 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
11014 : constexpr evaluation to avoid unnecessary template instantiation, and is
11015 : always done with mce_unknown. But due to gaps in the restriction logic
11016 : we may still end up taking an evaluation path that in turn requires
11017 : manifestly constant evaluation, and such evaluation must not be
11018 : restricted since it likely has semantic consequences.
11019 : TODO: Remove/replace the mechanism in GCC 17. */
11020 530895029 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
11021 530895029 : if (ctx.manifestly_const_eval == mce_true)
11022 278885937 : uid_sensitive_constexpr_evaluation_value = false;
11023 :
11024 530895029 : auto_vec<tree, 16> cleanups;
11025 530895029 : global_ctx.cleanups = &cleanups;
11026 :
11027 530895029 : if (manifestly_const_eval == mce_true)
11028 278885937 : instantiate_constexpr_fns (r);
11029 530895029 : tree jmp_target = NULL_TREE;
11030 530895029 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
11031 : &non_constant_p, &overflow_p,
11032 : &jmp_target);
11033 530893578 : if (throws (&jmp_target) && !non_constant_p)
11034 : {
11035 1248 : if (!ctx.quiet)
11036 374 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11037 1248 : non_constant_p = true;
11038 1248 : jmp_target = NULL_TREE;
11039 1248 : r = t;
11040 : }
11041 530891082 : else if (!non_constant_p && jmp_target)
11042 : {
11043 24 : non_constant_p = true;
11044 24 : if (!ctx.quiet)
11045 : {
11046 3 : if (breaks (&jmp_target))
11047 3 : error ("%<break%> outside of a loop or %<switch%>");
11048 0 : else if (continues (&jmp_target))
11049 0 : error ("%<continue%> outside of a loop");
11050 0 : else if (returns (&jmp_target))
11051 0 : error ("%<return%> in a statement expression");
11052 : else
11053 0 : gcc_unreachable ();
11054 : }
11055 24 : r = t;
11056 : }
11057 :
11058 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
11059 : of statements, and the result ought to be stored in ctx.ctor. */
11060 530892330 : if (r == void_node && !constexpr_dtor && ctx.ctor)
11061 0 : r = ctx.ctor;
11062 :
11063 530892330 : unsigned int i;
11064 530892330 : tree cleanup;
11065 530892330 : jmp_target = NULL_TREE;
11066 : /* Evaluate the cleanups. */
11067 1061878278 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
11068 93618 : if (cleanup == NULL_TREE)
11069 : /* NULL_TREE cleanup is a marker that before it is
11070 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
11071 23 : --i;
11072 : else
11073 93595 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
11074 : &non_constant_p, &overflow_p,
11075 : &jmp_target);
11076 530892330 : if (throws (&jmp_target) && !non_constant_p)
11077 : {
11078 0 : if (!ctx.quiet)
11079 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11080 0 : non_constant_p = true;
11081 0 : r = t;
11082 : }
11083 :
11084 : /* Mutable logic is a bit tricky: we want to allow initialization of
11085 : constexpr variables with mutable members, but we can't copy those
11086 : members to another constexpr variable. */
11087 530892330 : if (!non_constant_p
11088 403171667 : && TREE_CODE (r) == CONSTRUCTOR
11089 547045646 : && CONSTRUCTOR_MUTABLE_POISON (r))
11090 : {
11091 90 : if (!allow_non_constant)
11092 6 : error ("%qE is not a constant expression because it refers to "
11093 : "mutable subobjects of %qT", t, type);
11094 90 : non_constant_p = true;
11095 : }
11096 :
11097 403171577 : if (!non_constant_p && cxx_dialect >= cxx20
11098 934063907 : && !global_ctx.heap_vars.is_empty ())
11099 : {
11100 119812 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11101 : NULL);
11102 119812 : unsigned int i;
11103 119812 : if (heap_var)
11104 : {
11105 109291 : if (!allow_non_constant && !non_constant_p)
11106 : {
11107 17 : auto_diagnostic_group d;
11108 17 : if (DECL_LANG_SPECIFIC (heap_var))
11109 4 : error ("%qE is not a constant expression because it refers to "
11110 : "exception object allocated with "
11111 : "%<__cxa_allocate_exception%>", t);
11112 : else
11113 13 : error ("%qE is not a constant expression because it refers to "
11114 : "a result of %<operator new%>", t);
11115 17 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11116 17 : }
11117 109291 : r = t;
11118 109291 : non_constant_p = true;
11119 : }
11120 309484 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11121 : {
11122 189672 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11123 : {
11124 109363 : if (!allow_non_constant && !non_constant_p)
11125 : {
11126 16 : auto_diagnostic_group d;
11127 16 : error ("%qE is not a constant expression because allocated "
11128 : "storage has not been deallocated", t);
11129 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11130 16 : }
11131 109363 : r = t;
11132 109363 : non_constant_p = true;
11133 : }
11134 : }
11135 : }
11136 :
11137 : /* Check that immediate invocation does not return an expression referencing
11138 : any immediate function decls. */
11139 530892330 : if (!non_constant_p && cxx_dialect >= cxx20)
11140 794759654 : if (tree immediate_fndecl
11141 397379827 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11142 : NULL))
11143 : {
11144 7405 : if (!allow_non_constant && !non_constant_p)
11145 : {
11146 74 : if (is_consteval)
11147 36 : error_at (cp_expr_loc_or_input_loc (t),
11148 : "immediate evaluation returns address of immediate "
11149 : "function %qD", immediate_fndecl);
11150 : else
11151 58 : error_at (cp_expr_loc_or_input_loc (t),
11152 : "constant evaluation returns address of immediate "
11153 : "function %qD", immediate_fndecl);
11154 : }
11155 7405 : r = t;
11156 7405 : non_constant_p = true;
11157 : }
11158 :
11159 : /* Detect consteval-only smuggling: turning a consteval-only object
11160 : into one that is not. For instance, in
11161 : struct B { };
11162 : struct D : B { info r; };
11163 : constexpr D d{^^::};
11164 : constexpr const B &b = d; // #1
11165 : #1 is wrong because D is a consteval-only type but B is not. */
11166 530892330 : if (flag_reflection
11167 10875672 : && !non_constant_p
11168 8735979 : && object
11169 985766 : && POINTER_TYPE_P (TREE_TYPE (object))
11170 1702 : && !consteval_only_p (object)
11171 530893918 : && check_out_of_consteval_use (r, /*complain=*/false))
11172 : {
11173 30 : if (!allow_non_constant)
11174 : {
11175 10 : if (TYPE_REF_P (TREE_TYPE (object)))
11176 12 : error_at (cp_expr_loc_or_input_loc (t),
11177 : "reference into an object of consteval-only type is "
11178 : "not a constant expression unless it also has "
11179 : "consteval-only type");
11180 : else
11181 4 : error_at (cp_expr_loc_or_input_loc (t),
11182 : "pointer into an object of consteval-only type is "
11183 : "not a constant expression unless it also has "
11184 : "consteval-only type");
11185 : }
11186 30 : r = t;
11187 30 : non_constant_p = true;
11188 : }
11189 :
11190 530892330 : if (!non_constant_p && !constexpr_dtor)
11191 403054618 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11192 :
11193 : /* After verify_constant because reduced_constant_expression_p can unset
11194 : CONSTRUCTOR_NO_CLEARING. */
11195 530892330 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11196 : {
11197 67820 : if (!allow_non_constant)
11198 45 : error ("%qE is not a constant expression because it refers to "
11199 : "an incompletely initialized variable", t);
11200 67820 : TREE_CONSTANT (r) = false;
11201 67820 : non_constant_p = true;
11202 : }
11203 :
11204 530892330 : if (non_constant_p)
11205 : /* If we saw something bad, go back to our argument. The wrapping below is
11206 : only for the cases of TREE_CONSTANT argument or overflow. */
11207 129923193 : r = t;
11208 :
11209 530892330 : if (!non_constant_p && overflow_p)
11210 215 : non_constant_p = true;
11211 :
11212 : /* Unshare the result. */
11213 530892330 : bool should_unshare = true;
11214 530892330 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
11215 1505190 : && TARGET_EXPR_INITIAL (t) == r))
11216 : should_unshare = false;
11217 :
11218 530892330 : if (non_constant_p && !allow_non_constant)
11219 3882 : return error_mark_node;
11220 530888448 : else if (check_for_failed_contracts (&ctx))
11221 : {
11222 24 : if (manifestly_const_eval == mce_true)
11223 : /* If MCE, we gave a hard error and return error_mark_node. */
11224 21 : return error_mark_node;
11225 : else
11226 : {
11227 : /* Otherwise treat it as non-constant so the violation is still
11228 : detectable at run-time. */
11229 3 : gcc_checking_assert (allow_non_constant);
11230 : return t;
11231 : }
11232 : }
11233 530888424 : else if (non_constant_p && TREE_CONSTANT (r))
11234 142496 : r = mark_non_constant (r);
11235 530745928 : else if (non_constant_p)
11236 : return t;
11237 :
11238 401111394 : if (constexpr_dtor)
11239 : {
11240 165 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
11241 165 : return r;
11242 : }
11243 :
11244 : /* Check we are not trying to return the wrong type. */
11245 401111229 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
11246 : {
11247 : /* If so, this is not a constant expression. */
11248 157 : if (!allow_non_constant)
11249 4 : error ("%qE is not a constant expression because it initializes "
11250 4 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
11251 157 : return t;
11252 : }
11253 :
11254 401111072 : if (should_unshare)
11255 230371845 : r = unshare_expr (r);
11256 :
11257 401111072 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
11258 : {
11259 15293082 : r = adjust_temp_type (type, r);
11260 15293082 : if (TREE_CODE (t) == TARGET_EXPR
11261 15293082 : && TARGET_EXPR_INITIAL (t) == r)
11262 : return t;
11263 14339874 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
11264 2488227 : || TREE_CODE (t) == AGGR_INIT_EXPR)
11265 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
11266 646423 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
11267 749 : r = get_target_expr (r);
11268 : else
11269 : {
11270 645674 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
11271 645674 : TREE_CONSTANT (r) = true;
11272 : }
11273 : }
11274 :
11275 400157864 : if (TREE_CODE (t) == TARGET_EXPR
11276 551982 : && TREE_CODE (r) == TARGET_EXPR)
11277 : {
11278 : /* Preserve this flag for potential_constant_expression, and the others
11279 : for good measure. */
11280 551870 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
11281 551870 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
11282 551870 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
11283 551870 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
11284 : }
11285 :
11286 : /* Remember the original location if that wouldn't need a wrapper. */
11287 400157864 : if (location_t loc = EXPR_LOCATION (t))
11288 187718155 : protected_set_expr_location (r, loc);
11289 :
11290 400157864 : return r;
11291 540843719 : }
11292 :
11293 : /* If T represents a constant expression returns its reduced value.
11294 : Otherwise return error_mark_node. */
11295 :
11296 : tree
11297 149064951 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
11298 : tsubst_flags_t complain /* = tf_error */)
11299 : {
11300 149064951 : bool sfinae = !(complain & tf_error);
11301 149064951 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
11302 149064951 : if (sfinae && !TREE_CONSTANT (r))
11303 2559 : r = error_mark_node;
11304 149064951 : return r;
11305 : }
11306 :
11307 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
11308 : of constexpr variables. The actual initializer of DECL is not modified. */
11309 :
11310 : void
11311 200 : cxx_constant_dtor (tree t, tree decl)
11312 : {
11313 200 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
11314 200 : }
11315 :
11316 : /* Helper routine for fold_simple function. Either return simplified
11317 : expression T, otherwise NULL_TREE.
11318 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
11319 : even if we are within template-declaration. So be careful on call, as in
11320 : such case types can be undefined. */
11321 :
11322 : static tree
11323 168331314 : fold_simple_1 (tree t)
11324 : {
11325 168331314 : tree op1;
11326 168331314 : enum tree_code code = TREE_CODE (t);
11327 :
11328 168331314 : switch (code)
11329 : {
11330 : case INTEGER_CST:
11331 : case REAL_CST:
11332 : case VECTOR_CST:
11333 : case FIXED_CST:
11334 : case COMPLEX_CST:
11335 : return t;
11336 :
11337 1469016 : case SIZEOF_EXPR:
11338 1469016 : return fold_sizeof_expr (t);
11339 :
11340 39832088 : case ABS_EXPR:
11341 39832088 : case ABSU_EXPR:
11342 39832088 : case CONJ_EXPR:
11343 39832088 : case REALPART_EXPR:
11344 39832088 : case IMAGPART_EXPR:
11345 39832088 : case NEGATE_EXPR:
11346 39832088 : case BIT_NOT_EXPR:
11347 39832088 : case TRUTH_NOT_EXPR:
11348 39832088 : case VIEW_CONVERT_EXPR:
11349 39832088 : CASE_CONVERT:
11350 39832088 : case FLOAT_EXPR:
11351 39832088 : case FIX_TRUNC_EXPR:
11352 39832088 : case FIXED_CONVERT_EXPR:
11353 39832088 : case ADDR_SPACE_CONVERT_EXPR:
11354 :
11355 39832088 : op1 = TREE_OPERAND (t, 0);
11356 :
11357 39832088 : t = const_unop (code, TREE_TYPE (t), op1);
11358 39832088 : if (!t)
11359 : return NULL_TREE;
11360 :
11361 5988527 : if (CONVERT_EXPR_CODE_P (code)
11362 5988527 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
11363 0 : TREE_OVERFLOW (t) = false;
11364 : return t;
11365 :
11366 : default:
11367 : return NULL_TREE;
11368 : }
11369 : }
11370 :
11371 : /* If T is a simple constant expression, returns its simplified value.
11372 : Otherwise returns T. In contrast to maybe_constant_value we
11373 : simplify only few operations on constant-expressions, and we don't
11374 : try to simplify constexpressions. */
11375 :
11376 : tree
11377 169096429 : fold_simple (tree t)
11378 : {
11379 169096429 : if (processing_template_decl)
11380 : return t;
11381 :
11382 168331314 : tree r = fold_simple_1 (t);
11383 168331314 : if (r)
11384 : return r;
11385 :
11386 : return t;
11387 : }
11388 :
11389 : /* Try folding the expression T to a simple constant.
11390 : Returns that constant, otherwise returns T. */
11391 :
11392 : tree
11393 1670243 : fold_to_constant (tree t)
11394 : {
11395 1670243 : if (processing_template_decl)
11396 : return t;
11397 :
11398 1573291 : tree r = fold (t);
11399 1573291 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
11400 : return r;
11401 : else
11402 : return t;
11403 : }
11404 :
11405 : /* If T is a constant expression, returns its reduced value.
11406 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
11407 : Otherwise, returns a version of T without TREE_CONSTANT.
11408 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
11409 : as per P0595. */
11410 :
11411 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
11412 :
11413 : tree
11414 857360355 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
11415 : mce_value manifestly_const_eval /* = mce_unknown */)
11416 : {
11417 857360355 : tree orig_t = t;
11418 857360355 : tree r;
11419 :
11420 857360355 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
11421 : {
11422 : /* Look up each operand in the cv_cache first to see if we've already
11423 : reduced it, and reuse that result to avoid quadratic behavior if
11424 : we're called when building up a large expression. */
11425 245399896 : int n = cp_tree_operand_length (t);
11426 245399896 : tree *ops = XALLOCAVEC (tree, n);
11427 245399896 : bool rebuild = false;
11428 689673304 : for (int i = 0; i < n; ++i)
11429 : {
11430 444273408 : ops[i] = TREE_OPERAND (t, i);
11431 1332095217 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
11432 30755580 : if (*cached != ops[i])
11433 : {
11434 9607603 : ops[i] = *cached;
11435 9607603 : rebuild = true;
11436 : }
11437 : }
11438 245399896 : if (rebuild)
11439 : {
11440 8083917 : t = copy_node (t);
11441 26328932 : for (int i = 0; i < n; ++i)
11442 18245015 : TREE_OPERAND (t, i) = ops[i];
11443 : }
11444 : }
11445 :
11446 857360355 : if (!is_nondependent_constant_expression (t))
11447 : {
11448 0 : if (TREE_OVERFLOW_P (t)
11449 127405868 : || (!processing_template_decl && TREE_CONSTANT (t)))
11450 4495 : t = mark_non_constant (t);
11451 127405868 : return t;
11452 : }
11453 729954487 : else if (CONSTANT_CLASS_P (t))
11454 : /* No caching or evaluation needed. */
11455 : return t;
11456 :
11457 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11458 : but at least try folding it to a simple constant. */
11459 331483737 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11460 509031 : return fold_to_constant (t);
11461 :
11462 314606563 : if (manifestly_const_eval != mce_unknown)
11463 : /* TODO: Extend the cache to be mce_value aware. And if we have a
11464 : previously cached mce_unknown result that's TREE_CONSTANT, it means
11465 : the reduced value is independent of mce_value and so we should
11466 : be able to reuse it in the mce_true/false case. */
11467 171773365 : return cxx_eval_outermost_constant_expr (t, true, true,
11468 171770666 : manifestly_const_eval, false, decl);
11469 :
11470 159201341 : if (cv_cache == NULL)
11471 147786 : cv_cache = hash_map<tree, tree>::create_ggc (101);
11472 159201341 : if (tree *cached = cv_cache->get (t))
11473 : {
11474 13260409 : r = *cached;
11475 13260409 : if (r != t)
11476 : {
11477 : /* Clear processing_template_decl for sake of break_out_target_exprs;
11478 : entries in the cv_cache are non-templated. */
11479 4494293 : processing_template_decl_sentinel ptds;
11480 :
11481 4494293 : r = break_out_target_exprs (r, /*clear_loc*/true);
11482 4494293 : protected_set_expr_location (r, EXPR_LOCATION (t));
11483 4494293 : }
11484 13260409 : return r;
11485 : }
11486 :
11487 145940932 : uid_sensitive_constexpr_evaluation_checker c;
11488 145940932 : r = cxx_eval_outermost_constant_expr (t, true, true,
11489 : manifestly_const_eval, false, decl);
11490 145940932 : gcc_checking_assert (r == t
11491 : || CONVERT_EXPR_P (t)
11492 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11493 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11494 : || !cp_tree_equal (r, t));
11495 145940932 : if (!c.evaluation_restricted_p ())
11496 145686668 : cv_cache->put (orig_t, r);
11497 : return r;
11498 : }
11499 :
11500 : /* Dispose of the whole CV_CACHE. */
11501 :
11502 : static void
11503 40953823 : clear_cv_cache (void)
11504 : {
11505 40953823 : if (cv_cache != NULL)
11506 40667196 : cv_cache->empty ();
11507 40953823 : }
11508 :
11509 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
11510 :
11511 : void
11512 40953823 : clear_cv_and_fold_caches ()
11513 : {
11514 40953823 : clear_cv_cache ();
11515 40953823 : clear_fold_cache ();
11516 40953823 : }
11517 :
11518 : /* Internal function handling expressions in templates for
11519 : fold_non_dependent_expr and fold_non_dependent_init.
11520 :
11521 : If we're in a template, but T isn't value dependent, simplify
11522 : it. We're supposed to treat:
11523 :
11524 : template <typename T> void f(T[1 + 1]);
11525 : template <typename T> void f(T[2]);
11526 :
11527 : as two declarations of the same function, for example. */
11528 :
11529 : static tree
11530 31183489 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11531 : bool manifestly_const_eval,
11532 : tree object)
11533 : {
11534 31183489 : gcc_assert (processing_template_decl);
11535 :
11536 31183489 : if (is_nondependent_constant_expression (t))
11537 : {
11538 17351088 : processing_template_decl_sentinel s;
11539 17351088 : t = instantiate_non_dependent_expr_internal (t, complain);
11540 :
11541 17351088 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11542 : {
11543 0 : if (TREE_OVERFLOW_P (t))
11544 : {
11545 0 : t = build_nop (TREE_TYPE (t), t);
11546 0 : TREE_CONSTANT (t) = false;
11547 : }
11548 0 : return t;
11549 : }
11550 17351088 : else if (CONSTANT_CLASS_P (t))
11551 : /* No evaluation needed. */
11552 : return t;
11553 :
11554 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11555 : but at least try folding it to a simple constant. */
11556 4548926 : if (cp_unevaluated_operand && !manifestly_const_eval)
11557 89 : return fold_to_constant (t);
11558 :
11559 4548837 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11560 : mce_value (manifestly_const_eval),
11561 : false, object);
11562 : /* cp_tree_equal looks through NOPs, so allow them. */
11563 4548837 : gcc_checking_assert (r == t
11564 : || CONVERT_EXPR_P (t)
11565 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11566 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11567 : || !cp_tree_equal (r, t));
11568 4548837 : return r;
11569 17351088 : }
11570 13832401 : else if (TREE_OVERFLOW_P (t))
11571 : {
11572 0 : t = build_nop (TREE_TYPE (t), t);
11573 0 : TREE_CONSTANT (t) = false;
11574 : }
11575 :
11576 : return t;
11577 : }
11578 :
11579 : /* Like maybe_constant_value but first fully instantiate the argument.
11580 :
11581 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11582 : followed by maybe_constant_value but is more efficient,
11583 : because it calls instantiation_dependent_expression_p and
11584 : potential_constant_expression at most once.
11585 : The manifestly_const_eval argument is passed to maybe_constant_value.
11586 :
11587 : Callers should generally pass their active complain, or if they are in a
11588 : non-template, diagnosing context, they can use the default of
11589 : tf_warning_or_error. Callers that might be within a template context, don't
11590 : have a complain parameter, and aren't going to remember the result for long
11591 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11592 : appropriately. */
11593 :
11594 : tree
11595 260165546 : fold_non_dependent_expr (tree t,
11596 : tsubst_flags_t complain /* = tf_warning_or_error */,
11597 : bool manifestly_const_eval /* = false */,
11598 : tree object /* = NULL_TREE */)
11599 : {
11600 260165546 : if (t == NULL_TREE)
11601 : return NULL_TREE;
11602 :
11603 259046763 : if (processing_template_decl)
11604 30084712 : return fold_non_dependent_expr_template (t, complain,
11605 30084712 : manifestly_const_eval, object);
11606 :
11607 228962051 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11608 : }
11609 :
11610 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11611 : return the original expression. */
11612 :
11613 : tree
11614 2389386 : maybe_fold_non_dependent_expr (tree expr,
11615 : tsubst_flags_t complain/*=tf_warning_or_error*/)
11616 : {
11617 2389386 : tree t = fold_non_dependent_expr (expr, complain);
11618 2389386 : if (t && TREE_CONSTANT (t))
11619 1147430 : return t;
11620 :
11621 : return expr;
11622 : }
11623 :
11624 : /* Like maybe_constant_init but first fully instantiate the argument. */
11625 :
11626 : tree
11627 62479091 : fold_non_dependent_init (tree t,
11628 : tsubst_flags_t complain /*=tf_warning_or_error*/,
11629 : bool manifestly_const_eval /*=false*/,
11630 : tree object /* = NULL_TREE */)
11631 : {
11632 62479091 : if (t == NULL_TREE)
11633 : return NULL_TREE;
11634 :
11635 62479091 : if (processing_template_decl)
11636 : {
11637 1098777 : t = fold_non_dependent_expr_template (t, complain,
11638 : manifestly_const_eval, object);
11639 : /* maybe_constant_init does this stripping, so do it here too. */
11640 1098777 : if (TREE_CODE (t) == TARGET_EXPR)
11641 : {
11642 2107 : tree init = TARGET_EXPR_INITIAL (t);
11643 2107 : if (TREE_CODE (init) == CONSTRUCTOR)
11644 1098777 : t = init;
11645 : }
11646 1098777 : return t;
11647 : }
11648 :
11649 61380314 : return maybe_constant_init (t, object, manifestly_const_eval);
11650 : }
11651 :
11652 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11653 : than wrapped in a TARGET_EXPR.
11654 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11655 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11656 : per P0595 even when ALLOW_NON_CONSTANT is true. */
11657 :
11658 : static tree
11659 120072494 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11660 : mce_value manifestly_const_eval)
11661 : {
11662 120072494 : if (!t)
11663 : return t;
11664 120072494 : if (TREE_CODE (t) == EXPR_STMT)
11665 252 : t = TREE_OPERAND (t, 0);
11666 120072494 : if (TREE_CODE (t) == CONVERT_EXPR
11667 120072494 : && VOID_TYPE_P (TREE_TYPE (t)))
11668 2677 : t = TREE_OPERAND (t, 0);
11669 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11670 : DECL and losing that information would cause mischief later. */
11671 120072494 : if (TREE_CODE (t) == INIT_EXPR
11672 120072494 : && (!decl
11673 2443 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11674 2443 : TREE_TYPE (t))))
11675 1822 : t = TREE_OPERAND (t, 1);
11676 120072494 : if (TREE_CODE (t) == TARGET_EXPR)
11677 555023 : t = TARGET_EXPR_INITIAL (t);
11678 120072494 : if (!is_nondependent_static_init_expression (t))
11679 : /* Don't try to evaluate it. */;
11680 111700851 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11681 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11682 : else
11683 : {
11684 : /* [basic.start.static] allows constant-initialization of variables with
11685 : static or thread storage duration even if it isn't required, but we
11686 : shouldn't bend the rules the same way for automatic variables.
11687 :
11688 : But still enforce the requirements of constexpr/constinit.
11689 : [dcl.constinit] "If a variable declared with the constinit specifier
11690 : has dynamic initialization, the program is ill-formed, even if the
11691 : implementation would perform that initialization as a static
11692 : initialization." */
11693 20215226 : bool is_static = (decl && DECL_P (decl)
11694 65646013 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11695 3423719 : bool strict = (!is_static
11696 : || (decl && DECL_P (decl)
11697 3423719 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11698 986693 : || DECL_DECLARED_CONSTINIT_P (decl))));
11699 : if (is_static)
11700 : manifestly_const_eval = mce_true;
11701 :
11702 46195192 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11703 88941 : return fold_to_constant (t);
11704 :
11705 46106251 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11706 : manifestly_const_eval,
11707 : false, decl);
11708 : }
11709 119983553 : if (TREE_CODE (t) == TARGET_EXPR)
11710 : {
11711 93994 : tree init = TARGET_EXPR_INITIAL (t);
11712 93994 : if (TREE_CODE (init) == CONSTRUCTOR)
11713 120072494 : t = init;
11714 : }
11715 : return t;
11716 : }
11717 :
11718 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11719 :
11720 : tree
11721 99296118 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11722 : {
11723 99296118 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11724 : }
11725 :
11726 : tree
11727 20774300 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11728 : {
11729 20774300 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11730 : }
11731 :
11732 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11733 :
11734 : tree
11735 2076 : cxx_constant_init (tree t, tree decl)
11736 : {
11737 2076 : return maybe_constant_init_1 (t, decl, false, mce_true);
11738 : }
11739 :
11740 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11741 :
11742 : static bool
11743 209934848 : callee_might_throw (tree t)
11744 : {
11745 209934848 : if (cxx_dialect < cxx26 || !flag_exceptions)
11746 : return false;
11747 32410267 : tree callee = cp_get_callee (t);
11748 32410267 : if (callee == NULL_TREE)
11749 : return false;
11750 32355162 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11751 32355162 : return (!flag_enforce_eh_specs
11752 32355162 : || type_dependent_expression_p (callee)
11753 29444257 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11754 59763578 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11755 12791633 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11756 : }
11757 :
11758 : #if 0
11759 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11760 : /* Return true if the object referred to by REF has automatic or thread
11761 : local storage. */
11762 :
11763 : enum { ck_ok, ck_bad, ck_unknown };
11764 : static int
11765 : check_automatic_or_tls (tree ref)
11766 : {
11767 : machine_mode mode;
11768 : poly_int64 bitsize, bitpos;
11769 : tree offset;
11770 : int volatilep = 0, unsignedp = 0;
11771 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11772 : &mode, &unsignedp, &volatilep, false);
11773 : duration_kind dk;
11774 :
11775 : /* If there isn't a decl in the middle, we don't know the linkage here,
11776 : and this isn't a constant expression anyway. */
11777 : if (!DECL_P (decl))
11778 : return ck_unknown;
11779 : dk = decl_storage_duration (decl);
11780 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11781 : }
11782 : #endif
11783 :
11784 : /* Data structure for passing data from potential_constant_expression_1
11785 : to check_for_return_continue via cp_walk_tree. */
11786 : struct check_for_return_continue_data {
11787 : hash_set<tree> *pset;
11788 : tree continue_stmt;
11789 : tree break_stmt;
11790 : bool could_throw;
11791 : };
11792 :
11793 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11794 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11795 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11796 : For C++26 also note presence of possibly throwing calls. */
11797 : static tree
11798 49997718 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11799 : {
11800 49997718 : tree t = *tp, s, b;
11801 49997718 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11802 49997718 : switch (TREE_CODE (t))
11803 : {
11804 : case RETURN_EXPR:
11805 : return t;
11806 :
11807 28 : case CONTINUE_STMT:
11808 28 : if (d->continue_stmt == NULL_TREE)
11809 28 : d->continue_stmt = t;
11810 : break;
11811 :
11812 3210 : case BREAK_STMT:
11813 3210 : if (d->break_stmt == NULL_TREE)
11814 1335 : d->break_stmt = t;
11815 : break;
11816 :
11817 : #define RECUR(x) \
11818 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11819 : d->pset)) \
11820 : return r
11821 :
11822 : /* For loops, walk subtrees manually, so that continue stmts found
11823 : inside of the bodies of the loops are ignored. */
11824 42033 : case DO_STMT:
11825 42033 : *walk_subtrees = 0;
11826 42033 : RECUR (DO_COND (t));
11827 42033 : s = d->continue_stmt;
11828 42033 : b = d->break_stmt;
11829 42033 : RECUR (DO_BODY (t));
11830 42033 : d->continue_stmt = s;
11831 42033 : d->break_stmt = b;
11832 42033 : break;
11833 :
11834 123 : case WHILE_STMT:
11835 123 : *walk_subtrees = 0;
11836 123 : RECUR (WHILE_COND_PREP (t));
11837 123 : RECUR (WHILE_COND (t));
11838 123 : s = d->continue_stmt;
11839 123 : b = d->break_stmt;
11840 123 : RECUR (WHILE_BODY (t));
11841 109 : d->continue_stmt = s;
11842 109 : d->break_stmt = b;
11843 109 : break;
11844 :
11845 378 : case FOR_STMT:
11846 378 : *walk_subtrees = 0;
11847 378 : RECUR (FOR_INIT_STMT (t));
11848 378 : RECUR (FOR_COND_PREP (t));
11849 378 : RECUR (FOR_COND (t));
11850 378 : RECUR (FOR_EXPR (t));
11851 378 : s = d->continue_stmt;
11852 378 : b = d->break_stmt;
11853 378 : RECUR (FOR_BODY (t));
11854 348 : d->continue_stmt = s;
11855 348 : d->break_stmt = b;
11856 348 : break;
11857 :
11858 0 : case RANGE_FOR_STMT:
11859 0 : *walk_subtrees = 0;
11860 0 : RECUR (RANGE_FOR_EXPR (t));
11861 0 : s = d->continue_stmt;
11862 0 : b = d->break_stmt;
11863 0 : RECUR (RANGE_FOR_BODY (t));
11864 0 : d->continue_stmt = s;
11865 0 : d->break_stmt = b;
11866 0 : break;
11867 :
11868 209 : case SWITCH_STMT:
11869 209 : *walk_subtrees = 0;
11870 209 : RECUR (SWITCH_STMT_COND (t));
11871 209 : b = d->break_stmt;
11872 209 : RECUR (SWITCH_STMT_BODY (t));
11873 189 : d->break_stmt = b;
11874 189 : break;
11875 : #undef RECUR
11876 :
11877 : case STATEMENT_LIST:
11878 : case CONSTRUCTOR:
11879 : break;
11880 :
11881 2562933 : case AGGR_INIT_EXPR:
11882 2562933 : case CALL_EXPR:
11883 : /* In C++26 a function could throw. */
11884 2562933 : if (callee_might_throw (t))
11885 121839 : d->could_throw = true;
11886 : break;
11887 :
11888 45985045 : default:
11889 45985045 : if (!EXPR_P (t))
11890 14150322 : *walk_subtrees = 0;
11891 : break;
11892 : }
11893 :
11894 : return NULL_TREE;
11895 : }
11896 :
11897 : /* Return true if T denotes a potentially constant expression. Issue
11898 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11899 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11900 : consider the expression in the current context, independent of constexpr
11901 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11902 : and hard errors should not be reported by constexpr_error.
11903 :
11904 : C++0x [expr.const] used to say
11905 :
11906 : 6 An expression is a potential constant expression if it is
11907 : a constant expression where all occurrences of function
11908 : parameters are replaced by arbitrary constant expressions
11909 : of the appropriate type.
11910 :
11911 : 2 A conditional expression is a constant expression unless it
11912 : involves one of the following as a potentially evaluated
11913 : subexpression (3.2), but subexpressions of logical AND (5.14),
11914 : logical OR (5.15), and conditional (5.16) operations that are
11915 : not evaluated are not considered. */
11916 :
11917 : static bool
11918 4667306935 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11919 : bool fundef_p, tsubst_flags_t flags,
11920 : tree *jump_target)
11921 : {
11922 : #define RECUR(T,RV) \
11923 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11924 : jump_target)
11925 :
11926 4667306935 : enum { any = false, rval = true };
11927 4667306935 : int i;
11928 4667306935 : tree tmp;
11929 :
11930 4667306935 : if (t == error_mark_node)
11931 : return false;
11932 4667294670 : if (t == NULL_TREE)
11933 : return true;
11934 4659313443 : location_t loc = cp_expr_loc_or_input_loc (t);
11935 4659313443 : iloc_sentinel ils = loc;
11936 :
11937 4659313443 : if (*jump_target)
11938 : /* If we are jumping, ignore everything. This is simpler than the
11939 : cxx_eval_constant_expression handling because we only need to be
11940 : conservatively correct, and we don't necessarily have a constant value
11941 : available, so we don't bother with switch tracking. */
11942 : return true;
11943 :
11944 1836821 : if (TREE_THIS_VOLATILE (t) && want_rval
11945 1251388 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11946 4650014579 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11947 : {
11948 78138 : if (TREE_CLOBBER_P (t))
11949 : {
11950 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
11951 0 : gcc_checking_assert (false);
11952 : return true;
11953 : }
11954 :
11955 78138 : if (flags & tf_error)
11956 25 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11957 : "a volatile lvalue %qE with type %qT", t,
11958 25 : TREE_TYPE (t));
11959 78138 : return false;
11960 : }
11961 4649858277 : if (CONSTANT_CLASS_P (t))
11962 : return true;
11963 3337266866 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11964 3337266866 : && TREE_TYPE (t) == error_mark_node)
11965 : return false;
11966 :
11967 3337266823 : switch (TREE_CODE (t))
11968 : {
11969 : case FUNCTION_DECL:
11970 : case BASELINK:
11971 : case TEMPLATE_DECL:
11972 : case OVERLOAD:
11973 : case TEMPLATE_ID_EXPR:
11974 : case LABEL_DECL:
11975 : case CASE_LABEL_EXPR:
11976 : case PREDICT_EXPR:
11977 : case CONST_DECL:
11978 : case SIZEOF_EXPR:
11979 : case ALIGNOF_EXPR:
11980 : case OFFSETOF_EXPR:
11981 : case NOEXCEPT_EXPR:
11982 : case TEMPLATE_PARM_INDEX:
11983 : case TRAIT_EXPR:
11984 : case IDENTIFIER_NODE:
11985 : case USERDEF_LITERAL:
11986 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11987 : case FIELD_DECL:
11988 : case RESULT_DECL:
11989 : case USING_DECL:
11990 : case USING_STMT:
11991 : case PLACEHOLDER_EXPR:
11992 : case REQUIRES_EXPR:
11993 : case STATIC_ASSERT:
11994 : case DEBUG_BEGIN_STMT:
11995 : case REFLECT_EXPR:
11996 : return true;
11997 :
11998 24739837 : case RETURN_EXPR:
11999 24739837 : if (!RECUR (TREE_OPERAND (t, 0), any))
12000 : return false;
12001 : /* FALLTHROUGH */
12002 :
12003 24586897 : case BREAK_STMT:
12004 24586897 : case CONTINUE_STMT:
12005 24586897 : *jump_target = t;
12006 24586897 : return true;
12007 :
12008 347337955 : case PARM_DECL:
12009 347337955 : if (now && want_rval)
12010 : {
12011 87186695 : tree type = TREE_TYPE (t);
12012 87186695 : if (dependent_type_p (type)
12013 58412327 : || !COMPLETE_TYPE_P (processing_template_decl
12014 : ? type : complete_type (type))
12015 145599022 : || is_really_empty_class (type, /*ignore_vptr*/false))
12016 : /* An empty class has no data to read. */
12017 28775048 : return true;
12018 58411647 : if (flags & tf_error)
12019 19 : constexpr_error (input_location, fundef_p,
12020 : "%qE is not a constant expression", t);
12021 58411647 : return false;
12022 : }
12023 : return true;
12024 :
12025 252242434 : case AGGR_INIT_EXPR:
12026 252242434 : case CALL_EXPR:
12027 : /* -- an invocation of a function other than a constexpr function
12028 : or a constexpr constructor. */
12029 252242434 : {
12030 252242434 : tree fun = get_function_named_in_call (t);
12031 252242434 : const int nargs = call_expr_nargs (t);
12032 252242434 : i = 0;
12033 :
12034 252242434 : if (fun == NULL_TREE)
12035 : {
12036 : /* Reset to allow the function to continue past the end
12037 : of the block below. Otherwise return early. */
12038 423822 : bool bail = true;
12039 :
12040 423822 : if (TREE_CODE (t) == CALL_EXPR
12041 423822 : && CALL_EXPR_FN (t) == NULL_TREE)
12042 423822 : switch (CALL_EXPR_IFN (t))
12043 : {
12044 : /* These should be ignored, they are optimized away from
12045 : constexpr functions. */
12046 : case IFN_UBSAN_NULL:
12047 : case IFN_UBSAN_BOUNDS:
12048 : case IFN_UBSAN_VPTR:
12049 : case IFN_FALLTHROUGH:
12050 : case IFN_ASSUME:
12051 : return true;
12052 :
12053 : case IFN_ADD_OVERFLOW:
12054 : case IFN_SUB_OVERFLOW:
12055 : case IFN_MUL_OVERFLOW:
12056 : case IFN_LAUNDER:
12057 : case IFN_VEC_CONVERT:
12058 : case IFN_BSWAP:
12059 : case IFN_BITREVERSE:
12060 : bail = false;
12061 : break;
12062 :
12063 : default:
12064 : break;
12065 : }
12066 :
12067 : if (bail)
12068 : {
12069 : /* fold_call_expr can't do anything with IFN calls. */
12070 81 : if (flags & tf_error)
12071 0 : constexpr_error (loc, fundef_p,
12072 : "call to internal function %qE", t);
12073 81 : return false;
12074 : }
12075 : }
12076 :
12077 251818612 : if (fun && is_overloaded_fn (fun))
12078 : {
12079 233547140 : if (!RECUR (fun, true))
12080 : return false;
12081 233104498 : fun = get_fns (fun);
12082 :
12083 233104498 : if (TREE_CODE (fun) == FUNCTION_DECL)
12084 : {
12085 215366616 : if (builtin_valid_in_constant_expr_p (fun))
12086 : return true;
12087 213579984 : if (!maybe_constexpr_fn (fun)
12088 : /* Allow any built-in function; if the expansion
12089 : isn't constant, we'll deal with that then. */
12090 41144590 : && !fndecl_built_in_p (fun)
12091 : /* In C++20, replaceable global allocation functions
12092 : are constant expressions. */
12093 27909195 : && (!cxx_replaceable_global_alloc_fn (fun)
12094 747686 : || TREE_CODE (t) != CALL_EXPR
12095 747686 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
12096 277857 : && (current_function_decl == NULL_TREE
12097 277857 : || !is_std_allocator_allocate
12098 277857 : (current_function_decl))))
12099 : /* Allow placement new in std::construct_at. */
12100 27166453 : && (!cxx_placement_new_fn (fun)
12101 390690 : || TREE_CODE (t) != CALL_EXPR
12102 390690 : || current_function_decl == NULL_TREE
12103 390678 : || !is_std_construct_at (current_function_decl))
12104 26895241 : && !cxx_dynamic_cast_fn_p (fun)
12105 240471640 : && !cxx_cxa_builtin_fn_p (fun))
12106 : {
12107 : /* In C++26 evaluation of the function arguments might
12108 : throw and in that case it is irrelevant whether
12109 : fun is constexpr or not. */
12110 26842379 : if (cxx_dialect >= cxx26)
12111 5670484 : for (; i < nargs; ++i)
12112 : {
12113 3242372 : tree x = get_nth_callarg (t, i);
12114 3242372 : bool rv = processing_template_decl ? any : rval;
12115 3242372 : bool sub_now = false;
12116 3242372 : if (!potential_constant_expression_1 (x, rv, strict,
12117 : sub_now,
12118 : fundef_p,
12119 : flags,
12120 : jump_target))
12121 : return false;
12122 3052517 : if (throws (jump_target))
12123 : return true;
12124 : }
12125 26626167 : if ((flags & tf_error)
12126 26626167 : && constexpr_error (loc, fundef_p,
12127 : "call to non-%<constexpr%> "
12128 : "function %qD", fun))
12129 272 : explain_invalid_constexpr_fn (fun);
12130 26626167 : return false;
12131 : }
12132 : }
12133 :
12134 204475487 : fun = OVL_FIRST (fun);
12135 : /* Skip initial arguments to base constructors. */
12136 204475487 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12137 4151273 : i = num_artificial_parms_for (fun);
12138 : }
12139 18271472 : else if (fun)
12140 : {
12141 18271472 : if (TREE_TYPE (fun)
12142 18271472 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12143 : want_rval = rval;
12144 : else
12145 : want_rval = any;
12146 18271472 : if (RECUR (fun, want_rval))
12147 : /* Might end up being a constant function pointer. But it
12148 : could also be a function object with constexpr op(), so
12149 : we pass 'any' so that the underlying VAR_DECL is deemed
12150 : as potentially-constant even though it wasn't declared
12151 : constexpr. */;
12152 : else
12153 : return false;
12154 : }
12155 487303578 : for (; i < nargs; ++i)
12156 : {
12157 279877904 : tree x = get_nth_callarg (t, i);
12158 : /* In a template, reference arguments haven't been converted to
12159 : REFERENCE_TYPE and we might not even know if the parameter
12160 : is a reference, so accept lvalue constants too. */
12161 279877904 : bool rv = processing_template_decl ? any : rval;
12162 : /* Don't require an immediately constant value, as constexpr
12163 : substitution might not use the value of the argument. */
12164 279877904 : bool sub_now = false;
12165 279877904 : if (!potential_constant_expression_1 (x, rv, strict,
12166 : sub_now, fundef_p, flags,
12167 : jump_target))
12168 : return false;
12169 3052558775 : if (throws (jump_target))
12170 : return true;
12171 : }
12172 : /* In C++26 a function could throw. */
12173 207425674 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12174 10562121 : *jump_target = void_node;
12175 : return true;
12176 : }
12177 :
12178 198103593 : case NON_LVALUE_EXPR:
12179 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
12180 : -- an lvalue of integral type that refers to a non-volatile
12181 : const variable or static data member initialized with
12182 : constant expressions, or
12183 :
12184 : -- an lvalue of literal type that refers to non-volatile
12185 : object defined with constexpr, or that refers to a
12186 : sub-object of such an object; */
12187 198103593 : return RECUR (TREE_OPERAND (t, 0), rval);
12188 :
12189 39694 : case EXCESS_PRECISION_EXPR:
12190 39694 : return RECUR (TREE_OPERAND (t, 0), rval);
12191 :
12192 302832683 : case VAR_DECL:
12193 302832683 : if (DECL_HAS_VALUE_EXPR_P (t))
12194 : {
12195 4783022 : if (now && is_normal_capture_proxy (t))
12196 : {
12197 : /* -- in a lambda-expression, a reference to this or to a
12198 : variable with automatic storage duration defined outside that
12199 : lambda-expression, where the reference would be an
12200 : odr-use. */
12201 :
12202 399881 : if (want_rval)
12203 : /* Since we're doing an lvalue-rvalue conversion, this might
12204 : not be an odr-use, so evaluate the variable directly. */
12205 398371 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
12206 :
12207 1510 : if (flags & tf_error)
12208 : {
12209 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
12210 3 : auto_diagnostic_group d;
12211 3 : if (constexpr_error (input_location, fundef_p,
12212 : "lambda capture of %qE is not a "
12213 : "constant expression", cap)
12214 3 : && decl_constant_var_p (cap))
12215 3 : inform (input_location, "because it is used as a glvalue");
12216 3 : }
12217 1510 : return false;
12218 : }
12219 4383141 : tree ve = DECL_VALUE_EXPR (t);
12220 : /* Treat __PRETTY_FUNCTION__ inside a template function as
12221 : potentially-constant. */
12222 4383141 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
12223 : return true;
12224 4383136 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
12225 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
12226 4380400 : return RECUR (ve, rval);
12227 : }
12228 298049661 : if (want_rval
12229 212593742 : && (now || !var_in_maybe_constexpr_fn (t))
12230 194474614 : && !type_dependent_expression_p (t)
12231 162807593 : && !decl_maybe_constant_var_p (t)
12232 51298648 : && !is_local_temp (t)
12233 50753981 : && (strict
12234 1235534 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
12235 622701 : || (DECL_INITIAL (t)
12236 619485 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
12237 50750238 : && COMPLETE_TYPE_P (TREE_TYPE (t))
12238 348799749 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
12239 : {
12240 50745279 : if (flags & tf_error)
12241 167 : non_const_var_error (loc, t, fundef_p);
12242 50745279 : return false;
12243 : }
12244 : return true;
12245 :
12246 406033189 : case NOP_EXPR:
12247 406033189 : if (REINTERPRET_CAST_P (t))
12248 : {
12249 162239 : if (flags & tf_error)
12250 49 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
12251 : "constant expression");
12252 162239 : return false;
12253 : }
12254 : /* FALLTHRU */
12255 841276387 : case CONVERT_EXPR:
12256 841276387 : case VIEW_CONVERT_EXPR:
12257 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
12258 : may change to something more specific to type-punning (DR 1312). */
12259 841276387 : {
12260 841276387 : tree from = TREE_OPERAND (t, 0);
12261 841276387 : if (location_wrapper_p (t))
12262 361610813 : return (RECUR (from, want_rval));
12263 479665574 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
12264 : {
12265 265224600 : STRIP_ANY_LOCATION_WRAPPER (from);
12266 265224600 : if (TREE_CODE (from) == INTEGER_CST
12267 265224600 : && !integer_zerop (from))
12268 : {
12269 1625 : if (flags & tf_error)
12270 28 : constexpr_error (loc, fundef_p,
12271 : "%<reinterpret_cast%> from integer to "
12272 : "pointer");
12273 1625 : return false;
12274 : }
12275 : }
12276 479663949 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
12277 : }
12278 :
12279 12872 : case ADDRESSOF_EXPR:
12280 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
12281 12872 : t = TREE_OPERAND (t, 0);
12282 12872 : goto handle_addr_expr;
12283 :
12284 89544869 : case ADDR_EXPR:
12285 : /* -- a unary operator & that is applied to an lvalue that
12286 : designates an object with thread or automatic storage
12287 : duration; */
12288 89544869 : t = TREE_OPERAND (t, 0);
12289 :
12290 89544869 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
12291 : /* A pointer-to-member constant. */
12292 : return true;
12293 :
12294 89557479 : handle_addr_expr:
12295 : #if 0
12296 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
12297 : any checking here, as we might dereference the pointer later. If
12298 : we remove this code, also remove check_automatic_or_tls. */
12299 : i = check_automatic_or_tls (t);
12300 : if (i == ck_ok)
12301 : return true;
12302 : if (i == ck_bad)
12303 : {
12304 : if (flags & tf_error)
12305 : error ("address-of an object %qE with thread local or "
12306 : "automatic storage is not a constant expression", t);
12307 : return false;
12308 : }
12309 : #endif
12310 89557479 : return RECUR (t, any);
12311 :
12312 98501202 : case COMPONENT_REF:
12313 98501202 : case ARROW_EXPR:
12314 98501202 : case OFFSET_REF:
12315 : /* -- a class member access unless its postfix-expression is
12316 : of literal type or of pointer to literal type. */
12317 : /* This test would be redundant, as it follows from the
12318 : postfix-expression being a potential constant expression. */
12319 98501202 : if (type_unknown_p (t))
12320 : return true;
12321 90611796 : if (is_overloaded_fn (t))
12322 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
12323 : which uses ob as an lvalue. */
12324 92371709 : want_rval = false;
12325 92371709 : gcc_fallthrough ();
12326 :
12327 92371709 : case REALPART_EXPR:
12328 92371709 : case IMAGPART_EXPR:
12329 92371709 : case BIT_FIELD_REF:
12330 92371709 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12331 :
12332 274941 : case EXPR_PACK_EXPANSION:
12333 274941 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
12334 :
12335 : case PACK_INDEX_EXPR:
12336 : return true;
12337 :
12338 99974206 : case INDIRECT_REF:
12339 99974206 : return RECUR (TREE_OPERAND (t, 0), rval);
12340 :
12341 25429923 : case STATEMENT_LIST:
12342 4759331837 : for (tree stmt : tsi_range (t))
12343 75107682 : if (!RECUR (stmt, any))
12344 263299476 : return false;
12345 : return true;
12346 :
12347 4659686 : case MODIFY_EXPR:
12348 4659686 : if (cxx_dialect < cxx14)
12349 1976 : goto fail;
12350 4657710 : if (!RECUR (TREE_OPERAND (t, 0), any))
12351 : return false;
12352 : /* Just ignore clobbers. */
12353 4213324 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12354 : return true;
12355 3735847 : if (!RECUR (TREE_OPERAND (t, 1), rval))
12356 : return false;
12357 : return true;
12358 :
12359 891046 : case MODOP_EXPR:
12360 891046 : if (cxx_dialect < cxx14)
12361 98 : goto fail;
12362 890948 : if (!RECUR (TREE_OPERAND (t, 0), rval))
12363 : return false;
12364 880420 : if (!RECUR (TREE_OPERAND (t, 2), rval))
12365 : return false;
12366 : return true;
12367 :
12368 547978 : case DO_STMT:
12369 547978 : if (!RECUR (DO_COND (t), rval))
12370 : return false;
12371 547978 : if (!RECUR (DO_BODY (t), any))
12372 : return false;
12373 547371 : if (breaks (jump_target) || continues (jump_target))
12374 2 : *jump_target = NULL_TREE;
12375 : return true;
12376 :
12377 450224 : case FOR_STMT:
12378 450224 : if (!RECUR (FOR_INIT_STMT (t), any))
12379 : return false;
12380 450224 : if (!RECUR (FOR_COND_PREP (t), any))
12381 : return false;
12382 450224 : tmp = FOR_COND (t);
12383 450224 : if (!RECUR (tmp, rval))
12384 : return false;
12385 450156 : if (tmp)
12386 : {
12387 401915 : if (!processing_template_decl)
12388 397452 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12389 : /* If we couldn't evaluate the condition, it might not ever be
12390 : true. */
12391 401915 : if (!integer_onep (tmp))
12392 : {
12393 : /* Before returning true, check if the for body can contain
12394 : a return. */
12395 401915 : hash_set<tree> pset;
12396 401915 : check_for_return_continue_data data = { &pset, NULL_TREE,
12397 401915 : NULL_TREE, false };
12398 401915 : if (tree ret_expr
12399 401915 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
12400 : &data, &pset))
12401 140093 : *jump_target = ret_expr;
12402 401915 : if (data.could_throw)
12403 17042 : *jump_target = void_node;
12404 401915 : return true;
12405 401915 : }
12406 : }
12407 48241 : if (!RECUR (FOR_EXPR (t), any))
12408 : return false;
12409 48241 : if (!RECUR (FOR_BODY (t), any))
12410 : return false;
12411 48241 : if (!RECUR (FOR_COND_CLEANUP (t), any))
12412 : return false;
12413 48241 : if (breaks (jump_target) || continues (jump_target))
12414 12 : *jump_target = NULL_TREE;
12415 : return true;
12416 :
12417 478 : case RANGE_FOR_STMT:
12418 478 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
12419 : return false;
12420 478 : if (!RECUR (RANGE_FOR_EXPR (t), any))
12421 : return false;
12422 478 : if (!RECUR (RANGE_FOR_BODY (t), any))
12423 : return false;
12424 476 : if (breaks (jump_target) || continues (jump_target))
12425 0 : *jump_target = NULL_TREE;
12426 : return true;
12427 :
12428 167526 : case WHILE_STMT:
12429 167526 : if (!RECUR (WHILE_COND_PREP (t), any))
12430 : return false;
12431 167526 : tmp = WHILE_COND (t);
12432 167526 : if (!RECUR (tmp, rval))
12433 : return false;
12434 167462 : if (!processing_template_decl)
12435 167425 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12436 : /* If we couldn't evaluate the condition, it might not ever be true. */
12437 167462 : if (!integer_onep (tmp))
12438 : {
12439 : /* Before returning true, check if the while body can contain
12440 : a return. */
12441 157347 : hash_set<tree> pset;
12442 157347 : check_for_return_continue_data data = { &pset, NULL_TREE,
12443 157347 : NULL_TREE, false };
12444 157347 : if (tree ret_expr
12445 157347 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
12446 : &data, &pset))
12447 430 : *jump_target = ret_expr;
12448 157347 : if (data.could_throw)
12449 4340 : *jump_target = void_node;
12450 157347 : return true;
12451 157347 : }
12452 10115 : if (!RECUR (WHILE_BODY (t), any))
12453 : return false;
12454 10104 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
12455 : return false;
12456 10104 : if (breaks (jump_target) || continues (jump_target))
12457 24 : *jump_target = NULL_TREE;
12458 : return true;
12459 :
12460 42660 : case SWITCH_STMT:
12461 42660 : if (!RECUR (SWITCH_STMT_COND (t), rval))
12462 : return false;
12463 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
12464 : unreachable labels would be checked and it is enough if there is
12465 : a single switch cond value for which it is a valid constant
12466 : expression. We need to check if there are any RETURN_EXPRs
12467 : or CONTINUE_STMTs inside of the body though, as in that case
12468 : we need to set *jump_target. */
12469 : else
12470 : {
12471 42654 : hash_set<tree> pset;
12472 42654 : check_for_return_continue_data data = { &pset, NULL_TREE,
12473 42654 : NULL_TREE, false };
12474 42654 : if (tree ret_expr
12475 42654 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
12476 : &data, &pset))
12477 : /* The switch might return. */
12478 42267 : *jump_target = ret_expr;
12479 387 : else if (data.continue_stmt)
12480 : /* The switch can't return, but might continue. */
12481 3 : *jump_target = data.continue_stmt;
12482 42654 : if (data.could_throw)
12483 1815 : *jump_target = void_node;
12484 42654 : }
12485 42654 : return true;
12486 :
12487 50 : case STMT_EXPR:
12488 50 : return RECUR (STMT_EXPR_STMT (t), rval);
12489 :
12490 580224 : case LAMBDA_EXPR:
12491 580224 : if (cxx_dialect >= cxx17)
12492 : /* In C++17 lambdas can be constexpr, don't give up yet. */
12493 : return true;
12494 449 : else if (flags & tf_error)
12495 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
12496 : "constant expression before C++17");
12497 : return false;
12498 :
12499 113604 : case NEW_EXPR:
12500 113604 : case VEC_NEW_EXPR:
12501 113604 : case DELETE_EXPR:
12502 113604 : case VEC_DELETE_EXPR:
12503 113604 : if (cxx_dialect >= cxx20)
12504 : /* In C++20, new-expressions are potentially constant. */
12505 : return true;
12506 1077 : else if (flags & tf_error)
12507 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
12508 : "constant expression before C++20");
12509 : return false;
12510 :
12511 82413 : case DYNAMIC_CAST_EXPR:
12512 82413 : case PSEUDO_DTOR_EXPR:
12513 82413 : case OMP_PARALLEL:
12514 82413 : case OMP_TASK:
12515 82413 : case OMP_FOR:
12516 82413 : case OMP_SIMD:
12517 82413 : case OMP_DISTRIBUTE:
12518 82413 : case OMP_TASKLOOP:
12519 82413 : case OMP_LOOP:
12520 82413 : case OMP_TEAMS:
12521 82413 : case OMP_TARGET_DATA:
12522 82413 : case OMP_TARGET:
12523 82413 : case OMP_SECTIONS:
12524 82413 : case OMP_ORDERED:
12525 82413 : case OMP_CRITICAL:
12526 82413 : case OMP_SINGLE:
12527 82413 : case OMP_SCAN:
12528 82413 : case OMP_SCOPE:
12529 82413 : case OMP_SECTION:
12530 82413 : case OMP_MASTER:
12531 82413 : case OMP_MASKED:
12532 82413 : case OMP_TASKGROUP:
12533 82413 : case OMP_TARGET_UPDATE:
12534 82413 : case OMP_TARGET_ENTER_DATA:
12535 82413 : case OMP_TARGET_EXIT_DATA:
12536 82413 : case OMP_ATOMIC:
12537 82413 : case OMP_ATOMIC_READ:
12538 82413 : case OMP_ATOMIC_CAPTURE_OLD:
12539 82413 : case OMP_ATOMIC_CAPTURE_NEW:
12540 82413 : case OMP_DEPOBJ:
12541 82413 : case OACC_PARALLEL:
12542 82413 : case OACC_KERNELS:
12543 82413 : case OACC_SERIAL:
12544 82413 : case OACC_DATA:
12545 82413 : case OACC_HOST_DATA:
12546 82413 : case OACC_LOOP:
12547 82413 : case OACC_CACHE:
12548 82413 : case OACC_DECLARE:
12549 82413 : case OACC_ENTER_DATA:
12550 82413 : case OACC_EXIT_DATA:
12551 82413 : case OACC_UPDATE:
12552 82413 : case OMP_ARRAY_SECTION:
12553 : /* GCC internal stuff. */
12554 82413 : case VA_ARG_EXPR:
12555 82413 : case TRANSACTION_EXPR:
12556 82413 : case AT_ENCODE_EXPR:
12557 82413 : fail:
12558 82413 : if (flags & tf_error)
12559 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12560 : "expression", t);
12561 : return false;
12562 :
12563 : case OMP_DECLARE_MAPPER:
12564 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12565 : constant. */
12566 : return true;
12567 :
12568 26065 : case THROW_EXPR:
12569 26065 : if (cxx_dialect < cxx26)
12570 278 : goto fail;
12571 25787 : return RECUR (TREE_OPERAND (t, 0), rval);
12572 :
12573 527 : case ASM_EXPR:
12574 527 : if (flags & tf_error)
12575 6 : inline_asm_in_constexpr_error (loc, fundef_p);
12576 : return false;
12577 :
12578 328591 : case OBJ_TYPE_REF:
12579 328591 : if (cxx_dialect >= cxx20)
12580 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12581 : return true;
12582 5596 : else if (flags & tf_error)
12583 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12584 : "%<constexpr%> before C++20");
12585 : return false;
12586 :
12587 6448 : case TYPEID_EXPR:
12588 : /* In C++20, a typeid expression whose operand is of polymorphic
12589 : class type can be constexpr. */
12590 6448 : {
12591 6448 : tree e = TREE_OPERAND (t, 0);
12592 6448 : if (cxx_dialect < cxx20
12593 26 : && strict
12594 26 : && !TYPE_P (e)
12595 19 : && !type_dependent_expression_p (e)
12596 7 : && CLASS_TYPE_P (TREE_TYPE (e))
12597 6453 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12598 : {
12599 4 : if (flags & tf_error)
12600 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12601 : "constant expression because %qE is "
12602 : "of polymorphic type", e);
12603 4 : return false;
12604 : }
12605 : return true;
12606 : }
12607 :
12608 29995933 : case POINTER_DIFF_EXPR:
12609 29995933 : case MINUS_EXPR:
12610 29995933 : want_rval = true;
12611 29995933 : goto binary;
12612 :
12613 79076612 : case LT_EXPR:
12614 79076612 : case LE_EXPR:
12615 79076612 : case GT_EXPR:
12616 79076612 : case GE_EXPR:
12617 79076612 : case EQ_EXPR:
12618 79076612 : case NE_EXPR:
12619 79076612 : case SPACESHIP_EXPR:
12620 79076612 : want_rval = true;
12621 79076612 : goto binary;
12622 :
12623 1863647 : case PREINCREMENT_EXPR:
12624 1863647 : case POSTINCREMENT_EXPR:
12625 1863647 : case PREDECREMENT_EXPR:
12626 1863647 : case POSTDECREMENT_EXPR:
12627 1863647 : if (cxx_dialect < cxx14)
12628 8862 : goto fail;
12629 1854785 : goto unary;
12630 :
12631 1123936 : case BIT_NOT_EXPR:
12632 : /* A destructor. */
12633 1123936 : if (TYPE_P (TREE_OPERAND (t, 0)))
12634 : return true;
12635 : /* fall through. */
12636 :
12637 45226197 : case CONJ_EXPR:
12638 45226197 : case SAVE_EXPR:
12639 45226197 : case FIX_TRUNC_EXPR:
12640 45226197 : case FLOAT_EXPR:
12641 45226197 : case NEGATE_EXPR:
12642 45226197 : case ABS_EXPR:
12643 45226197 : case ABSU_EXPR:
12644 45226197 : case TRUTH_NOT_EXPR:
12645 45226197 : case FIXED_CONVERT_EXPR:
12646 45226197 : case UNARY_PLUS_EXPR:
12647 45226197 : case UNARY_LEFT_FOLD_EXPR:
12648 45226197 : case UNARY_RIGHT_FOLD_EXPR:
12649 45226197 : case VEC_DUPLICATE_EXPR:
12650 1123936 : unary:
12651 45226197 : return RECUR (TREE_OPERAND (t, 0), rval);
12652 :
12653 31293965 : case CAST_EXPR:
12654 31293965 : case CONST_CAST_EXPR:
12655 31293965 : case STATIC_CAST_EXPR:
12656 31293965 : case REINTERPRET_CAST_EXPR:
12657 31293965 : case IMPLICIT_CONV_EXPR:
12658 31293965 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12659 : /* In C++98, a conversion to non-integral type can't be part of a
12660 : constant expression. */
12661 : {
12662 343 : if (flags & tf_error)
12663 0 : constexpr_error (loc, fundef_p,
12664 : "cast to non-integral type %qT in a constant "
12665 0 : "expression", TREE_TYPE (t));
12666 343 : return false;
12667 : }
12668 : /* This might be a conversion from a class to a (potentially) literal
12669 : type. Let's consider it potentially constant since the conversion
12670 : might be a constexpr user-defined conversion. */
12671 31293622 : else if (cxx_dialect >= cxx11
12672 31272024 : && (dependent_type_p (TREE_TYPE (t))
12673 10990735 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12674 10976170 : || literal_type_p (TREE_TYPE (t)))
12675 31233986 : && TREE_OPERAND (t, 0)
12676 62282580 : && (TREE_CODE (t) != CAST_EXPR
12677 23444583 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12678 : {
12679 30945984 : tree from = TREE_OPERAND (t, 0);
12680 30945984 : if (TREE_CODE (t) == CAST_EXPR)
12681 23401609 : from = TREE_VALUE (from);
12682 30945984 : tree type = TREE_TYPE (from);
12683 : /* If this is a dependent type, it could end up being a class
12684 : with conversions. */
12685 30945984 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12686 : return true;
12687 : /* Or a non-dependent class which has conversions. */
12688 620886 : else if (CLASS_TYPE_P (type)
12689 620886 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12690 269159 : return true;
12691 : }
12692 :
12693 24877717 : return (RECUR (TREE_OPERAND (t, 0),
12694 24877717 : !TYPE_REF_P (TREE_TYPE (t))));
12695 :
12696 14418485 : case BIND_EXPR:
12697 14418485 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12698 :
12699 70920782 : case CLEANUP_POINT_EXPR:
12700 70920782 : case MUST_NOT_THROW_EXPR:
12701 70920782 : case TRY_CATCH_EXPR:
12702 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12703 : body could throw, even with catch (...) among handlers we'd need
12704 : to analyze them in detail if they couldn't rethrow it. More
12705 : importantly though, throws (jump_target) is just conservative,
12706 : and there could be e.g.
12707 : try
12708 : {
12709 : possibly_throwing_fn (args);
12710 : break;
12711 : }
12712 : catch (...)
12713 : {
12714 : }
12715 : or continue or return instead of break. So, clearing *jump_target
12716 : because we see catch (...) handler might mean we missed break
12717 : etc. */
12718 70920782 : case TRY_BLOCK:
12719 70920782 : case EH_SPEC_BLOCK:
12720 70920782 : case EXPR_STMT:
12721 70920782 : case PAREN_EXPR:
12722 : /* For convenience. */
12723 70920782 : case LOOP_EXPR:
12724 70920782 : case EXIT_EXPR:
12725 70920782 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12726 :
12727 12855129 : case DECL_EXPR:
12728 12855129 : tmp = DECL_EXPR_DECL (t);
12729 8503256 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12730 18944808 : && (processing_template_decl
12731 5616123 : ? !decl_maybe_constant_var_p (tmp)
12732 5142567 : : !decl_constant_var_p (tmp)))
12733 : {
12734 4957819 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12735 : {
12736 8 : if (flags & tf_error)
12737 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12738 : "%qD defined %<thread_local%> in "
12739 : "%<constexpr%> context", tmp);
12740 8 : return false;
12741 : }
12742 4957811 : else if (TREE_STATIC (tmp))
12743 : {
12744 142 : if (flags & tf_error)
12745 34 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12746 : "%qD defined %<static%> in %<constexpr%> "
12747 : "context", tmp);
12748 142 : return false;
12749 : }
12750 4957669 : else if (!check_for_uninitialized_const_var
12751 4957669 : (tmp, /*constexpr_context_p=*/true, flags))
12752 : return false;
12753 : }
12754 12851244 : if (VAR_P (tmp))
12755 8499371 : return RECUR (DECL_INITIAL (tmp), want_rval);
12756 : return true;
12757 :
12758 42613 : case TRY_FINALLY_EXPR:
12759 42613 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12760 42613 : && RECUR (TREE_OPERAND (t, 1), any));
12761 :
12762 0 : case EH_ELSE_EXPR:
12763 : /* maybe_apply_function_contracts uses this to check postconditions only
12764 : on normal return. */
12765 0 : return (RECUR (TREE_OPERAND (t, 1), any)
12766 0 : || RECUR (TREE_OPERAND (t, 0), any));
12767 :
12768 24607699 : case SCOPE_REF:
12769 24607699 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12770 :
12771 43277857 : case TARGET_EXPR:
12772 43277857 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12773 43072125 : && !TARGET_EXPR_ELIDING_P (t)
12774 77449102 : && !literal_type_p (TREE_TYPE (t)))
12775 : {
12776 803554 : if (flags & tf_error)
12777 : {
12778 25 : auto_diagnostic_group d;
12779 25 : if (constexpr_error (loc, fundef_p,
12780 : "temporary of non-literal type %qT in a "
12781 25 : "constant expression", TREE_TYPE (t)))
12782 25 : explain_non_literal_class (TREE_TYPE (t));
12783 25 : }
12784 803554 : return false;
12785 : }
12786 : /* FALLTHRU */
12787 70966231 : case INIT_EXPR:
12788 70966231 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12789 : return true;
12790 70899538 : return RECUR (TREE_OPERAND (t, 1), rval);
12791 :
12792 37354274 : case CONSTRUCTOR:
12793 37354274 : {
12794 37354274 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12795 37354274 : constructor_elt *ce;
12796 4924102983 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12797 228483218 : if (!RECUR (ce->value, want_rval))
12798 : return false;
12799 : return true;
12800 : }
12801 :
12802 21048863 : case TREE_LIST:
12803 21048863 : {
12804 21048863 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12805 : || DECL_P (TREE_PURPOSE (t)));
12806 21048863 : if (!RECUR (TREE_VALUE (t), want_rval))
12807 : return false;
12808 18864973 : if (TREE_CHAIN (t) == NULL_TREE)
12809 : return true;
12810 55015 : return RECUR (TREE_CHAIN (t), want_rval);
12811 : }
12812 :
12813 15246021 : case TRUNC_DIV_EXPR:
12814 15246021 : case CEIL_DIV_EXPR:
12815 15246021 : case FLOOR_DIV_EXPR:
12816 15246021 : case ROUND_DIV_EXPR:
12817 15246021 : case TRUNC_MOD_EXPR:
12818 15246021 : case CEIL_MOD_EXPR:
12819 15246021 : case ROUND_MOD_EXPR:
12820 15246021 : {
12821 15246021 : tree denom = TREE_OPERAND (t, 1);
12822 15246021 : if (!RECUR (denom, rval))
12823 : return false;
12824 : /* We can't call cxx_eval_outermost_constant_expr on an expression
12825 : that hasn't been through instantiate_non_dependent_expr yet. */
12826 14783147 : if (!processing_template_decl)
12827 7798218 : denom = cxx_eval_outermost_constant_expr (denom, true);
12828 14783147 : if (integer_zerop (denom))
12829 : {
12830 352 : if (flags & tf_error)
12831 38 : constexpr_error (input_location, fundef_p,
12832 : "division by zero is not a constant expression");
12833 352 : return false;
12834 : }
12835 : else
12836 : {
12837 14782795 : want_rval = true;
12838 14782795 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12839 : }
12840 : }
12841 :
12842 6995056 : case COMPOUND_EXPR:
12843 6995056 : {
12844 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12845 : COMPOUND_EXPR; don't get confused. */
12846 6995056 : tree op0 = TREE_OPERAND (t, 0);
12847 6995056 : tree op1 = TREE_OPERAND (t, 1);
12848 6995056 : STRIP_NOPS (op1);
12849 6995056 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12850 1264139 : return RECUR (op0, want_rval);
12851 : else
12852 5730917 : goto binary;
12853 : }
12854 :
12855 : /* If the first operand is the non-short-circuit constant, look at
12856 : the second operand; otherwise we only care about the first one for
12857 : potentiality. */
12858 18077681 : case TRUTH_AND_EXPR:
12859 18077681 : case TRUTH_ANDIF_EXPR:
12860 18077681 : tmp = boolean_true_node;
12861 18077681 : goto truth;
12862 7267007 : case TRUTH_OR_EXPR:
12863 7267007 : case TRUTH_ORIF_EXPR:
12864 7267007 : tmp = boolean_false_node;
12865 25344688 : truth:
12866 25344688 : {
12867 25344688 : tree op0 = TREE_OPERAND (t, 0);
12868 25344688 : tree op1 = TREE_OPERAND (t, 1);
12869 25344688 : if (!RECUR (op0, rval))
12870 : return false;
12871 17530922 : if (!(flags & tf_error) && RECUR (op1, rval))
12872 : /* When quiet, try to avoid expensive trial evaluation by first
12873 : checking potentiality of the second operand. */
12874 : return true;
12875 1777340 : if (!processing_template_decl)
12876 1376219 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12877 1777340 : if (tree_int_cst_equal (op0, tmp))
12878 5193 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12879 : else
12880 : return true;
12881 : }
12882 :
12883 : case PLUS_EXPR:
12884 : case MULT_EXPR:
12885 : case POINTER_PLUS_EXPR:
12886 : case RDIV_EXPR:
12887 : case EXACT_DIV_EXPR:
12888 : case MIN_EXPR:
12889 : case MAX_EXPR:
12890 : case LSHIFT_EXPR:
12891 : case RSHIFT_EXPR:
12892 : case LROTATE_EXPR:
12893 : case RROTATE_EXPR:
12894 : case BIT_IOR_EXPR:
12895 : case BIT_XOR_EXPR:
12896 : case BIT_AND_EXPR:
12897 : case TRUTH_XOR_EXPR:
12898 : case UNORDERED_EXPR:
12899 : case ORDERED_EXPR:
12900 : case UNLT_EXPR:
12901 : case UNLE_EXPR:
12902 : case UNGT_EXPR:
12903 : case UNGE_EXPR:
12904 : case UNEQ_EXPR:
12905 : case LTGT_EXPR:
12906 : case RANGE_EXPR:
12907 : case COMPLEX_EXPR:
12908 232910673 : want_rval = true;
12909 : /* Fall through. */
12910 232910673 : case ARRAY_REF:
12911 232910673 : case ARRAY_RANGE_REF:
12912 232910673 : case MEMBER_REF:
12913 232910673 : case DOTSTAR_EXPR:
12914 232910673 : case MEM_REF:
12915 232910673 : case BINARY_LEFT_FOLD_EXPR:
12916 232910673 : case BINARY_RIGHT_FOLD_EXPR:
12917 232910673 : binary:
12918 518340801 : for (i = 0; i < 2; ++i)
12919 382352671 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12920 : return false;
12921 : return true;
12922 :
12923 : case VEC_PERM_EXPR:
12924 107733 : for (i = 0; i < 3; ++i)
12925 106863 : if (!RECUR (TREE_OPERAND (t, i), true))
12926 : return false;
12927 : return true;
12928 :
12929 6544976 : case COND_EXPR:
12930 6544976 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12931 : {
12932 8 : if (flags & tf_error)
12933 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12934 : "constant expression");
12935 8 : return false;
12936 : }
12937 : /* Fall through. */
12938 17271384 : case IF_STMT:
12939 17271384 : case VEC_COND_EXPR:
12940 : /* If the condition is a known constant, we know which of the legs we
12941 : care about; otherwise we only require that the condition and
12942 : either of the legs be potentially constant. */
12943 17271384 : tmp = TREE_OPERAND (t, 0);
12944 17271384 : if (!RECUR (tmp, rval))
12945 : return false;
12946 15675545 : if (!processing_template_decl)
12947 13672568 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12948 : /* potential_constant_expression* isn't told if it is called for
12949 : manifestly_const_eval or not, so for consteval if always
12950 : process both branches as if the condition is not a known
12951 : constant. */
12952 15675545 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12953 : {
12954 15600582 : if (integer_zerop (tmp))
12955 3485536 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12956 12115046 : else if (TREE_CODE (tmp) == INTEGER_CST)
12957 3805837 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12958 : }
12959 8384172 : tmp = *jump_target;
12960 9564324 : for (i = 1; i < 3; ++i)
12961 : {
12962 9483631 : tree this_jump_target = tmp;
12963 9483631 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12964 : want_rval, strict, now, fundef_p,
12965 : tf_none, &this_jump_target))
12966 : {
12967 8587423 : if (returns (&this_jump_target) || throws (&this_jump_target))
12968 2449383 : *jump_target = this_jump_target;
12969 5854096 : else if (!returns (jump_target) && !throws (jump_target))
12970 : {
12971 5854096 : if (breaks (&this_jump_target)
12972 5854096 : || continues (&this_jump_target))
12973 42 : *jump_target = this_jump_target;
12974 5854096 : if (i == 1)
12975 : {
12976 : /* If the then branch is potentially constant, but
12977 : does not return, check if the else branch
12978 : couldn't return, break or continue. */
12979 4836003 : hash_set<tree> pset;
12980 4836003 : check_for_return_continue_data data = { &pset, NULL_TREE,
12981 : NULL_TREE,
12982 4836003 : false };
12983 9672006 : if (tree ret_expr
12984 4836003 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12985 : check_for_return_continue, &data,
12986 : &pset))
12987 79866 : *jump_target = ret_expr;
12988 4756137 : else if (*jump_target == NULL_TREE)
12989 : {
12990 4756098 : if (data.continue_stmt)
12991 0 : *jump_target = data.continue_stmt;
12992 4756098 : else if (data.break_stmt)
12993 13 : *jump_target = data.break_stmt;
12994 : }
12995 4836003 : if (data.could_throw)
12996 52306 : *jump_target = void_node;
12997 4836003 : }
12998 : }
12999 8303479 : return true;
13000 : }
13001 : }
13002 80693 : if (flags & tf_error)
13003 : {
13004 3 : if (TREE_CODE (t) == IF_STMT)
13005 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
13006 : "constant expression");
13007 : else
13008 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
13009 : "constant expression", t);
13010 : }
13011 : return false;
13012 :
13013 1103 : case VEC_INIT_EXPR:
13014 1103 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
13015 : return true;
13016 453 : if (flags & tf_error)
13017 : {
13018 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
13019 : "initialization"))
13020 2 : diagnose_non_constexpr_vec_init (t);
13021 : }
13022 : return false;
13023 :
13024 : case TYPE_DECL:
13025 : case TAG_DEFN:
13026 : /* We can see these in statement-expressions. */
13027 : return true;
13028 :
13029 1478499 : case CLEANUP_STMT:
13030 1478499 : if (!RECUR (CLEANUP_BODY (t), any))
13031 : return false;
13032 1477972 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
13033 : return false;
13034 : return true;
13035 :
13036 : case EMPTY_CLASS_EXPR:
13037 : return true;
13038 :
13039 29 : case GOTO_EXPR:
13040 29 : {
13041 29 : tree *target = &TREE_OPERAND (t, 0);
13042 : /* Gotos representing break, continue and cdtor return are OK. */
13043 29 : if (breaks (target) || continues (target) || returns (target))
13044 : {
13045 9 : *jump_target = *target;
13046 9 : return true;
13047 : }
13048 20 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
13049 : /* The user didn't write this goto, this isn't the problem. */
13050 : return true;
13051 16 : if (flags & tf_error)
13052 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
13053 : "expression");
13054 : return false;
13055 : }
13056 :
13057 : case ASSERTION_STMT:
13058 : case PRECONDITION_STMT:
13059 : case POSTCONDITION_STMT:
13060 : /* Contracts are not supposed to alter this; we have to check that this
13061 : is not violated at a later time. */
13062 : return true;
13063 :
13064 225 : case LABEL_EXPR:
13065 225 : t = LABEL_EXPR_LABEL (t);
13066 225 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
13067 : return true;
13068 25 : else if (flags & tf_error)
13069 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
13070 : "function only available with %<-std=c++23%> or "
13071 : "%<-std=gnu++23%>");
13072 : return false;
13073 :
13074 10255 : case ANNOTATE_EXPR:
13075 10255 : return RECUR (TREE_OPERAND (t, 0), rval);
13076 :
13077 140121 : case BIT_CAST_EXPR:
13078 140121 : return RECUR (TREE_OPERAND (t, 0), rval);
13079 :
13080 : /* Coroutine await, yield and return expressions are not. */
13081 768 : case CO_AWAIT_EXPR:
13082 768 : case CO_YIELD_EXPR:
13083 768 : case CO_RETURN_EXPR:
13084 768 : case TEMPLATE_FOR_STMT:
13085 768 : if (flags & tf_error)
13086 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
13087 : "%qE is not a constant expression", t);
13088 : return false;
13089 :
13090 : /* Assume a TU-local entity is not constant, we'll error later when
13091 : instantiating. */
13092 : case TU_LOCAL_ENTITY:
13093 : return false;
13094 :
13095 : /* A splice expression is dependent, but will be constant after
13096 : substitution. */
13097 : case SPLICE_EXPR:
13098 : return true;
13099 :
13100 30 : case NONTYPE_ARGUMENT_PACK:
13101 30 : {
13102 30 : tree args = ARGUMENT_PACK_ARGS (t);
13103 30 : int len = TREE_VEC_LENGTH (args);
13104 90 : for (int i = 0; i < len; ++i)
13105 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
13106 : return false;
13107 : return true;
13108 : }
13109 :
13110 0 : default:
13111 0 : if (objc_non_constant_expr_p (t))
13112 : return false;
13113 :
13114 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13115 0 : gcc_unreachable ();
13116 : return false;
13117 : }
13118 : #undef RECUR
13119 4659313443 : }
13120 :
13121 : bool
13122 1701628245 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13123 : bool fundef_p, tsubst_flags_t flags)
13124 : {
13125 1701628245 : if (flags & tf_error)
13126 : {
13127 : /* Check potentiality quietly first, as that could be performed more
13128 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13129 : that fails, replay the check noisily to give errors. */
13130 11081418 : flags &= ~tf_error;
13131 11081418 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13132 : flags))
13133 : return true;
13134 1069 : flags |= tf_error;
13135 : }
13136 :
13137 1690547896 : tree target = NULL_TREE;
13138 1690547896 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13139 1690547896 : flags, &target);
13140 : }
13141 :
13142 : /* The main entry point to the above. */
13143 :
13144 : bool
13145 447273691 : potential_constant_expression (tree t)
13146 : {
13147 447273691 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13148 : /*now*/false, /*fundef_p*/false,
13149 447273691 : tf_none);
13150 : }
13151 :
13152 : /* As above, but require a constant rvalue. */
13153 :
13154 : bool
13155 35467198 : potential_rvalue_constant_expression (tree t)
13156 : {
13157 35467198 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13158 : /*now*/false, /*fundef_p*/false,
13159 35467198 : tf_none);
13160 : }
13161 :
13162 : /* Like above, but complain about non-constant expressions. */
13163 :
13164 : bool
13165 88 : require_potential_constant_expression (tree t)
13166 : {
13167 88 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13168 : /*now*/false, /*fundef_p*/false,
13169 88 : tf_warning_or_error);
13170 : }
13171 :
13172 : /* Cross product of the above. */
13173 :
13174 : bool
13175 132406 : require_potential_rvalue_constant_expression (tree t)
13176 : {
13177 132406 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13178 : /*now*/false, /*fundef_p*/false,
13179 132406 : tf_warning_or_error);
13180 : }
13181 :
13182 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
13183 :
13184 : bool
13185 286 : require_potential_rvalue_constant_expression_fncheck (tree t)
13186 : {
13187 286 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13188 : /*now*/false, /*fundef_p*/true,
13189 286 : tf_warning_or_error);
13190 : }
13191 :
13192 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
13193 :
13194 : bool
13195 1135 : require_rvalue_constant_expression (tree t)
13196 : {
13197 1135 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13198 : /*now*/true, /*fundef_p*/false,
13199 1135 : tf_warning_or_error);
13200 : }
13201 :
13202 : /* Like potential_constant_expression, but don't consider possible constexpr
13203 : substitution of the current function. That is, PARM_DECL qualifies under
13204 : potential_constant_expression, but not here.
13205 :
13206 : This is basically what you can check when any actual constant values might
13207 : be value-dependent. */
13208 :
13209 : bool
13210 920722056 : is_constant_expression (tree t)
13211 : {
13212 920722056 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13213 : /*now*/true, /*fundef_p*/false,
13214 920722056 : tf_none);
13215 : }
13216 :
13217 : /* As above, but expect an rvalue. */
13218 :
13219 : bool
13220 155929970 : is_rvalue_constant_expression (tree t)
13221 : {
13222 155929970 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13223 : /*now*/true, /*fundef_p*/false,
13224 155929970 : tf_none);
13225 : }
13226 :
13227 : /* Like above, but complain about non-constant expressions. */
13228 :
13229 : bool
13230 10947503 : require_constant_expression (tree t)
13231 : {
13232 10947503 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13233 : /*now*/true, /*fundef_p*/false,
13234 10947503 : tf_warning_or_error);
13235 : }
13236 :
13237 : /* Like is_constant_expression, but allow const variables that are not allowed
13238 : under constexpr rules. */
13239 :
13240 : bool
13241 120072494 : is_static_init_expression (tree t)
13242 : {
13243 120072494 : return potential_constant_expression_1 (t, /*want_rval*/false,
13244 : /*strict*/false, /*now*/true,
13245 120072494 : /*fundef_p*/false, tf_none);
13246 : }
13247 :
13248 : /* Returns true if T is a potential constant expression that is not
13249 : instantiation-dependent, and therefore a candidate for constant folding even
13250 : in a template. */
13251 :
13252 : bool
13253 888920342 : is_nondependent_constant_expression (tree t)
13254 : {
13255 888920342 : return (!type_unknown_p (t)
13256 888920293 : && is_constant_expression (t)
13257 1667710141 : && !instantiation_dependent_expression_p (t));
13258 : }
13259 :
13260 : /* Returns true if T is a potential static initializer expression that is not
13261 : instantiation-dependent. */
13262 :
13263 : bool
13264 120072494 : is_nondependent_static_init_expression (tree t)
13265 : {
13266 120072494 : return (!type_unknown_p (t)
13267 120072494 : && is_static_init_expression (t)
13268 231811829 : && !instantiation_dependent_expression_p (t));
13269 : }
13270 :
13271 : /* True iff FN is an implicitly constexpr function. */
13272 :
13273 : bool
13274 459485 : decl_implicit_constexpr_p (tree fn)
13275 : {
13276 459485 : if (!(flag_implicit_constexpr
13277 3 : && TREE_CODE (fn) == FUNCTION_DECL
13278 3 : && DECL_DECLARED_CONSTEXPR_P (fn)))
13279 : return false;
13280 :
13281 3 : if (DECL_CLONED_FUNCTION_P (fn))
13282 0 : fn = DECL_CLONED_FUNCTION (fn);
13283 :
13284 3 : return (DECL_LANG_SPECIFIC (fn)
13285 3 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
13286 : }
13287 :
13288 : /* Finalize constexpr processing after parsing. */
13289 :
13290 : void
13291 98561 : fini_constexpr (void)
13292 : {
13293 : /* The contexpr call and fundef copies tables are no longer needed. */
13294 98561 : constexpr_call_table = NULL;
13295 98561 : fundef_copies_table = NULL;
13296 98561 : }
13297 :
13298 : #include "gt-cp-constexpr.h"
|