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 237114202 : is_instantiation_of_constexpr (tree fun)
62 : {
63 314730412 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 161009265 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 344873220 : || (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 204120793 : literal_type_p (tree t)
73 : {
74 200600276 : if (SCALAR_TYPE_P (t)
75 99140840 : || VECTOR_TYPE_P (t)
76 99128701 : || TYPE_REF_P (t)
77 286632953 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : return true;
79 77099145 : if (CLASS_TYPE_P (t))
80 : {
81 75591131 : t = complete_type (t);
82 75591131 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 75591131 : return CLASSTYPE_LITERAL_P (t);
84 : }
85 1508014 : if (TREE_CODE (t) == ARRAY_TYPE)
86 1507827 : 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 330459934 : ensure_literal_type_for_constexpr_object (tree decl)
96 : {
97 330459934 : tree type = TREE_TYPE (decl);
98 330459934 : if (VAR_P (decl)
99 131577209 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 91179258 : || var_in_constexpr_fn (decl))
101 390353276 : && !processing_template_decl)
102 : {
103 41707351 : tree stype = strip_array_types (type);
104 41707351 : 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 41707345 : else if (!literal_type_p (type))
108 : {
109 24003 : 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 23950 : else if (cxx_dialect < cxx23)
119 : {
120 18228 : 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 18228 : cp_function_chain->invalid_constexpr = true;
131 : }
132 : }
133 41683342 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 41683342 : && 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 330459934 : 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 960 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : const char *gmsgid, ...)
156 : {
157 960 : diagnostics::diagnostic_info diagnostic;
158 960 : va_list ap;
159 960 : rich_location richloc (line_table, location);
160 960 : va_start (ap, gmsgid);
161 960 : bool ret;
162 960 : if (!constexpr_fundef_p)
163 : {
164 : /* Report an error that cannot be suppressed. */
165 700 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
166 : diagnostics::kind::error);
167 700 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
168 : }
169 260 : else if (warn_invalid_constexpr)
170 : {
171 148 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
172 148 : (cxx_dialect < cxx23
173 : ? diagnostics::kind::pedwarn
174 : : diagnostics::kind::warning));
175 148 : diagnostic.m_option_id = OPT_Winvalid_constexpr;
176 148 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
177 : }
178 : else
179 : ret = false;
180 960 : va_end (ap);
181 1920 : return ret;
182 960 : }
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 882988092 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : const constexpr_fundef *rhs)
202 : {
203 856443058 : 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 860019002 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : {
212 860019002 : return DECL_UID (fundef->decl);
213 : }
214 :
215 : /* Return a previously saved definition of function FUN. */
216 :
217 : constexpr_fundef *
218 97376162 : retrieve_constexpr_fundef (tree fun)
219 : {
220 97376162 : if (constexpr_fundef_table == NULL)
221 : return NULL;
222 :
223 97372344 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 97372344 : 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 34466063 : is_valid_constexpr_fn (tree fun, bool complain)
232 : {
233 34466063 : bool ret = true;
234 :
235 68932126 : if (DECL_INHERITED_CTOR (fun)
236 4135593 : && 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 34466063 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 68634505 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 34168442 : if (!literal_type_p (TREE_TYPE (parm)))
248 : {
249 13636 : ret = false;
250 13636 : if (complain)
251 : {
252 22 : auto_diagnostic_group d;
253 22 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
254 : "invalid type for parameter %d of "
255 : "%<constexpr%> function %q+#D",
256 22 : DECL_PARM_INDEX (parm), fun))
257 14 : explain_non_literal_class (TREE_TYPE (parm));
258 22 : }
259 : }
260 : }
261 :
262 58154649 : 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 68932120 : 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 34466060 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : {
279 29606920 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 29606920 : if (!literal_type_p (rettype))
281 : {
282 50665 : ret = false;
283 50665 : if (complain)
284 : {
285 13 : auto_diagnostic_group d;
286 13 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
287 : "invalid return type %qT of %<constexpr%> "
288 : "function %q+D", rettype, fun))
289 9 : explain_non_literal_class (rettype);
290 13 : }
291 : }
292 :
293 : /* C++14 DR 1684 removed this restriction. */
294 29606920 : if (cxx_dialect < cxx14
295 31479 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 29608009 : && !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 4859140 : 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 34466063 : 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 894 : 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 894 : auto_vec<tree, 2> fields;
344 1884 : do
345 : {
346 1884 : fields.safe_push (TREE_OPERAND (member, 1));
347 1884 : member = TREE_OPERAND (member, 0);
348 : }
349 1884 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 3774 : && 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 1884 : while (field = fields.pop(),
357 1884 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : {
359 990 : tree ctor;
360 : /* If there is already an outer constructor entry for the anonymous
361 : aggregate FIELD, use it; otherwise, insert one. */
362 990 : if (vec_safe_is_empty (*vec)
363 198 : || (*vec)->last().index != field)
364 : {
365 888 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 888 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : }
368 : else
369 102 : ctor = (*vec)->last().value;
370 990 : 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 894 : gcc_assert (fields.is_empty());
376 894 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 :
378 894 : return true;
379 894 : }
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 6797648 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : {
390 7452984 : tree member, init;
391 7452984 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 3526113 : t = TREE_OPERAND (t, 0);
393 7452984 : if (TREE_CODE (t) == EXPR_STMT)
394 3525259 : t = TREE_OPERAND (t, 0);
395 7452984 : if (t == error_mark_node)
396 : return false;
397 7452975 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : {
399 8314885 : for (tree stmt : tsi_range (t))
400 865663 : if (! build_data_member_initialization (stmt, vec))
401 9 : return false;
402 : return true;
403 : }
404 6801392 : 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 617818 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : }
413 6183574 : if (TREE_CODE (t) == CONVERT_EXPR)
414 3484062 : t = TREE_OPERAND (t, 0);
415 6183574 : if (TREE_CODE (t) == INIT_EXPR)
416 : {
417 3348400 : member = TREE_OPERAND (t, 0);
418 3348400 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : }
420 2835174 : else if (TREE_CODE (t) == CALL_EXPR)
421 : {
422 2343909 : tree fn = get_callee_fndecl (t);
423 4687818 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : /* We're only interested in calls to subobject constructors. */
425 : return true;
426 1938220 : 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 1938220 : init = break_out_target_exprs (t);
431 : }
432 491265 : else if (TREE_CODE (t) == BIND_EXPR)
433 37518 : 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 5286620 : if (INDIRECT_REF_P (member))
438 58281 : member = TREE_OPERAND (member, 0);
439 5286620 : if (TREE_CODE (member) == NOP_EXPR)
440 : {
441 522730 : tree op = member;
442 522730 : STRIP_NOPS (op);
443 522730 : if (TREE_CODE (op) == ADDR_EXPR)
444 : {
445 590 : 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 522140 : else if (op == current_class_ptr
453 1044149 : && (same_type_ignoring_top_level_qualifiers_p
454 522009 : (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 451277 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : }
464 : }
465 5286620 : if (TREE_CODE (member) == ADDR_EXPR)
466 1474361 : member = TREE_OPERAND (member, 0);
467 5286620 : if (TREE_CODE (member) == COMPONENT_REF)
468 : {
469 4733002 : tree aggr = TREE_OPERAND (member, 0);
470 4733002 : if (TREE_CODE (aggr) == VAR_DECL)
471 : /* Initializing a local variable, don't add anything. */
472 : return true;
473 4733000 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : /* Normal member initialization. */
475 4732103 : member = TREE_OPERAND (member, 1);
476 897 : else if (VAR_P (get_base_address (aggr)))
477 : /* Initializing a local variable, don't add anything. */
478 : return true;
479 894 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
480 : /* Initializing a member of an anonymous union. */
481 894 : 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 6701207 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
491 50 : (*vec)->last().value = init;
492 : else
493 5285671 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
494 : return true;
495 : }
496 :
497 : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
498 : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
499 : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
500 :
501 : static bool
502 2394 : check_constexpr_bind_expr_vars (tree t)
503 : {
504 2394 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
505 :
506 3216 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
507 834 : if (TREE_CODE (var) == TYPE_DECL
508 815 : && DECL_IMPLICIT_TYPEDEF_P (var)
509 856 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
510 : return false;
511 : return true;
512 : }
513 :
514 : /* Subroutine of check_constexpr_ctor_body. */
515 :
516 : static bool
517 4133 : check_constexpr_ctor_body_1 (tree last, tree list)
518 : {
519 4133 : switch (TREE_CODE (list))
520 : {
521 6 : case DECL_EXPR:
522 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
523 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
524 : return true;
525 : return false;
526 :
527 4 : case CLEANUP_POINT_EXPR:
528 4 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
529 4 : /*complain=*/false);
530 :
531 2057 : case BIND_EXPR:
532 2057 : if (!check_constexpr_bind_expr_vars (list)
533 2057 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
534 : /*complain=*/false))
535 42 : return false;
536 : return true;
537 :
538 : case USING_STMT:
539 : case STATIC_ASSERT:
540 : case DEBUG_BEGIN_STMT:
541 : return true;
542 :
543 : default:
544 : return false;
545 : }
546 : }
547 :
548 : /* Make sure that there are no statements after LAST in the constructor
549 : body represented by LIST. */
550 :
551 : bool
552 5409753 : 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 5409753 : if (cxx_dialect >= cxx14)
556 : return true;
557 :
558 13619 : bool ok = true;
559 13619 : if (TREE_CODE (list) == STATEMENT_LIST)
560 : {
561 13593 : tree_stmt_iterator i = tsi_last (list);
562 17631 : for (; !tsi_end_p (i); tsi_prev (&i))
563 : {
564 15508 : tree t = tsi_stmt (i);
565 15508 : if (t == last)
566 : break;
567 4107 : if (!check_constexpr_ctor_body_1 (last, t))
568 : {
569 : ok = false;
570 : break;
571 : }
572 : }
573 : }
574 26 : else if (list != last
575 26 : && !check_constexpr_ctor_body_1 (last, list))
576 : ok = false;
577 13619 : if (!ok && complain)
578 : {
579 48 : pedwarn (input_location, OPT_Wc__14_extensions,
580 : "%<constexpr%> constructor does not have empty body");
581 48 : ok = true;
582 : }
583 : return ok;
584 : }
585 :
586 : /* V is a vector of constructor elements built up for the base and member
587 : initializers of a constructor for TYPE. They need to be in increasing
588 : offset order, which they might not be yet if TYPE has a primary base
589 : which is not first in the base-clause or a vptr and at least one base
590 : all of which are non-primary. */
591 :
592 : static vec<constructor_elt, va_gc> *
593 4063854 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
594 : {
595 4063854 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
596 4063854 : tree field_type;
597 4063854 : unsigned i;
598 4063854 : constructor_elt *ce;
599 :
600 4063854 : if (pri)
601 74134 : field_type = BINFO_TYPE (pri);
602 3989720 : else if (TYPE_CONTAINS_VPTR_P (type))
603 51967 : 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 182807 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
610 130720 : if (TREE_TYPE (ce->index) == field_type)
611 : break;
612 :
613 164329 : if (i > 0 && i < vec_safe_length (v))
614 : {
615 22 : vec<constructor_elt, va_gc> &vref = *v;
616 22 : constructor_elt elt = vref[i];
617 44 : for (; i > 0; --i)
618 22 : vref[i] = vref[i-1];
619 22 : vref[0] = elt;
620 : }
621 :
622 : return v;
623 : }
624 :
625 : /* Build compile-time evalable representations of member-initializer list
626 : for a constexpr constructor. */
627 :
628 : static tree
629 4134704 : build_constexpr_constructor_member_initializers (tree type, tree body)
630 : {
631 4134704 : vec<constructor_elt, va_gc> *vec = NULL;
632 4134704 : bool ok = true;
633 6217734 : while (true)
634 6217734 : switch (TREE_CODE (body))
635 : {
636 2082964 : case MUST_NOT_THROW_EXPR:
637 2082964 : case EH_SPEC_BLOCK:
638 2082964 : body = TREE_OPERAND (body, 0);
639 2082964 : break;
640 :
641 66 : case STATEMENT_LIST:
642 6217819 : for (tree stmt : tsi_range (body))
643 : {
644 135 : body = stmt;
645 135 : if (TREE_CODE (body) == BIND_EXPR)
646 : break;
647 : }
648 : break;
649 :
650 4134704 : case BIND_EXPR:
651 4134704 : body = BIND_EXPR_BODY (body);
652 4134704 : goto found;
653 :
654 0 : default:
655 0 : gcc_unreachable ();
656 : }
657 4134704 : found:
658 4134704 : if (TREE_CODE (body) == TRY_BLOCK)
659 : {
660 29 : body = TREE_OPERAND (body, 0);
661 29 : if (TREE_CODE (body) == BIND_EXPR)
662 29 : body = BIND_EXPR_BODY (body);
663 : }
664 4134704 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
665 : {
666 2302807 : body = TREE_OPERAND (body, 0);
667 2302807 : if (TREE_CODE (body) == EXPR_STMT)
668 2302803 : body = TREE_OPERAND (body, 0);
669 2302807 : if (TREE_CODE (body) == INIT_EXPR
670 2302807 : && (same_type_ignoring_top_level_qualifiers_p
671 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
672 : current_class_type)))
673 : {
674 : /* Trivial copy. */
675 0 : return TREE_OPERAND (body, 1);
676 : }
677 2302807 : ok = build_data_member_initialization (body, &vec);
678 : }
679 1831897 : else if (TREE_CODE (body) == STATEMENT_LIST)
680 : {
681 5454822 : for (tree stmt : tsi_range (body))
682 : {
683 3626052 : ok = build_data_member_initialization (stmt, &vec);
684 3626052 : if (!ok)
685 : break;
686 : }
687 : }
688 3126 : else if (EXPR_P (body))
689 3126 : ok = build_data_member_initialization (body, &vec);
690 : else
691 0 : gcc_assert (errorcount > 0);
692 4134704 : if (ok)
693 : {
694 4134695 : if (vec_safe_length (vec) > 0)
695 : {
696 : /* In a delegating constructor, return the target. */
697 3871003 : constructor_elt *ce = &(*vec)[0];
698 3871003 : if (ce->index == current_class_ptr)
699 : {
700 70841 : body = ce->value;
701 70841 : vec_free (vec);
702 70841 : return body;
703 : }
704 : }
705 4063854 : vec = sort_constexpr_mem_initializers (type, vec);
706 4063854 : return build_constructor (type, vec);
707 : }
708 : else
709 9 : return error_mark_node;
710 : }
711 :
712 : /* We have an expression tree T that represents a call, either CALL_EXPR
713 : or AGGR_INIT_EXPR. If the call is lexically to a named function,
714 : return the _DECL for that function. */
715 :
716 : static tree
717 603834868 : get_function_named_in_call (tree t)
718 : {
719 603834868 : tree callee = cp_get_callee (t);
720 603834868 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
721 603834868 : return fun ? fun : callee;
722 : }
723 :
724 : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
725 : declared to be constexpr, or a sub-statement thereof. Returns the
726 : return value if suitable, error_mark_node for a statement not allowed in
727 : a constexpr function, or NULL_TREE if no return value was found. */
728 :
729 : tree
730 68586 : constexpr_fn_retval (tree body)
731 : {
732 75112 : switch (TREE_CODE (body))
733 : {
734 18518 : case STATEMENT_LIST:
735 18518 : {
736 18518 : tree expr = NULL_TREE;
737 55484 : for (tree stmt : tsi_range (body))
738 : {
739 36995 : tree s = constexpr_fn_retval (stmt);
740 36995 : if (s == error_mark_node)
741 : return error_mark_node;
742 36968 : else if (s == NULL_TREE)
743 : /* Keep iterating. */;
744 18483 : else if (expr)
745 : /* Multiple return statements. */
746 : return error_mark_node;
747 : else
748 : expr = s;
749 : }
750 : return expr;
751 : }
752 :
753 31541 : case RETURN_EXPR:
754 31541 : return break_out_target_exprs (TREE_OPERAND (body, 0));
755 :
756 20 : case DECL_EXPR:
757 20 : {
758 20 : tree decl = DECL_EXPR_DECL (body);
759 20 : if (TREE_CODE (decl) == USING_DECL
760 : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
761 20 : || DECL_ARTIFICIAL (decl))
762 : return NULL_TREE;
763 6 : return error_mark_node;
764 : }
765 :
766 6195 : case CLEANUP_POINT_EXPR:
767 6195 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
768 :
769 337 : case BIND_EXPR:
770 337 : if (!check_constexpr_bind_expr_vars (body))
771 6 : return error_mark_node;
772 331 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
773 :
774 : case USING_STMT:
775 : case DEBUG_BEGIN_STMT:
776 : return NULL_TREE;
777 :
778 0 : case CALL_EXPR:
779 0 : {
780 0 : tree fun = get_function_named_in_call (body);
781 0 : if (fun != NULL_TREE
782 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
783 : return NULL_TREE;
784 : }
785 : /* Fallthru. */
786 :
787 30 : default:
788 30 : return error_mark_node;
789 : }
790 : }
791 :
792 : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
793 : FUN; do the necessary transformations to turn it into a single expression
794 : that we can store in the hash table. */
795 :
796 : static tree
797 33058282 : massage_constexpr_body (tree fun, tree body)
798 : {
799 66116564 : if (DECL_CONSTRUCTOR_P (fun))
800 4134704 : body = build_constexpr_constructor_member_initializers
801 4134704 : (DECL_CONTEXT (fun), body);
802 28923578 : else if (cxx_dialect < cxx14)
803 : {
804 31356 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
805 1 : body = EH_SPEC_STMTS (body);
806 31356 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
807 20267 : body = TREE_OPERAND (body, 0);
808 31356 : body = constexpr_fn_retval (body);
809 : }
810 33058282 : return body;
811 : }
812 :
813 : /* CTYPE is a type constructed from BODY. Return true if some
814 : bases/fields are uninitialized, and complain if COMPLAIN. */
815 :
816 : static bool
817 3438357 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
818 : {
819 : /* We allow uninitialized bases/fields in C++20. */
820 3438357 : if (cxx_dialect >= cxx20)
821 : return false;
822 :
823 13322 : unsigned nelts = 0;
824 :
825 13322 : if (body)
826 : {
827 13318 : if (TREE_CODE (body) != CONSTRUCTOR)
828 : return false;
829 13268 : nelts = CONSTRUCTOR_NELTS (body);
830 : }
831 13272 : tree field = TYPE_FIELDS (ctype);
832 :
833 13272 : if (TREE_CODE (ctype) == UNION_TYPE)
834 : {
835 109 : if (nelts == 0 && next_aggregate_field (field))
836 : {
837 2 : if (complain)
838 2 : error ("%<constexpr%> constructor for union %qT must "
839 : "initialize exactly one non-static data member", ctype);
840 2 : return true;
841 : }
842 107 : return false;
843 : }
844 :
845 : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
846 : need an explicit initialization. */
847 : bool bad = false;
848 28004 : for (unsigned i = 0; i <= nelts; ++i)
849 : {
850 28004 : tree index = NULL_TREE;
851 28004 : if (i < nelts)
852 : {
853 14841 : index = CONSTRUCTOR_ELT (body, i)->index;
854 : /* Skip vptr adjustment represented with a COMPONENT_REF. */
855 14841 : if (TREE_CODE (index) != FIELD_DECL)
856 602 : continue;
857 : }
858 :
859 480645 : for (; field != index; field = DECL_CHAIN (field))
860 : {
861 453249 : tree ftype;
862 453249 : if (TREE_CODE (field) != FIELD_DECL)
863 904698 : continue;
864 1772 : if (DECL_UNNAMED_BIT_FIELD (field))
865 10 : continue;
866 : /* Artificial fields can be ignored unless they're bases. */
867 1762 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
868 6 : continue;
869 1756 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
870 : {
871 : /* Recurse to check the anonymous aggregate member. */
872 8 : bad |= cx_check_missing_mem_inits
873 4 : (TREE_TYPE (field), NULL_TREE, complain);
874 4 : if (bad && !complain)
875 6 : return true;
876 4 : continue;
877 : }
878 1752 : ftype = TREE_TYPE (field);
879 1752 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
880 : /* A flexible array can't be intialized here, so don't complain
881 : that it isn't. */
882 1 : continue;
883 1751 : if (is_empty_field (field))
884 : /* An empty field doesn't need an initializer. */
885 1716 : continue;
886 35 : ftype = strip_array_types (ftype);
887 35 : if (type_has_constexpr_default_constructor (ftype))
888 : {
889 : /* It's OK to skip a member with a trivial constexpr ctor.
890 : A constexpr ctor that isn't trivial should have been
891 : added in by now. */
892 7 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
893 : || errorcount != 0);
894 7 : continue;
895 : }
896 28 : if (!complain)
897 : return true;
898 22 : auto_diagnostic_group d;
899 22 : error ("member %qD must be initialized by mem-initializer "
900 : "in %<constexpr%> constructor", field);
901 22 : inform (DECL_SOURCE_LOCATION (field), "declared here");
902 22 : bad = true;
903 22 : }
904 27396 : if (field == NULL_TREE)
905 : break;
906 :
907 14239 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
908 : {
909 : /* Check the anonymous aggregate initializer is valid. */
910 76 : bad |= cx_check_missing_mem_inits
911 38 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
912 38 : if (bad && !complain)
913 : return true;
914 : }
915 14239 : field = DECL_CHAIN (field);
916 : }
917 :
918 : return bad;
919 : }
920 :
921 : /* We are processing the definition of the constexpr function FUN.
922 : Check that its body fulfills the apropriate requirements and
923 : enter it in the constexpr function definition table. */
924 :
925 : void
926 166164344 : maybe_save_constexpr_fundef (tree fun)
927 : {
928 166164344 : if (processing_template_decl
929 73947407 : || cp_function_chain->invalid_constexpr
930 240093598 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
931 133106325 : return;
932 :
933 : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
934 : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
935 56657828 : bool implicit = false;
936 56657828 : if (flag_implicit_constexpr)
937 : {
938 19 : if (DECL_DELETING_DESTRUCTOR_P (fun)
939 19 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
940 : /* Don't inherit implicit constexpr from the non-deleting
941 : destructor. */
942 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
943 :
944 19 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
945 16 : && DECL_DECLARED_INLINE_P (fun)
946 31 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
947 : implicit = true;
948 : }
949 :
950 56657828 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
951 : return;
952 :
953 33113445 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
954 :
955 33113445 : if (!is_valid_constexpr_fn (fun, complain))
956 : return;
957 :
958 33058219 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
959 33058219 : if (massaged == NULL_TREE || massaged == error_mark_node)
960 : {
961 72 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
962 22 : error ("body of %<constexpr%> function %qD not a return-statement",
963 : fun);
964 36 : return;
965 : }
966 :
967 33058183 : bool potential = potential_rvalue_constant_expression (massaged);
968 33058183 : if (!potential && complain)
969 224 : require_potential_rvalue_constant_expression_fncheck (massaged);
970 :
971 37192866 : if (DECL_CONSTRUCTOR_P (fun) && potential
972 37190780 : && !DECL_DEFAULTED_FN (fun))
973 : {
974 3438303 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
975 : massaged, complain))
976 : potential = false;
977 3438277 : else if (cxx_dialect > cxx11)
978 : {
979 : /* What we got from massage_constexpr_body is pretty much just the
980 : ctor-initializer, also check the body. */
981 3433567 : massaged = DECL_SAVED_TREE (fun);
982 3433567 : potential = potential_rvalue_constant_expression (massaged);
983 3433567 : if (!potential && complain)
984 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
985 : }
986 : }
987 :
988 33058183 : if (!potential && complain
989 : /* If -Wno-invalid-constexpr was specified, we haven't complained
990 : about non-constant expressions yet. Register the function and
991 : complain in explain_invalid_constexpr_fn if the function is
992 : called. */
993 260 : && warn_invalid_constexpr != 0)
994 : return;
995 :
996 33058029 : if (implicit)
997 : {
998 12 : if (potential)
999 : {
1000 2 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
1001 2 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
1002 4 : if (DECL_CONSTRUCTOR_P (fun))
1003 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
1004 : }
1005 : else
1006 : /* Don't bother keeping the pre-generic body of unsuitable functions
1007 : not explicitly declared constexpr. */
1008 : return;
1009 : }
1010 :
1011 33058019 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1012 33058019 : bool clear_ctx = false;
1013 33058019 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1014 : {
1015 33058019 : clear_ctx = true;
1016 33058019 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1017 : }
1018 33058019 : tree saved_fn = current_function_decl;
1019 33058019 : current_function_decl = fun;
1020 33058019 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1021 33058019 : current_function_decl = saved_fn;
1022 33058019 : if (clear_ctx)
1023 33058019 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1024 33058019 : if (!potential)
1025 : /* For a template instantiation, we want to remember the pre-generic body
1026 : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1027 : that it doesn't need to bother trying to expand the function. */
1028 108835 : entry.result = error_mark_node;
1029 :
1030 33058019 : register_constexpr_fundef (entry);
1031 : }
1032 :
1033 : /* BODY is a validated and massaged definition of a constexpr
1034 : function. Register it in the hash table. */
1035 :
1036 : void
1037 33088798 : register_constexpr_fundef (const constexpr_fundef &value)
1038 : {
1039 : /* Create the constexpr function table if necessary. */
1040 33088798 : if (constexpr_fundef_table == NULL)
1041 25868 : constexpr_fundef_table
1042 25868 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1043 :
1044 33088798 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1045 33088798 : (const_cast<constexpr_fundef *> (&value), INSERT);
1046 :
1047 33088798 : gcc_assert (*slot == NULL);
1048 33088798 : *slot = ggc_alloc<constexpr_fundef> ();
1049 33088798 : **slot = value;
1050 33088798 : }
1051 :
1052 : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1053 : function called in a context that requires a constant expression).
1054 : If it comes from a constexpr template, explain why the instantiation
1055 : isn't constexpr. Otherwise, explain why the function cannot be used
1056 : in a constexpr context. */
1057 :
1058 : void
1059 761 : explain_invalid_constexpr_fn (tree fun)
1060 : {
1061 761 : static hash_set<tree> *diagnosed;
1062 761 : tree body;
1063 :
1064 : /* Don't try to explain a function we already complained about. */
1065 761 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1066 572 : if (f->language->erroneous)
1067 761 : return;
1068 :
1069 : /* In C++23, a function marked 'constexpr' may not actually be a constant
1070 : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1071 : wasn't enabled. The function was called, so diagnose why it cannot be
1072 : used in a constant expression. */
1073 712 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1074 : /* Go on. */;
1075 : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1076 684 : else if (!DECL_DEFAULTED_FN (fun)
1077 913 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1078 655 : && !(flag_implicit_constexpr
1079 8 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1080 8 : && DECL_DECLARED_INLINE_P (fun))
1081 1334 : && !is_instantiation_of_constexpr (fun))
1082 : {
1083 632 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1084 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1085 635 : && decl_defined_p (fun))
1086 3 : inform (DECL_SOURCE_LOCATION (fun),
1087 : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1088 632 : return;
1089 : }
1090 80 : if (diagnosed == NULL)
1091 64 : diagnosed = new hash_set<tree>;
1092 80 : if (diagnosed->add (fun))
1093 : /* Already explained. */
1094 : return;
1095 :
1096 79 : iloc_sentinel ils = input_location;
1097 79 : if (!lambda_static_thunk_p (fun))
1098 : {
1099 : /* Diagnostics should completely ignore the static thunk, so leave
1100 : input_location set to our caller's location. */
1101 75 : input_location = DECL_SOURCE_LOCATION (fun);
1102 75 : inform (input_location,
1103 : "%qD is not usable as a %<constexpr%> function because:", fun);
1104 : }
1105 : /* First check the declaration. */
1106 79 : if (is_valid_constexpr_fn (fun, true))
1107 : {
1108 : /* Then if it's OK, the body. */
1109 73 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1110 73 : && DECL_DEFAULTED_FN (fun))
1111 10 : explain_implicit_non_constexpr (fun);
1112 : else
1113 : {
1114 63 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1115 46 : body = fd->body;
1116 : else
1117 17 : body = DECL_SAVED_TREE (fun);
1118 63 : tree massaged = massage_constexpr_body (fun, body);
1119 63 : require_potential_rvalue_constant_expression (massaged);
1120 126 : if (DECL_CONSTRUCTOR_P (fun))
1121 : {
1122 12 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1123 12 : if (cxx_dialect > cxx11)
1124 : /* Also check the body, not just the ctor-initializer. */
1125 10 : require_potential_rvalue_constant_expression (body);
1126 : }
1127 51 : else if (massaged == NULL_TREE || massaged == error_mark_node)
1128 1 : error ("body of %<constexpr%> function %qD not a return-statement",
1129 : fun);
1130 : }
1131 : }
1132 79 : }
1133 :
1134 : /* Objects of this type represent calls to constexpr functions
1135 : along with the bindings of parameters to their arguments, for
1136 : the purpose of compile time evaluation. */
1137 :
1138 : struct GTY((for_user)) constexpr_call {
1139 : /* Description of the constexpr function definition. */
1140 : constexpr_fundef *fundef = nullptr;
1141 : /* Parameter bindings environment. A TREE_VEC of arguments. */
1142 : tree bindings = NULL_TREE;
1143 : /* Result of the call, indexed by the value of
1144 : constexpr_ctx::manifestly_const_eval.
1145 : unknown_type_node means the call is being evaluated.
1146 : error_mark_node means that the evaluation was erroneous or otherwise
1147 : uncacheable (e.g. because it depends on the caller).
1148 : Otherwise, the actual value of the call. */
1149 : tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1150 : /* The hash of this call; we remember it here to avoid having to
1151 : recalculate it when expanding the hash table. */
1152 : hashval_t hash = 0;
1153 :
1154 : /* The result slot corresponding to the given mce_value. */
1155 63241291 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1156 : };
1157 :
1158 : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1159 : {
1160 : static hashval_t hash (constexpr_call *);
1161 : static bool equal (constexpr_call *, constexpr_call *);
1162 : };
1163 :
1164 : enum constexpr_switch_state {
1165 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1166 : and default: label for that switch has not been seen yet. */
1167 : css_default_not_seen,
1168 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1169 : and default: label for that switch has been seen already. */
1170 : css_default_seen,
1171 : /* Used when processing a switch for the second time by
1172 : cxx_eval_switch_expr, where default: label should match. */
1173 : css_default_processing
1174 : };
1175 :
1176 : /* The constexpr expansion context part which needs one instance per
1177 : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1178 : variables initialized within the expression. */
1179 :
1180 : class constexpr_global_ctx {
1181 : /* Values for any temporaries or local variables within the
1182 : constant-expression. Objects outside their lifetime have
1183 : value 'void_node'. */
1184 : hash_map<tree,tree> values;
1185 : public:
1186 : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1187 : on simple constants or location wrappers) encountered during current
1188 : cxx_eval_outermost_constant_expr call. */
1189 : HOST_WIDE_INT constexpr_ops_count;
1190 : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1191 : expression. */
1192 : auto_vec<tree, 16> heap_vars;
1193 : /* Vector of caught exceptions, including exceptions still not active at
1194 : the start of a handler (those are immediately followed up by HANDLER_TYPE
1195 : until __cxa_begin_catch finishes). */
1196 : auto_vec<tree, 2> caught_exceptions;
1197 : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1198 : vec<tree> *cleanups;
1199 : /* If non-null, only allow modification of existing values of the variables
1200 : in this set. Set by modifiable_tracker, below. */
1201 : hash_set<tree> *modifiable;
1202 : /* If cxx_eval_outermost_constant_expr is called on the consteval block
1203 : operator (), this is the FUNCTION_DECL of that operator (). */
1204 : tree consteval_block;
1205 : /* Number of heap VAR_DECL deallocations. */
1206 : unsigned heap_dealloc_count;
1207 : /* Number of uncaught exceptions. */
1208 : unsigned uncaught_exceptions;
1209 : /* A contract statement that failed or was not constant, we only store the
1210 : first one that fails. */
1211 : tree contract_statement;
1212 : /* [basic.contract.eval]/7.3 if this expression would otherwise be constant
1213 : then a non-const contract makes the program ill-formed. */
1214 : bool contract_condition_non_const;
1215 : /* Some metafunctions aren't dependent just on their arguments, but also
1216 : on various other dependencies, e.g. has_identifier on a function parameter
1217 : reflection can change depending on further declarations of corresponding
1218 : function, is_complete_type depends on type definitions and template
1219 : specializations in between the calls, define_aggregate even defines
1220 : class types, etc. Thus, we need to arrange for calls which call
1221 : at least some metafunctions to be non-cacheable, because their behavior
1222 : might not be the same. Until we figure out which exact metafunctions
1223 : need this and which don't, do it for all of them. */
1224 : bool metafns_called;
1225 :
1226 : /* Constructor. */
1227 615990857 : constexpr_global_ctx ()
1228 1231981714 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1229 615990857 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1230 615990857 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1231 615990857 : contract_condition_non_const (false), metafns_called (false) {}
1232 :
1233 414058675 : bool is_outside_lifetime (tree t)
1234 : {
1235 414058675 : if (tree *p = values.get (t))
1236 130691250 : if (*p == void_node)
1237 189 : return true;
1238 : return false;
1239 : }
1240 533670900 : tree get_value (tree t)
1241 : {
1242 533670900 : if (tree *p = values.get (t))
1243 225268241 : if (*p != void_node)
1244 221322787 : return *p;
1245 : return NULL_TREE;
1246 : }
1247 82458687 : tree *get_value_ptr (tree t, bool initializing)
1248 : {
1249 82458687 : if (modifiable && !modifiable->contains (t))
1250 : return nullptr;
1251 82458676 : if (tree *p = values.get (t))
1252 : {
1253 82453408 : if (*p != void_node)
1254 : return p;
1255 48 : else if (initializing)
1256 : {
1257 6 : *p = NULL_TREE;
1258 6 : return p;
1259 : }
1260 : }
1261 : return nullptr;
1262 : }
1263 278606059 : void put_value (tree t, tree v)
1264 : {
1265 278606059 : bool already_in_map = values.put (t, v);
1266 278606059 : if (!already_in_map && modifiable)
1267 30 : modifiable->add (t);
1268 278606059 : }
1269 199399203 : void destroy_value (tree t)
1270 : {
1271 199399203 : if (TREE_CODE (t) == VAR_DECL
1272 199399203 : || TREE_CODE (t) == PARM_DECL
1273 : || TREE_CODE (t) == RESULT_DECL)
1274 199394550 : values.put (t, void_node);
1275 : else
1276 4653 : values.remove (t);
1277 199399203 : }
1278 13969116 : void clear_value (tree t)
1279 : {
1280 27938232 : values.remove (t);
1281 : }
1282 : };
1283 :
1284 : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1285 : side-effects from evaluation of a particular subexpression of a
1286 : constant-expression. In such cases we use modifiable_tracker to prevent
1287 : modification of variables created outside of that subexpression.
1288 :
1289 : ??? We could change the hash_set to a hash_map, allow and track external
1290 : modifications, and roll them back in the destructor. It's not clear to me
1291 : that this would be worthwhile. */
1292 :
1293 : class modifiable_tracker
1294 : {
1295 : hash_set<tree> set;
1296 : constexpr_global_ctx *global;
1297 : public:
1298 173420 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1299 : {
1300 173420 : global->modifiable = &set;
1301 173420 : }
1302 173420 : ~modifiable_tracker ()
1303 : {
1304 173450 : for (tree t: set)
1305 30 : global->clear_value (t);
1306 173420 : global->modifiable = nullptr;
1307 173420 : }
1308 : };
1309 :
1310 : /* The constexpr expansion context. CALL is the current function
1311 : expansion, CTOR is the current aggregate initializer, OBJECT is the
1312 : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1313 :
1314 : struct constexpr_ctx {
1315 : /* The part of the context that needs to be unique to the whole
1316 : cxx_eval_outermost_constant_expr invocation. */
1317 : constexpr_global_ctx *global;
1318 : /* The innermost call we're evaluating. */
1319 : constexpr_call *call;
1320 : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1321 : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1322 : vec<tree> *save_exprs;
1323 : /* The CONSTRUCTOR we're currently building up for an aggregate
1324 : initializer. */
1325 : tree ctor;
1326 : /* The object we're building the CONSTRUCTOR for. */
1327 : tree object;
1328 : /* If inside SWITCH_EXPR. */
1329 : constexpr_switch_state *css_state;
1330 : /* The aggregate initialization context inside which this one is nested. This
1331 : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1332 : const constexpr_ctx *parent;
1333 :
1334 : /* Whether we should error on a non-constant expression or fail quietly.
1335 : This flag needs to be here, but some of the others could move to global
1336 : if they get larger than a word. */
1337 : bool quiet;
1338 : /* Whether we are strictly conforming to constant expression rules or
1339 : trying harder to get a constant value. */
1340 : bool strict;
1341 : /* Whether __builtin_is_constant_evaluated () should be true. */
1342 : mce_value manifestly_const_eval;
1343 : };
1344 :
1345 : /* Return ctx->quiet. For use in reflect.cc. */
1346 :
1347 : bool
1348 258 : cxx_constexpr_quiet_p (const constexpr_ctx *ctx)
1349 : {
1350 258 : return ctx->quiet;
1351 : }
1352 :
1353 : /* Return ctx->manifestly_const_eval. For use in reflect.cc. */
1354 :
1355 : mce_value
1356 108 : cxx_constexpr_manifestly_const_eval (const constexpr_ctx *ctx)
1357 : {
1358 108 : return ctx->manifestly_const_eval;
1359 : }
1360 :
1361 : /* Return ctx->call->fundef->decl or NULL_TREE. For use in
1362 : reflect.cc. */
1363 :
1364 : tree
1365 714 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1366 : {
1367 714 : if (ctx->call)
1368 375 : return ctx->call->fundef->decl;
1369 : else
1370 : return NULL_TREE;
1371 : }
1372 :
1373 : /* Return ctx->global->consteval_block. For use in reflect.cc. */
1374 :
1375 : tree
1376 72 : cxx_constexpr_consteval_block (const constexpr_ctx *ctx)
1377 : {
1378 72 : return ctx->global->consteval_block;
1379 : }
1380 :
1381 : /* Predicates for the meaning of *jump_target. */
1382 :
1383 : static bool
1384 87281569 : returns (tree *jump_target)
1385 : {
1386 14363236 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1387 : }
1388 :
1389 : static bool
1390 81777217 : breaks (tree *jump_target)
1391 : {
1392 81777217 : return (*jump_target
1393 81777217 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1394 91 : && LABEL_DECL_BREAK (*jump_target))
1395 977159 : || TREE_CODE (*jump_target) == BREAK_STMT
1396 936267 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1397 : }
1398 :
1399 : static bool
1400 121630675 : continues (tree *jump_target)
1401 : {
1402 121630675 : return (*jump_target
1403 121630675 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1404 163 : && LABEL_DECL_CONTINUE (*jump_target))
1405 954777 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1406 : }
1407 :
1408 : static bool
1409 9731411 : switches (tree *jump_target)
1410 : {
1411 48775 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1412 : }
1413 :
1414 : static bool
1415 1926202664 : throws (tree *jump_target)
1416 : {
1417 : /* void_node is for use in potential_constant_expression_1, otherwise
1418 : it should an artificial VAR_DECL created by constant evaluation
1419 : of __cxa_allocate_exception (). */
1420 153771027 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1421 : }
1422 :
1423 : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1424 : references and objects are in effect. */
1425 :
1426 : static bool
1427 283481227 : p2280_active_p (const constexpr_ctx *ctx)
1428 : {
1429 47602230 : if (ctx->manifestly_const_eval != mce_true)
1430 : /* Disable these relaxations during speculative constexpr folding,
1431 : as it can significantly increase compile time/memory use
1432 : (PR119387). */
1433 : return false;
1434 :
1435 : /* P2280R4 was accepted as a DR against C++11. */
1436 0 : return cxx_dialect >= cxx11;
1437 : }
1438 :
1439 : /* Remove T from the global values map, checking for attempts to destroy
1440 : a value that has already finished its lifetime. */
1441 :
1442 : static void
1443 199224807 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1444 : {
1445 199224807 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1446 : return;
1447 :
1448 : /* Don't error again here if we've already reported a problem. */
1449 199224790 : if (!*non_constant_p
1450 131498878 : && DECL_P (t)
1451 : /* Non-trivial destructors have their lifetimes ended explicitly
1452 : with a clobber, so don't worry about it here. */
1453 131494405 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1454 : /* ...except parameters are remapped in cxx_eval_call_expression,
1455 : and the destructor call during cleanup won't be able to tell that
1456 : this value has already been destroyed, so complain now. This is
1457 : not quite unobservable, but is extremely unlikely to crop up in
1458 : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1459 753861 : || TREE_CODE (t) == PARM_DECL)
1460 329965970 : && ctx->global->is_outside_lifetime (t))
1461 : {
1462 54 : if (!ctx->quiet)
1463 : {
1464 18 : auto_diagnostic_group d;
1465 18 : error ("destroying %qE outside its lifetime", t);
1466 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1467 18 : }
1468 54 : *non_constant_p = true;
1469 : }
1470 199224790 : ctx->global->destroy_value (t);
1471 : }
1472 :
1473 : /* This internal flag controls whether we should avoid doing anything during
1474 : constexpr evaluation that would cause extra DECL_UID generation, such as
1475 : template instantiation and function body copying. */
1476 :
1477 : static bool uid_sensitive_constexpr_evaluation_value;
1478 :
1479 : /* An internal counter that keeps track of the number of times
1480 : uid_sensitive_constexpr_evaluation_p returned true. */
1481 :
1482 : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1483 :
1484 : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1485 : increments the corresponding counter. */
1486 :
1487 : static bool
1488 10604652 : uid_sensitive_constexpr_evaluation_p ()
1489 : {
1490 10566382 : if (uid_sensitive_constexpr_evaluation_value)
1491 : {
1492 347308 : ++uid_sensitive_constexpr_evaluation_true_counter;
1493 347308 : return true;
1494 : }
1495 : else
1496 : return false;
1497 : }
1498 :
1499 : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1500 : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1501 : during the lifetime of the sentinel object. Upon its destruction, the
1502 : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1503 :
1504 84717484 : uid_sensitive_constexpr_evaluation_sentinel
1505 84717484 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1506 84717484 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1507 : {
1508 84717484 : }
1509 :
1510 : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1511 : records the current number of times that uid_sensitive_constexpr_evaluation_p
1512 : has been called and returned true. */
1513 :
1514 4568191900 : uid_sensitive_constexpr_evaluation_checker
1515 4568191900 : ::uid_sensitive_constexpr_evaluation_checker ()
1516 4568191900 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1517 : {
1518 4568191900 : }
1519 :
1520 : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1521 : some constexpr evaluation was restricted due to u_s_c_e_p being called
1522 : and returning true during the lifetime of this checker object. */
1523 :
1524 : bool
1525 3003848476 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1526 : {
1527 3003848476 : return (uid_sensitive_constexpr_evaluation_value
1528 3003848476 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1529 : }
1530 :
1531 :
1532 : /* A table of all constexpr calls that have been evaluated by the
1533 : compiler in this translation unit. */
1534 :
1535 : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1536 :
1537 : /* Compute a hash value for a constexpr call representation. */
1538 :
1539 : inline hashval_t
1540 204739609 : constexpr_call_hasher::hash (constexpr_call *info)
1541 : {
1542 204739609 : return info->hash;
1543 : }
1544 :
1545 : /* Return true if the objects pointed to by P and Q represent calls
1546 : to the same constexpr function with the same arguments.
1547 : Otherwise, return false. */
1548 :
1549 : bool
1550 211956141 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1551 : {
1552 211956141 : if (lhs == rhs)
1553 : return true;
1554 211956141 : if (lhs->hash != rhs->hash)
1555 : return false;
1556 26545034 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1557 : return false;
1558 26545034 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1559 : }
1560 :
1561 : /* Initialize the constexpr call table, if needed. */
1562 :
1563 : static void
1564 32625105 : maybe_initialize_constexpr_call_table (void)
1565 : {
1566 32625105 : if (constexpr_call_table == NULL)
1567 17084 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1568 32625105 : }
1569 :
1570 : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1571 : a function happens to get called recursively, we unshare the callee
1572 : function's body and evaluate this unshared copy instead of evaluating the
1573 : original body.
1574 :
1575 : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1576 : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1577 : that's keyed off of the original FUNCTION_DECL and whose value is a
1578 : TREE_LIST of this function's unused copies awaiting reuse.
1579 :
1580 : This is not GC-deletable to avoid GC affecting UID generation. */
1581 :
1582 : static GTY(()) decl_tree_map *fundef_copies_table;
1583 :
1584 : /* Reuse a copy or create a new unshared copy of the function FUN.
1585 : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1586 : is parms, TYPE is result. */
1587 :
1588 : static tree
1589 76699559 : get_fundef_copy (constexpr_fundef *fundef)
1590 : {
1591 76699559 : tree copy;
1592 76699559 : bool existed;
1593 76699559 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1594 76699559 : (fundef_copies_table, fundef->decl, &existed, 127));
1595 :
1596 76699559 : if (!existed)
1597 : {
1598 : /* There is no cached function available, or in use. We can use
1599 : the function directly. That the slot is now created records
1600 : that this function is now in use. */
1601 8841001 : copy = build_tree_list (fundef->body, fundef->parms);
1602 8841001 : TREE_TYPE (copy) = fundef->result;
1603 : }
1604 67858558 : else if (*slot == NULL_TREE)
1605 : {
1606 4345 : if (uid_sensitive_constexpr_evaluation_p ())
1607 0 : return NULL_TREE;
1608 :
1609 : /* We've already used the function itself, so make a copy. */
1610 4345 : copy = build_tree_list (NULL, NULL);
1611 4345 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1612 4345 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1613 4345 : tree saved_result = DECL_RESULT (fundef->decl);
1614 4345 : tree saved_fn = current_function_decl;
1615 4345 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1616 4345 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1617 4345 : DECL_RESULT (fundef->decl) = fundef->result;
1618 4345 : current_function_decl = fundef->decl;
1619 4345 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1620 4345 : TREE_TYPE (copy));
1621 4345 : current_function_decl = saved_fn;
1622 4345 : DECL_RESULT (fundef->decl) = saved_result;
1623 4345 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1624 4345 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1625 : }
1626 : else
1627 : {
1628 : /* We have a cached function available. */
1629 67854213 : copy = *slot;
1630 67854213 : *slot = TREE_CHAIN (copy);
1631 : }
1632 :
1633 : return copy;
1634 : }
1635 :
1636 : /* Save the copy COPY of function FUN for later reuse by
1637 : get_fundef_copy(). By construction, there will always be an entry
1638 : to find. */
1639 :
1640 : static void
1641 76699557 : save_fundef_copy (tree fun, tree copy)
1642 : {
1643 76699557 : tree *slot = fundef_copies_table->get (fun);
1644 76699557 : TREE_CHAIN (copy) = *slot;
1645 76699557 : *slot = copy;
1646 76699557 : }
1647 :
1648 : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1649 : value_cat, bool *, bool *, tree *);
1650 : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1651 : bool *, tree *);
1652 : static tree find_heap_var_refs (tree *, int *, void *);
1653 :
1654 : /* For exception object EXC if it has class type and usable what () method
1655 : which returns cv char * return the xmalloced string literal which it returns
1656 : if possible, otherwise return NULL. */
1657 :
1658 : static char *
1659 198 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1660 : {
1661 198 : tree type = strip_array_types (TREE_TYPE (exc));
1662 198 : if (!CLASS_TYPE_P (type))
1663 : return NULL;
1664 157 : tree std_exception = lookup_qualified_name (std_node, "exception",
1665 : LOOK_want::NORMAL, false);
1666 157 : if (TREE_CODE (std_exception) != TYPE_DECL)
1667 : return NULL;
1668 154 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1669 : return NULL;
1670 154 : base_kind b_kind;
1671 154 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1672 : tf_none);
1673 154 : if (binfo == NULL_TREE || binfo == error_mark_node)
1674 : return NULL;
1675 150 : if (type != TREE_TYPE (exc))
1676 112 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1677 150 : tree call
1678 150 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1679 : tf_none);
1680 150 : if (call == error_mark_node)
1681 : return NULL;
1682 150 : releasing_vec what_args;
1683 150 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1684 150 : if (call == error_mark_node)
1685 : return NULL;
1686 150 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1687 150 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1688 150 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1689 150 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1690 150 : TYPE_SIZE_UNIT (char_type_node))
1691 300 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1692 0 : return NULL;
1693 150 : if (!potential_constant_expression (call))
1694 : return NULL;
1695 150 : bool non_constant_p = false, overflow_p = false;
1696 150 : tree jmp_target = NULL;
1697 150 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1698 : &non_constant_p, &overflow_p,
1699 : &jmp_target);
1700 150 : if (throws (&jmp_target) || non_constant_p)
1701 : return NULL;
1702 150 : if (reduced_constant_expression_p (ptr))
1703 40 : if (const char *msg = c_getstr (ptr))
1704 40 : return xstrdup (msg);
1705 110 : auto_vec <char, 32> v;
1706 4308 : for (unsigned i = 0; i < INT_MAX; ++i)
1707 : {
1708 4308 : tree t = call;
1709 4308 : if (i)
1710 4198 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1711 4308 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1712 4308 : non_constant_p = false;
1713 4308 : overflow_p = false;
1714 4308 : jmp_target = NULL;
1715 4308 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1716 : &non_constant_p, &overflow_p,
1717 : &jmp_target);
1718 4308 : if (throws (&jmp_target)
1719 4308 : || non_constant_p
1720 4308 : || !tree_fits_shwi_p (t2))
1721 0 : return NULL;
1722 4308 : char c = tree_to_shwi (t2);
1723 4308 : v.safe_push (c);
1724 4308 : if (c == '\0')
1725 : break;
1726 : }
1727 220 : return xstrdup (v.address ());
1728 260 : }
1729 :
1730 : /* Diagnose constant expression evaluation encountering call to
1731 : std::terminate due to exception EXC. */
1732 :
1733 : static void
1734 11 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1735 : {
1736 11 : tree type = strip_array_types (TREE_TYPE (exc));
1737 11 : if (char *str = exception_what_str (ctx, exc))
1738 : {
1739 2 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1740 : "%<what()%>: %qs", "std::terminate", type, str);
1741 2 : free (str);
1742 : }
1743 : else
1744 : {
1745 9 : if (type != TREE_TYPE (exc))
1746 9 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1747 9 : bool non_constant_p = false, overflow_p = false;
1748 9 : tree jmp_target = NULL;
1749 9 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1750 : &non_constant_p, &overflow_p,
1751 : &jmp_target);
1752 9 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1753 9 : if (reduced_constant_expression_p (val))
1754 9 : error_at (loc, "%qs called after throwing an exception %qE",
1755 : "std::terminate", val);
1756 : else
1757 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1758 : "std::terminate", type);
1759 : }
1760 11 : }
1761 :
1762 : /* Diagnose constant expression evaluation encountering call to
1763 : uncaught exception EXC. */
1764 :
1765 : static void
1766 187 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1767 : {
1768 187 : tree type = strip_array_types (TREE_TYPE (exc));
1769 187 : if (char *str = exception_what_str (ctx, exc))
1770 : {
1771 148 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1772 148 : free (str);
1773 : }
1774 : else
1775 : {
1776 39 : if (type != TREE_TYPE (exc))
1777 39 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1778 39 : bool non_constant_p = false, overflow_p = false;
1779 39 : tree jmp_target = NULL;
1780 39 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1781 : &non_constant_p, &overflow_p,
1782 : &jmp_target);
1783 39 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1784 39 : if (reduced_constant_expression_p (val))
1785 38 : error_at (loc, "uncaught exception %qE", val);
1786 : else
1787 1 : error_at (loc, "uncaught exception of type %qT", type);
1788 : }
1789 187 : }
1790 :
1791 : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1792 : magic constexpr functions for C++26. */
1793 :
1794 : enum cxa_builtin {
1795 : CXA_NONE = 0,
1796 : CXA_ALLOCATE_EXCEPTION = 1,
1797 : CXA_FREE_EXCEPTION = 2,
1798 : CXA_THROW = 3,
1799 : CXA_BEGIN_CATCH = 4,
1800 : CXA_END_CATCH = 5,
1801 : CXA_RETHROW = 6,
1802 : CXA_GET_EXCEPTION_PTR = 7,
1803 : CXA_BAD_CAST = 8,
1804 : CXA_BAD_TYPEID = 9,
1805 : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1806 : STD_UNCAUGHT_EXCEPTIONS = 11,
1807 : STD_CURRENT_EXCEPTION = 12,
1808 : STD_RETHROW_EXCEPTION = 13,
1809 : BUILTIN_EH_PTR_ADJUST_REF = 14
1810 : };
1811 :
1812 : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1813 : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1814 :
1815 : static enum cxa_builtin
1816 40890432 : cxx_cxa_builtin_fn_p (tree fndecl)
1817 : {
1818 40890432 : if (cxx_dialect < cxx26)
1819 : return CXA_NONE;
1820 2557388 : if (DECL_LANGUAGE (fndecl) != lang_c)
1821 : {
1822 2245018 : if (!decl_in_std_namespace_p (fndecl))
1823 : return CXA_NONE;
1824 929249 : if (id_equal (DECL_NAME (fndecl), "uncaught_exceptions"))
1825 : return STD_UNCAUGHT_EXCEPTIONS;
1826 929193 : if (id_equal (DECL_NAME (fndecl), "current_exception"))
1827 : return STD_CURRENT_EXCEPTION;
1828 917177 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1829 : return STD_RETHROW_EXCEPTION;
1830 : return CXA_NONE;
1831 : }
1832 312370 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1833 : return CXA_NONE;
1834 54628 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1835 : return CXA_ALLOCATE_EXCEPTION;
1836 11808 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1837 : return CXA_FREE_EXCEPTION;
1838 11807 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1839 : return CXA_THROW;
1840 7238 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1841 : return CXA_BEGIN_CATCH;
1842 2538 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1843 : return CXA_END_CATCH;
1844 870 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1845 : return CXA_RETHROW;
1846 814 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1847 : return CXA_GET_EXCEPTION_PTR;
1848 784 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1849 : return CXA_BAD_CAST;
1850 500 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1851 : return CXA_BAD_TYPEID;
1852 492 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1853 : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1854 : return CXA_NONE;
1855 : }
1856 :
1857 : /* Helper function for cxx_eval_cxa_builtin_fn.
1858 : Check if ARG is a valid first argument of __cxa_throw or
1859 : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1860 : not, otherwise return the artificial __cxa_allocate_exception allocated
1861 : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1862 :
1863 : static tree
1864 812 : cxa_check_throw_arg (tree arg, bool free_exc)
1865 : {
1866 812 : STRIP_NOPS (arg);
1867 812 : if (TREE_CODE (arg) != ADDR_EXPR)
1868 : return NULL_TREE;
1869 812 : arg = TREE_OPERAND (arg, 0);
1870 812 : if (!VAR_P (arg)
1871 812 : || !DECL_ARTIFICIAL (arg)
1872 812 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1873 812 : && DECL_NAME (arg) != heap_identifier)
1874 1624 : || !DECL_LANG_SPECIFIC (arg))
1875 0 : return NULL_TREE;
1876 : return arg;
1877 : }
1878 :
1879 : /* Helper function for cxx_eval_cxa_builtin_fn.
1880 : "Allocate" on the constexpr heap an exception object of TYPE
1881 : with REFCOUNT. */
1882 :
1883 : static tree
1884 15754 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1885 : tree refcount)
1886 : {
1887 15754 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1888 15754 : DECL_ARTIFICIAL (var) = 1;
1889 15754 : retrofit_lang_decl (var);
1890 15754 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1891 15754 : ctx->global->heap_vars.safe_push (var);
1892 15754 : return var;
1893 : }
1894 :
1895 : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1896 : C++26 constexpr exception support (P3068R5). */
1897 :
1898 : static tree
1899 24906 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1900 : enum cxa_builtin kind, tree fndecl,
1901 : bool *non_constant_p, bool *overflow_p,
1902 : tree *jump_target)
1903 : {
1904 24906 : int nargs = call_expr_nargs (call);
1905 24906 : location_t loc = cp_expr_loc_or_input_loc (call);
1906 24906 : tree args[4], arg;
1907 24906 : if (nargs > 4)
1908 : {
1909 0 : invalid_nargs:
1910 0 : if (!ctx->quiet)
1911 0 : error_at (loc, "call to %qD function with incorrect "
1912 : "number of arguments", fndecl);
1913 0 : *non_constant_p = true;
1914 0 : return call;
1915 : }
1916 24906 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1917 3207 : && nargs == 1
1918 3207 : && (arg = CALL_EXPR_ARG (call, 0))
1919 3207 : && TREE_CODE (arg) == CALL_EXPR
1920 3207 : && call_expr_nargs (arg) == 1
1921 28113 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1922 3207 : if (tree fun = get_function_named_in_call (arg))
1923 3207 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1924 : {
1925 3207 : if (ctx->global->caught_exceptions.length () < 2)
1926 : {
1927 1523 : no_caught_exceptions:
1928 1523 : if (!ctx->quiet)
1929 0 : error_at (loc, "%qD called with no caught exceptions pending",
1930 : fndecl);
1931 1523 : *non_constant_p = true;
1932 1523 : return call;
1933 : }
1934 : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1935 : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1936 : ctx->global->caught_exceptions vector to end with
1937 : __cxa_allocate_exception created artificial VAR_DECL (the
1938 : exception object) followed by handler type, pushed by TRY_BLOCK
1939 : evaluation. The only difference between the functions is that
1940 : __cxa_begin_catch pops the handler type from the vector and keeps
1941 : the VAR_DECL last and decreases uncaught_exceptions. The
1942 : VAR_DECL after __cxa_begin_catch serves as the current exception
1943 : and is then popped in __cxa_end_catch evaluation. */
1944 1684 : tree handler_type = ctx->global->caught_exceptions.last ();
1945 1684 : if (handler_type && VAR_P (handler_type))
1946 0 : goto no_caught_exceptions;
1947 1684 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1948 1684 : arg = ctx->global->caught_exceptions[idx];
1949 1684 : gcc_assert (VAR_P (arg));
1950 1684 : if (kind == CXA_BEGIN_CATCH)
1951 : {
1952 1670 : ctx->global->caught_exceptions.pop ();
1953 1670 : --ctx->global->uncaught_exceptions;
1954 : }
1955 1684 : if (handler_type == NULL_TREE)
1956 : /* Used for catch (...). Just return void. */
1957 32 : return void_node;
1958 1652 : else if (POINTER_TYPE_P (handler_type))
1959 : {
1960 : /* Used for catch of a pointer. */
1961 33 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1962 33 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1963 : size_zero_node, NULL_TREE, NULL_TREE);
1964 33 : arg = cp_convert (handler_type, arg,
1965 33 : ctx->quiet ? tf_none : tf_warning_or_error);
1966 33 : if (arg == error_mark_node)
1967 : {
1968 0 : *non_constant_p = true;
1969 0 : return call;
1970 : }
1971 : }
1972 : else
1973 : {
1974 : /* Used for catch of a non-pointer type. */
1975 1619 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1976 1619 : tree exc_ptr_type = build_pointer_type (exc_type);
1977 1619 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
1978 1619 : if (CLASS_TYPE_P (handler_type))
1979 : {
1980 1578 : tree ptr_type = build_pointer_type (handler_type);
1981 1578 : arg = cp_convert (ptr_type, arg,
1982 1578 : ctx->quiet ? tf_none
1983 : : tf_warning_or_error);
1984 1578 : if (arg == error_mark_node)
1985 : {
1986 0 : *non_constant_p = true;
1987 0 : return call;
1988 : }
1989 : }
1990 : }
1991 1652 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
1992 : non_constant_p, overflow_p,
1993 1652 : jump_target);
1994 : }
1995 38175 : for (int i = 0; i < nargs; ++i)
1996 : {
1997 16476 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1998 : vc_prvalue, non_constant_p,
1999 : overflow_p, jump_target);
2000 16476 : if (*non_constant_p)
2001 : return call;
2002 16476 : if (*jump_target)
2003 : return NULL_TREE;
2004 : }
2005 21699 : switch (kind)
2006 : {
2007 14157 : case CXA_ALLOCATE_EXCEPTION:
2008 14157 : if (nargs != 1)
2009 0 : goto invalid_nargs;
2010 14157 : if (!tree_fits_uhwi_p (args[0]))
2011 : {
2012 0 : if (!ctx->quiet)
2013 0 : error_at (loc, "cannot allocate exception: size not constant");
2014 0 : *non_constant_p = true;
2015 0 : return call;
2016 : }
2017 : else
2018 : {
2019 28314 : tree type = build_array_type_nelts (char_type_node,
2020 14157 : tree_to_uhwi (args[0]));
2021 14157 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2022 14157 : ctx->global->put_value (var, NULL_TREE);
2023 14157 : return fold_convert (ptr_type_node, build_address (var));
2024 : }
2025 1 : case CXA_FREE_EXCEPTION:
2026 1 : if (nargs != 1)
2027 0 : goto invalid_nargs;
2028 1 : arg = cxa_check_throw_arg (args[0], true);
2029 1 : if (arg == NULL_TREE)
2030 : {
2031 0 : invalid_ptr:
2032 0 : if (!ctx->quiet)
2033 0 : error_at (loc, "first argument to %qD function not result of "
2034 : "%<__cxa_allocate_exception%>", fndecl);
2035 0 : *non_constant_p = true;
2036 0 : return call;
2037 : }
2038 1 : DECL_NAME (arg) = heap_deleted_identifier;
2039 1 : ctx->global->destroy_value (arg);
2040 1 : ctx->global->heap_dealloc_count++;
2041 1 : return void_node;
2042 718 : case CXA_THROW:
2043 718 : if (nargs != 3)
2044 0 : goto invalid_nargs;
2045 718 : arg = cxa_check_throw_arg (args[0], false);
2046 718 : if (arg == NULL_TREE)
2047 0 : goto invalid_ptr;
2048 718 : DECL_EXCEPTION_REFCOUNT (arg)
2049 718 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2050 : size_one_node);
2051 718 : ++ctx->global->uncaught_exceptions;
2052 718 : *jump_target = arg;
2053 718 : return void_node;
2054 0 : case CXA_BEGIN_CATCH:
2055 0 : case CXA_GET_EXCEPTION_PTR:
2056 0 : goto invalid_nargs;
2057 1668 : case CXA_END_CATCH:
2058 1668 : if (nargs != 0)
2059 0 : goto invalid_nargs;
2060 1668 : if (ctx->global->caught_exceptions.is_empty ())
2061 : {
2062 0 : no_active_exc:
2063 0 : if (!ctx->quiet)
2064 0 : error_at (loc, "%qD called with no caught exceptions active",
2065 : fndecl);
2066 0 : *non_constant_p = true;
2067 0 : return call;
2068 : }
2069 : else
2070 : {
2071 1668 : arg = ctx->global->caught_exceptions.pop ();
2072 1668 : if (arg == NULL_TREE || !VAR_P (arg))
2073 0 : goto no_active_exc;
2074 1668 : free_except:
2075 1711 : DECL_EXCEPTION_REFCOUNT (arg)
2076 1711 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2077 : size_one_node);
2078 1711 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2079 : {
2080 1605 : if (type_build_dtor_call (TREE_TYPE (arg)))
2081 : {
2082 : /* So that we don't complain about out-of-consteval use. */
2083 1534 : temp_override<tree> ovr (current_function_decl);
2084 1534 : if (ctx->call && ctx->call->fundef)
2085 1534 : current_function_decl = ctx->call->fundef->decl;
2086 1534 : tree cleanup
2087 1545 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2088 : : tf_warning_or_error));
2089 1534 : if (cleanup == error_mark_node)
2090 0 : *non_constant_p = true;
2091 1534 : tree jmp_target = NULL_TREE;
2092 1534 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2093 : non_constant_p, overflow_p,
2094 : &jmp_target);
2095 1534 : if (throws (&jmp_target))
2096 0 : *jump_target = jmp_target;
2097 1534 : }
2098 1605 : DECL_NAME (arg) = heap_deleted_identifier;
2099 1605 : ctx->global->destroy_value (arg);
2100 1605 : ctx->global->heap_dealloc_count++;
2101 : }
2102 : }
2103 1711 : return void_node;
2104 50 : case CXA_RETHROW:
2105 50 : if (nargs != 0)
2106 0 : goto invalid_nargs;
2107 50 : unsigned idx;
2108 100 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2109 36 : if (arg == NULL_TREE || !VAR_P (arg))
2110 0 : --idx;
2111 : else
2112 : break;
2113 50 : if (arg == NULL_TREE)
2114 : {
2115 14 : if (!ctx->quiet)
2116 4 : error_at (loc, "%qD called with no caught exceptions active",
2117 : fndecl);
2118 14 : *non_constant_p = true;
2119 14 : return call;
2120 : }
2121 36 : DECL_EXCEPTION_REFCOUNT (arg)
2122 36 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2123 36 : ++ctx->global->uncaught_exceptions;
2124 36 : *jump_target = arg;
2125 36 : return void_node;
2126 143 : case CXA_BAD_CAST:
2127 143 : case CXA_BAD_TYPEID:
2128 143 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2129 143 : if (nargs != 0)
2130 0 : goto invalid_nargs;
2131 : else
2132 : {
2133 143 : tree name;
2134 143 : switch (kind)
2135 : {
2136 117 : case CXA_BAD_CAST:
2137 117 : name = get_identifier ("bad_cast");
2138 117 : break;
2139 5 : case CXA_BAD_TYPEID:
2140 5 : name = get_identifier ("bad_typeid");
2141 5 : break;
2142 21 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2143 21 : name = get_identifier ("bad_array_new_length");
2144 21 : break;
2145 : default:
2146 : gcc_unreachable ();
2147 : }
2148 143 : tree decl = lookup_qualified_name (std_node, name);
2149 143 : if (TREE_CODE (decl) != TYPE_DECL
2150 128 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2151 271 : || !type_build_ctor_call (TREE_TYPE (decl)))
2152 : {
2153 15 : if (!ctx->quiet)
2154 4 : error_at (loc, "%qD called without %<std::%D%> being defined",
2155 : fndecl, name);
2156 15 : *non_constant_p = true;
2157 15 : return call;
2158 : }
2159 128 : tree type = TREE_TYPE (decl);
2160 128 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2161 128 : tree ctor
2162 128 : = build_special_member_call (var, complete_ctor_identifier,
2163 : NULL, type, LOOKUP_NORMAL,
2164 128 : ctx->quiet ? tf_none
2165 : : tf_warning_or_error);
2166 128 : if (ctor == error_mark_node)
2167 : {
2168 0 : *non_constant_p = true;
2169 0 : return call;
2170 : }
2171 128 : if (TREE_CONSTANT (ctor))
2172 0 : ctx->global->put_value (var, ctor);
2173 : else
2174 : {
2175 128 : ctx->global->put_value (var, NULL_TREE);
2176 128 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2177 : non_constant_p, overflow_p,
2178 : jump_target);
2179 128 : if (*non_constant_p)
2180 : return call;
2181 128 : if (throws (jump_target))
2182 : return NULL_TREE;
2183 : }
2184 128 : ++ctx->global->uncaught_exceptions;
2185 128 : *jump_target = var;
2186 : }
2187 128 : return void_node;
2188 46 : case STD_UNCAUGHT_EXCEPTIONS:
2189 46 : if (nargs != 0)
2190 0 : goto invalid_nargs;
2191 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2192 : want to give a definite answer during mce_unknown evaluation,
2193 : because that might prevent evaluation later on when some
2194 : exceptions might be uncaught. But unlike that, we don't
2195 : want to constant fold it even during cp_fold, because at runtime
2196 : std::uncaught_exceptions () might still be non-zero. */
2197 46 : if (ctx->manifestly_const_eval != mce_true)
2198 : {
2199 25 : *non_constant_p = true;
2200 25 : return call;
2201 : }
2202 21 : return build_int_cst (integer_type_node,
2203 21 : ctx->global->uncaught_exceptions);
2204 4823 : case STD_CURRENT_EXCEPTION:
2205 4823 : if (nargs != 0)
2206 0 : goto invalid_nargs;
2207 : else
2208 : {
2209 4823 : tree name = get_identifier ("exception_ptr");
2210 4823 : tree decl = lookup_qualified_name (std_node, name);
2211 4823 : tree fld;
2212 4823 : if (TREE_CODE (decl) != TYPE_DECL
2213 4823 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2214 4823 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2215 4823 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2216 4823 : || DECL_ARTIFICIAL (fld)
2217 4823 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2218 4823 : || next_aggregate_field (DECL_CHAIN (fld))
2219 9646 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2220 4823 : TYPE_SIZE (TREE_TYPE (fld))))
2221 : {
2222 0 : if (!ctx->quiet)
2223 0 : error_at (loc, "%qD called without supportable %qs",
2224 : fndecl, "std::exception_ptr");
2225 0 : *non_constant_p = true;
2226 0 : return call;
2227 : }
2228 9646 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2229 21 : if (arg == NULL_TREE || !VAR_P (arg))
2230 0 : --idx;
2231 : else
2232 : break;
2233 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2234 : want to give a definite answer during mce_unknown evaluation,
2235 : because that might prevent evaluation later on when some
2236 : exceptions might be current. But unlike that, we don't
2237 : want to constant fold it to null even during cp_fold, because
2238 : at runtime std::current_exception () might still be non-null. */
2239 4823 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2240 : {
2241 4791 : *non_constant_p = true;
2242 4791 : return call;
2243 : }
2244 32 : if (arg == NULL_TREE)
2245 11 : arg = build_zero_cst (TREE_TYPE (fld));
2246 : else
2247 : {
2248 21 : DECL_EXCEPTION_REFCOUNT (arg)
2249 21 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2250 : size_one_node);
2251 21 : arg = fold_convert (ptr_type_node, build_address (arg));
2252 : }
2253 32 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2254 : }
2255 22 : case STD_RETHROW_EXCEPTION:
2256 22 : if (nargs != 1)
2257 0 : goto invalid_nargs;
2258 22 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2259 : {
2260 22 : arg = args[0];
2261 22 : STRIP_NOPS (arg);
2262 22 : if (TREE_CODE (arg) == ADDR_EXPR)
2263 : {
2264 22 : args[0]
2265 22 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2266 : vc_prvalue, non_constant_p,
2267 : overflow_p, jump_target);
2268 22 : if (*non_constant_p)
2269 : return call;
2270 22 : if (*jump_target)
2271 : return NULL_TREE;
2272 : }
2273 : }
2274 22 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2275 22 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2276 : {
2277 0 : invalid_std_rethrow:
2278 0 : if (!ctx->quiet)
2279 0 : error_at (loc, "%qD called with unexpected %qs argument",
2280 : fndecl, "std::exception_ptr");
2281 0 : *non_constant_p = true;
2282 0 : return void_node;
2283 : }
2284 22 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2285 22 : if (arg == NULL_TREE)
2286 0 : goto invalid_std_rethrow;
2287 22 : DECL_EXCEPTION_REFCOUNT (arg)
2288 22 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2289 22 : ++ctx->global->uncaught_exceptions;
2290 22 : *jump_target = arg;
2291 22 : return void_node;
2292 71 : case BUILTIN_EH_PTR_ADJUST_REF:
2293 71 : if (nargs != 2)
2294 0 : goto invalid_nargs;
2295 71 : arg = cxa_check_throw_arg (args[0], false);
2296 71 : if (arg == NULL_TREE)
2297 0 : goto invalid_ptr;
2298 71 : if (integer_onep (args[1]))
2299 28 : DECL_EXCEPTION_REFCOUNT (arg)
2300 56 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2301 : size_one_node);
2302 43 : else if (integer_minus_onep (args[1]))
2303 43 : goto free_except;
2304 : else
2305 : {
2306 0 : if (!ctx->quiet)
2307 0 : error_at (loc, "%qD called with second argument "
2308 : "other than 1 or -1", fndecl);
2309 0 : *non_constant_p = true;
2310 : }
2311 28 : return void_node;
2312 0 : default:
2313 0 : gcc_unreachable ();
2314 : }
2315 : }
2316 :
2317 : /* Variables and functions to manage constexpr call expansion context.
2318 : These do not need to be marked for PCH or GC. */
2319 :
2320 : /* FIXME remember and print actual constant arguments. */
2321 : static vec<tree> call_stack;
2322 : static int call_stack_tick;
2323 : static int last_cx_error_tick;
2324 :
2325 : /* Attempt to evaluate T which represents a call to __builtin_constexpr_diag.
2326 : The arguments should be an integer (0 for inform, 1 for warning, 2 for
2327 : error) optionally with 16 ored in if it should use caller's caller location
2328 : instead of caller's location and 2 messages which are either a pointer to
2329 : a STRING_CST or class with data () and size () member functions like
2330 : string_view or u8string_view. The first message is a tag, with "" passed
2331 : for no tag, data () should return const char *, the tag should only contain
2332 : alphanumeric letters or underscores. The second message is the diagnostic
2333 : message, data () can be either const char * or const char8_t *. size ()
2334 : should return the corresponding length of the strings in bytes as an
2335 : integer. */
2336 :
2337 : static tree
2338 98 : cxx_eval_constexpr_diag (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
2339 : bool *overflow_p, tree *jump_target)
2340 : {
2341 98 : location_t loc = EXPR_LOCATION (t);
2342 98 : if (call_expr_nargs (t) != 3)
2343 : {
2344 6 : if (!ctx->quiet)
2345 6 : error_at (loc, "wrong number of arguments to %qs call",
2346 : "__builtin_constexpr_diag");
2347 6 : *non_constant_p = true;
2348 6 : return t;
2349 : }
2350 : tree args[3];
2351 368 : for (int i = 0; i < 3; ++i)
2352 : {
2353 276 : tree arg = CALL_EXPR_ARG (t, i);
2354 276 : arg = cxx_eval_constant_expression (ctx, arg,
2355 : (i == 0
2356 250 : || POINTER_TYPE_P (TREE_TYPE (arg)))
2357 460 : ? vc_prvalue : vc_glvalue,
2358 : non_constant_p, overflow_p,
2359 : jump_target);
2360 276 : if (*jump_target)
2361 : return NULL_TREE;
2362 276 : if (*non_constant_p)
2363 : return t;
2364 276 : args[i] = arg;
2365 : }
2366 184 : if (TREE_CODE (args[0]) != INTEGER_CST
2367 92 : || wi::to_widest (args[0]) < 0
2368 90 : || wi::to_widest (args[0]) > 18
2369 184 : || (wi::to_widest (args[0]) & 15) > 2)
2370 : {
2371 4 : if (!ctx->quiet)
2372 4 : error_at (loc, "first %qs call argument should be 0, 1, 2, 16, 17 or "
2373 : "18", "__builtin_constexpr_diag");
2374 4 : *non_constant_p = true;
2375 4 : return t;
2376 : }
2377 88 : const char *msgs[2] = {};
2378 88 : int lens[3] = {};
2379 528 : cexpr_str cstrs[2];
2380 218 : diagnostics::kind kind = diagnostics::kind::error;
2381 218 : for (int i = 1; i < 3; ++i)
2382 : {
2383 158 : tree arg = args[i];
2384 158 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2385 : {
2386 92 : tree str = arg;
2387 92 : STRIP_NOPS (str);
2388 92 : if (TREE_CODE (str) == ADDR_EXPR
2389 92 : && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST)
2390 : {
2391 92 : str = TREE_OPERAND (str, 0);
2392 92 : tree eltype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)));
2393 92 : if (eltype == char_type_node
2394 12 : || (i == 2 && eltype == char8_type_node))
2395 158 : arg = str;
2396 : }
2397 : }
2398 158 : cstrs[i - 1].message = arg;
2399 158 : if (!cstrs[i - 1].type_check (loc, i == 2))
2400 : {
2401 20 : *non_constant_p = true;
2402 20 : return t;
2403 : }
2404 138 : if (!cstrs[i - 1].extract (loc, msgs[i - 1], lens[i - 1], ctx,
2405 : non_constant_p, overflow_p, jump_target))
2406 : {
2407 8 : if (*jump_target)
2408 : return NULL_TREE;
2409 8 : *non_constant_p = true;
2410 8 : return t;
2411 : }
2412 : }
2413 60 : if (msgs[0])
2414 : {
2415 356 : for (int i = 0; i < lens[0]; ++i)
2416 298 : if (!ISALNUM (msgs[0][i]) && msgs[0][i] != '_')
2417 : {
2418 2 : if (!ctx->quiet)
2419 2 : error_at (loc, "%qs tag string contains %qc character other than"
2420 : " letters, digits or %<_%>",
2421 : "__builtin_constexpr_diag", msgs[0][i]);
2422 2 : *non_constant_p = true;
2423 2 : return t;
2424 : }
2425 : }
2426 58 : if (ctx->manifestly_const_eval == mce_unknown)
2427 : {
2428 0 : *non_constant_p = true;
2429 0 : return t;
2430 : }
2431 58 : int arg0 = tree_to_uhwi (args[0]);
2432 58 : if (arg0 & 16)
2433 : {
2434 16 : arg0 &= 15;
2435 16 : if (!call_stack.is_empty ())
2436 : {
2437 16 : tree call = call_stack.last ();
2438 16 : if (EXPR_HAS_LOCATION (call))
2439 16 : loc = EXPR_LOCATION (call);
2440 : }
2441 : }
2442 58 : if (arg0 == 0)
2443 : kind = diagnostics::kind::note;
2444 36 : else if (arg0 == 1)
2445 18 : kind = diagnostics::kind::warning;
2446 58 : if (lens[0])
2447 : {
2448 44 : const char *color = "error";
2449 44 : if (kind == diagnostics::kind::note)
2450 : color = "note";
2451 30 : else if (kind == diagnostics::kind::warning)
2452 16 : color = "warning";
2453 44 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s [%r%.*s%R]",
2454 : lens[1], msgs[1], color, lens[0], msgs[0]);
2455 : }
2456 : else
2457 14 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s",
2458 : lens[1], msgs[1]);
2459 58 : return void_node;
2460 264 : }
2461 :
2462 : /* Attempt to evaluate T which represents a call to a builtin function.
2463 : We assume here that all builtin functions evaluate to scalar types
2464 : represented by _CST nodes. */
2465 :
2466 : static tree
2467 16863745 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2468 : value_cat lval,
2469 : bool *non_constant_p, bool *overflow_p,
2470 : tree *jump_target)
2471 : {
2472 16863745 : const int nargs = call_expr_nargs (t);
2473 16863745 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2474 16863745 : tree new_call;
2475 16863745 : int i;
2476 :
2477 : /* Don't fold __builtin_constant_p within a constexpr function. */
2478 16863745 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2479 :
2480 : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2481 : in a constexpr function until we have values for the parameters. */
2482 1938235 : if (bi_const_p
2483 1938235 : && ctx->manifestly_const_eval != mce_true
2484 1924824 : && current_function_decl
2485 1923705 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2486 : {
2487 1636216 : *non_constant_p = true;
2488 1636216 : return t;
2489 : }
2490 :
2491 : /* For __builtin_is_constant_evaluated, defer it if not
2492 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2493 : without manifestly_const_eval even expressions or parts thereof which
2494 : will later be manifestly const_eval evaluated), otherwise fold it to
2495 : true. */
2496 15227529 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
2497 : BUILT_IN_FRONTEND))
2498 : {
2499 5868264 : if (ctx->manifestly_const_eval == mce_unknown)
2500 : {
2501 5834199 : *non_constant_p = true;
2502 5834199 : return t;
2503 : }
2504 34065 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2505 34065 : boolean_type_node);
2506 : }
2507 :
2508 9359265 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
2509 : {
2510 2005 : temp_override<tree> ovr (current_function_decl);
2511 2005 : if (ctx->call && ctx->call->fundef)
2512 1406 : current_function_decl = ctx->call->fundef->decl;
2513 2005 : return fold_builtin_source_location (t);
2514 2005 : }
2515 :
2516 9357260 : if (fndecl_built_in_p (fun, CP_BUILT_IN_EH_PTR_ADJUST_REF,
2517 : BUILT_IN_FRONTEND))
2518 71 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2519 : fun, non_constant_p, overflow_p,
2520 71 : jump_target);
2521 :
2522 9357189 : if (fndecl_built_in_p (fun, CP_BUILT_IN_CONSTEXPR_DIAG, BUILT_IN_FRONTEND))
2523 98 : return cxx_eval_constexpr_diag (ctx, t, non_constant_p, overflow_p,
2524 98 : jump_target);
2525 :
2526 9357091 : int strops = 0;
2527 9357091 : int strret = 0;
2528 9357091 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2529 8653442 : switch (DECL_FUNCTION_CODE (fun))
2530 : {
2531 : case BUILT_IN_STRLEN:
2532 : case BUILT_IN_STRNLEN:
2533 9356995 : strops = 1;
2534 : break;
2535 110821 : case BUILT_IN_MEMCHR:
2536 110821 : case BUILT_IN_STRCHR:
2537 110821 : case BUILT_IN_STRRCHR:
2538 110821 : strops = 1;
2539 110821 : strret = 1;
2540 110821 : break;
2541 71638 : case BUILT_IN_MEMCMP:
2542 71638 : case BUILT_IN_STRCMP:
2543 71638 : strops = 2;
2544 71638 : break;
2545 28629 : case BUILT_IN_STRSTR:
2546 28629 : strops = 2;
2547 28629 : strret = 1;
2548 28629 : break;
2549 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2550 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2551 42 : case BUILT_IN_OBSERVABLE_CHKPT:
2552 : /* These builtins shall be ignored during constant expression
2553 : evaluation. */
2554 42 : return void_node;
2555 54 : case BUILT_IN_UNREACHABLE:
2556 54 : case BUILT_IN_TRAP:
2557 54 : if (!*non_constant_p && !ctx->quiet)
2558 : {
2559 : /* Do not allow__builtin_unreachable in constexpr function.
2560 : The __builtin_unreachable call with BUILTINS_LOCATION
2561 : comes from cp_maybe_instrument_return. */
2562 13 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2563 0 : error ("%<constexpr%> call flows off the end of the function");
2564 : else
2565 13 : error ("%q+E is not a constant expression", t);
2566 : }
2567 54 : *non_constant_p = true;
2568 54 : return t;
2569 : default:
2570 : break;
2571 : }
2572 :
2573 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2574 : return constant false for a non-constant argument. */
2575 9356995 : constexpr_ctx new_ctx = *ctx;
2576 9356995 : new_ctx.quiet = true;
2577 25820601 : for (i = 0; i < nargs; ++i)
2578 : {
2579 16463607 : tree arg = CALL_EXPR_ARG (t, i);
2580 16463607 : tree oarg = arg;
2581 :
2582 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2583 : expand_builtin doesn't know how to look in the values table. */
2584 16463607 : bool strop = i < strops;
2585 16463607 : if (strop)
2586 : {
2587 449083 : STRIP_NOPS (arg);
2588 449083 : if (TREE_CODE (arg) == ADDR_EXPR)
2589 7867 : arg = TREE_OPERAND (arg, 0);
2590 : else
2591 : strop = false;
2592 : }
2593 :
2594 : /* If builtin_valid_in_constant_expr_p is true,
2595 : potential_constant_expression_1 has not recursed into the arguments
2596 : of the builtin, verify it here. */
2597 16463607 : if (!builtin_valid_in_constant_expr_p (fun)
2598 16463607 : || potential_constant_expression (arg))
2599 : {
2600 16463476 : bool dummy1 = false, dummy2 = false;
2601 16463476 : tree jmp_target = NULL_TREE;
2602 16463476 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2603 : &dummy1, &dummy2, &jmp_target);
2604 16463475 : if (jmp_target)
2605 : {
2606 0 : *jump_target = jmp_target;
2607 0 : return NULL_TREE;
2608 : }
2609 : }
2610 :
2611 16463606 : if (bi_const_p)
2612 : /* For __builtin_constant_p, fold all expressions with constant values
2613 : even if they aren't C++ constant-expressions. */
2614 302018 : arg = cp_fold_rvalue (arg);
2615 16161588 : else if (strop)
2616 : {
2617 7867 : if (TREE_CODE (arg) == CONSTRUCTOR)
2618 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2619 7867 : if (TREE_CODE (arg) == STRING_CST)
2620 2926 : arg = build_address (arg);
2621 : else
2622 : arg = oarg;
2623 : }
2624 :
2625 16463606 : args[i] = arg;
2626 : }
2627 :
2628 9356994 : bool save_ffbcp = force_folding_builtin_constant_p;
2629 9356994 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2630 9356994 : tree save_cur_fn = current_function_decl;
2631 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2632 9356994 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2633 38 : && ctx->call
2634 9356998 : && ctx->call->fundef)
2635 4 : current_function_decl = ctx->call->fundef->decl;
2636 9356994 : if (fndecl_built_in_p (fun,
2637 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2638 : BUILT_IN_FRONTEND))
2639 : {
2640 336 : location_t loc = EXPR_LOCATION (t);
2641 336 : if (nargs >= 1)
2642 333 : VERIFY_CONSTANT (args[0]);
2643 136 : new_call
2644 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2645 : args);
2646 : }
2647 9356658 : else if (fndecl_built_in_p (fun,
2648 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2649 : BUILT_IN_FRONTEND))
2650 : {
2651 471 : location_t loc = EXPR_LOCATION (t);
2652 471 : if (nargs >= 2)
2653 : {
2654 465 : VERIFY_CONSTANT (args[0]);
2655 237 : VERIFY_CONSTANT (args[1]);
2656 : }
2657 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2658 : }
2659 9356187 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
2660 : BUILT_IN_FRONTEND))
2661 : {
2662 1658 : location_t loc = EXPR_LOCATION (t);
2663 1658 : if (nargs >= 1)
2664 : {
2665 1658 : tree arg = CALL_EXPR_ARG (t, 0);
2666 1658 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2667 : non_constant_p, overflow_p,
2668 : jump_target);
2669 1658 : if (*jump_target)
2670 : return NULL_TREE;
2671 1658 : args[0] = arg;
2672 : }
2673 1658 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
2674 : }
2675 : else
2676 18709058 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2677 9354529 : CALL_EXPR_FN (t), nargs, args);
2678 9356566 : current_function_decl = save_cur_fn;
2679 9356566 : force_folding_builtin_constant_p = save_ffbcp;
2680 9356566 : if (new_call == NULL)
2681 : {
2682 6578881 : if (!*non_constant_p && !ctx->quiet)
2683 : {
2684 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2685 0 : CALL_EXPR_FN (t), nargs, args);
2686 0 : error ("%q+E is not a constant expression", new_call);
2687 : }
2688 6578881 : *non_constant_p = true;
2689 6578881 : return t;
2690 : }
2691 :
2692 2777685 : if (!potential_constant_expression (new_call))
2693 : {
2694 730 : if (!*non_constant_p && !ctx->quiet)
2695 4 : error ("%q+E is not a constant expression", new_call);
2696 730 : *non_constant_p = true;
2697 730 : return t;
2698 : }
2699 :
2700 2776955 : if (strret)
2701 : {
2702 : /* memchr returns a pointer into the first argument, but we replaced the
2703 : argument above with a STRING_CST; put it back it now. */
2704 119 : tree op = CALL_EXPR_ARG (t, strret-1);
2705 119 : STRIP_NOPS (new_call);
2706 119 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2707 60 : TREE_OPERAND (new_call, 0) = op;
2708 59 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2709 2776955 : new_call = op;
2710 : }
2711 :
2712 2776955 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2713 : non_constant_p, overflow_p,
2714 2776955 : jump_target);
2715 : }
2716 :
2717 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2718 : the type of the value to match. */
2719 :
2720 : static tree
2721 137028610 : adjust_temp_type (tree type, tree temp)
2722 : {
2723 137028610 : if (same_type_p (TREE_TYPE (temp), type))
2724 : return temp;
2725 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2726 72318529 : if (TREE_CODE (temp) == CONSTRUCTOR)
2727 : {
2728 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2729 3340345 : tree t = copy_node (temp);
2730 3340345 : TREE_TYPE (t) = type;
2731 3340345 : return t;
2732 : }
2733 68978184 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2734 0 : return build0 (EMPTY_CLASS_EXPR, type);
2735 68978184 : gcc_assert (scalarish_type_p (type));
2736 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2737 : type is cv-unqualified. */
2738 68978184 : return cp_fold_convert (cv_unqualified (type), temp);
2739 : }
2740 :
2741 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2742 : sub-CONSTRUCTORs. Otherwise return T.
2743 :
2744 : We use this whenever we initialize an object as a whole, whether it's a
2745 : parameter, a local variable, or a subobject, so that subsequent
2746 : modifications don't affect other places where it was used. */
2747 :
2748 : tree
2749 85217472 : unshare_constructor (tree t MEM_STAT_DECL)
2750 : {
2751 85217472 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2752 : return t;
2753 16102629 : auto_vec <tree*, 4> ptrs;
2754 16102629 : ptrs.safe_push (&t);
2755 16102629 : while (!ptrs.is_empty ())
2756 : {
2757 19037042 : tree *p = ptrs.pop ();
2758 19037042 : tree n = copy_node (*p PASS_MEM_STAT);
2759 30146288 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2760 19037042 : *p = n;
2761 19037042 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2762 19037042 : constructor_elt *ce;
2763 77547540 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2764 23370827 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2765 2934413 : ptrs.safe_push (&ce->value);
2766 : }
2767 16102629 : return t;
2768 16102629 : }
2769 :
2770 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2771 :
2772 : static void
2773 867247 : free_constructor (tree t)
2774 : {
2775 867247 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2776 0 : return;
2777 867247 : releasing_vec ctors;
2778 867247 : vec_safe_push (ctors, t);
2779 1772237 : while (!ctors->is_empty ())
2780 : {
2781 904990 : tree c = ctors->pop ();
2782 904990 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2783 : {
2784 : constructor_elt *ce;
2785 314776 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2786 203032 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2787 37743 : vec_safe_push (ctors, ce->value);
2788 111744 : ggc_free (elts);
2789 : }
2790 904990 : ggc_free (c);
2791 : }
2792 867247 : }
2793 :
2794 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2795 : if *TP is address of a static variable (or part of it) currently being
2796 : constructed or of a heap artificial variable. */
2797 :
2798 : static tree
2799 31473686 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2800 : {
2801 31473686 : if (TREE_CODE (*tp) == ADDR_EXPR)
2802 5719093 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2803 5719093 : if (VAR_P (var) && TREE_STATIC (var))
2804 : {
2805 2548934 : if (DECL_NAME (var) == heap_uninit_identifier
2806 2548934 : || DECL_NAME (var) == heap_identifier
2807 2548934 : || DECL_NAME (var) == heap_vec_uninit_identifier
2808 5097868 : || DECL_NAME (var) == heap_vec_identifier)
2809 : return var;
2810 :
2811 2548934 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2812 2548934 : if (global->get_value (var))
2813 : return var;
2814 : }
2815 31107679 : if (TYPE_P (*tp))
2816 1777 : *walk_subtrees = false;
2817 : return NULL_TREE;
2818 : }
2819 :
2820 : /* Subroutine of cxx_eval_call_expression.
2821 : We are processing a call expression (either CALL_EXPR or
2822 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2823 : all arguments and bind their values to correspondings
2824 : parameters, making up the NEW_CALL context. */
2825 :
2826 : static tree
2827 180796702 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2828 : tree orig_fun, bool *non_constant_p,
2829 : bool *overflow_p, bool *non_constant_args,
2830 : tree *jump_target)
2831 : {
2832 180796702 : int nargs = call_expr_nargs (t);
2833 180796702 : tree parms = DECL_ARGUMENTS (fun);
2834 180796702 : int i, j = 0;
2835 180796702 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2836 1239 : ++nargs;
2837 180796702 : if (DECL_HAS_VTT_PARM_P (fun)
2838 1241 : && fun != orig_fun
2839 180797941 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2840 902 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2841 348 : ++nargs;
2842 : /* We don't record ellipsis args below. */
2843 180796702 : int nparms = list_length (parms);
2844 180796702 : int nbinds = nargs < nparms ? nargs : nparms;
2845 180796702 : tree binds = make_tree_vec (nbinds);
2846 :
2847 : /* The call is not a constant expression if it involves the cdtor for a type
2848 : with virtual bases before C++26. */
2849 180796702 : if (cxx_dialect < cxx26
2850 180796702 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2851 : {
2852 17 : if (!ctx->quiet)
2853 : {
2854 3 : error_at (cp_expr_loc_or_input_loc (t),
2855 : "call to non-%<constexpr%> function %qD", fun);
2856 3 : explain_invalid_constexpr_fn (fun);
2857 : }
2858 17 : *non_constant_p = true;
2859 17 : return binds;
2860 : }
2861 :
2862 307273322 : for (i = 0; i < nargs; ++i)
2863 : {
2864 210260425 : tree x, arg;
2865 210260425 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2866 210260425 : if (parms && DECL_BY_REFERENCE (parms))
2867 8404 : type = TREE_TYPE (type);
2868 210260425 : if (i == 1
2869 210260425 : && j == 0
2870 44321536 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2871 210261553 : && orig_fun != fun)
2872 : {
2873 1128 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2874 1128 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2875 328 : x = boolean_true_node;
2876 : else
2877 800 : x = boolean_false_node;
2878 : j = -1;
2879 : }
2880 210259297 : else if (i == 2
2881 210259297 : && j == -1
2882 1128 : && DECL_HAS_VTT_PARM_P (fun)
2883 1128 : && orig_fun != fun
2884 210260425 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2885 811 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2886 : {
2887 328 : x = build_zero_cst (type);
2888 328 : j = -2;
2889 : }
2890 : else
2891 210258969 : x = get_nth_callarg (t, i + j);
2892 : /* For member function, the first argument is a pointer to the implied
2893 : object. For a constructor, it might still be a dummy object, in
2894 : which case we get the real argument from ctx. */
2895 316145730 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2896 238061277 : && is_dummy_object (x))
2897 : {
2898 14015426 : x = ctx->object;
2899 14015426 : x = build_address (x);
2900 : }
2901 210260425 : if (TREE_ADDRESSABLE (type))
2902 : {
2903 : /* Undo convert_for_arg_passing work here. */
2904 14577 : x = convert_from_reference (x);
2905 14577 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2906 : non_constant_p, overflow_p,
2907 : jump_target);
2908 : }
2909 : else
2910 : /* Normally we would strip a TARGET_EXPR in an initialization context
2911 : such as this, but here we do the elision differently: we keep the
2912 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2913 210245848 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2914 : non_constant_p, overflow_p,
2915 : jump_target);
2916 210260425 : if (*jump_target)
2917 : break;
2918 : /* Check we aren't dereferencing a null pointer when calling a non-static
2919 : member function, which is undefined behaviour. */
2920 158072859 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2921 108053046 : && integer_zerop (arg)
2922 : /* But ignore calls from within compiler-generated code, to handle
2923 : cases like lambda function pointer conversion operator thunks
2924 : which pass NULL as the 'this' pointer. */
2925 210404217 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2926 : {
2927 476 : if (!ctx->quiet)
2928 6 : error_at (cp_expr_loc_or_input_loc (x),
2929 : "dereferencing a null pointer");
2930 476 : *non_constant_p = true;
2931 : }
2932 : /* Don't VERIFY_CONSTANT here. */
2933 210260415 : if (*non_constant_p && ctx->quiet)
2934 : break;
2935 : /* Just discard ellipsis args after checking their constantitude. */
2936 126476637 : if (!parms)
2937 283 : continue;
2938 :
2939 126476354 : if (!*non_constant_p)
2940 : {
2941 : /* Make sure the binding has the same type as the parm. But
2942 : only for constant args. */
2943 126475753 : if (TREE_ADDRESSABLE (type))
2944 : {
2945 8970 : if (!same_type_p (type, TREE_TYPE (arg)))
2946 : {
2947 9 : arg = build_fold_addr_expr (arg);
2948 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2949 9 : arg = convert_from_reference (arg);
2950 : }
2951 : }
2952 126466783 : else if (!TYPE_REF_P (type))
2953 103913225 : arg = adjust_temp_type (type, arg);
2954 126475753 : if (!TREE_CONSTANT (arg))
2955 90253703 : *non_constant_args = true;
2956 36222050 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2957 : /* The destructor needs to see any modifications the callee makes
2958 : to the argument. */
2959 0 : *non_constant_args = true;
2960 : /* If arg is or contains address of a heap artificial variable or
2961 : of a static variable being constructed, avoid caching the
2962 : function call, as those variables might be modified by the
2963 : function, or might be modified by the callers in between
2964 : the cached function and just read by the function. */
2965 36222050 : else if (!*non_constant_args
2966 36222050 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2967 : NULL))
2968 366007 : *non_constant_args = true;
2969 :
2970 : /* For virtual calls, adjust the this argument, so that it is
2971 : the object on which the method is called, rather than
2972 : one of its bases. */
2973 126475753 : if (i == 0 && DECL_VIRTUAL_P (fun))
2974 : {
2975 20655 : tree addr = arg;
2976 20655 : STRIP_NOPS (addr);
2977 20655 : if (TREE_CODE (addr) == ADDR_EXPR)
2978 : {
2979 20635 : tree obj = TREE_OPERAND (addr, 0);
2980 20635 : while (TREE_CODE (obj) == COMPONENT_REF
2981 9784 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
2982 30421 : && !same_type_ignoring_top_level_qualifiers_p
2983 9732 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
2984 54 : obj = TREE_OPERAND (obj, 0);
2985 20635 : if (obj != TREE_OPERAND (addr, 0))
2986 51 : arg = build_fold_addr_expr_with_type (obj,
2987 : TREE_TYPE (arg));
2988 : }
2989 : }
2990 126475753 : TREE_VEC_ELT (binds, i) = arg;
2991 : }
2992 126476354 : parms = TREE_CHAIN (parms);
2993 : }
2994 :
2995 : return binds;
2996 : }
2997 :
2998 : static int
2999 96344032 : push_cx_call_context (tree call)
3000 : {
3001 96344032 : ++call_stack_tick;
3002 96344032 : if (!EXPR_HAS_LOCATION (call))
3003 69453 : SET_EXPR_LOCATION (call, input_location);
3004 96344032 : call_stack.safe_push (call);
3005 96344032 : int len = call_stack.length ();
3006 96344032 : if (len > max_constexpr_depth)
3007 30 : return false;
3008 : return len;
3009 : }
3010 :
3011 : static void
3012 96344030 : pop_cx_call_context (void)
3013 : {
3014 96344030 : ++call_stack_tick;
3015 96344030 : call_stack.pop ();
3016 96344030 : }
3017 :
3018 : vec<tree>
3019 224917 : cx_error_context (void)
3020 : {
3021 224917 : vec<tree> r = vNULL;
3022 224917 : if (call_stack_tick != last_cx_error_tick
3023 224917 : && !call_stack.is_empty ())
3024 : r = call_stack;
3025 224917 : last_cx_error_tick = call_stack_tick;
3026 224917 : return r;
3027 : }
3028 :
3029 : /* E is an operand of a failed assertion, fold it either with or without
3030 : constexpr context. */
3031 :
3032 : static tree
3033 557 : fold_operand (tree e, const constexpr_ctx *ctx)
3034 : {
3035 557 : if (ctx)
3036 : {
3037 32 : bool new_non_constant_p = false, new_overflow_p = false;
3038 32 : tree jmp_target = NULL_TREE;
3039 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3040 : &new_non_constant_p,
3041 : &new_overflow_p, &jmp_target);
3042 : }
3043 : else
3044 525 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3045 557 : return e;
3046 : }
3047 :
3048 : /* If we have a condition in conjunctive normal form (CNF), find the first
3049 : failing clause. In other words, given an expression like
3050 :
3051 : true && true && false && true && false
3052 :
3053 : return the first 'false'. EXPR is the expression. */
3054 :
3055 : static tree
3056 320 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3057 : {
3058 421 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3059 : {
3060 : /* First check the left side... */
3061 186 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3062 186 : if (e == NULL_TREE)
3063 : /* ...if we didn't find a false clause, check the right side. */
3064 101 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3065 : return e;
3066 : }
3067 235 : tree e = contextual_conv_bool (expr, tf_none);
3068 235 : e = fold_operand (e, ctx);
3069 235 : if (integer_zerop (e))
3070 : /* This is the failing clause. */
3071 134 : return expr;
3072 : return NULL_TREE;
3073 : }
3074 :
3075 : /* Wrapper for find_failing_clause_r. */
3076 :
3077 : tree
3078 1583 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3079 : {
3080 1583 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3081 134 : if (tree e = find_failing_clause_r (ctx, expr))
3082 1583 : expr = e;
3083 1583 : return expr;
3084 : }
3085 :
3086 : /* Emit additional diagnostics for failing condition BAD.
3087 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3088 : If SHOW_EXPR_P is true, print the condition (because it was
3089 : instantiation-dependent). */
3090 :
3091 : void
3092 1583 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3093 : const constexpr_ctx *ctx /* = nullptr */)
3094 : {
3095 : /* Nobody wants to see the artificial (bool) cast. */
3096 1583 : bad = tree_strip_nop_conversions (bad);
3097 1583 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3098 3 : bad = TREE_OPERAND (bad, 0);
3099 :
3100 1583 : auto_diagnostic_nesting_level sentinel;
3101 :
3102 : /* Actually explain the failure if this is a concept check or a
3103 : requires-expression. */
3104 1583 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3105 287 : diagnose_constraints (cloc, bad, NULL_TREE);
3106 : /* Similarly if this is a standard trait. */
3107 1296 : else if (maybe_diagnose_standard_trait (cloc, bad))
3108 : ;
3109 968 : else if (COMPARISON_CLASS_P (bad)
3110 968 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
3111 : {
3112 161 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3113 161 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3114 161 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3115 161 : inform (cloc, "the comparison reduces to %qE", cond);
3116 : }
3117 807 : else if (show_expr_p)
3118 565 : inform (cloc, "%qE evaluates to false", bad);
3119 1583 : }
3120 :
3121 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3122 : do it without changing the current evaluation state. If it evaluates to
3123 : false, complain and return false; otherwise, return true. */
3124 :
3125 : static bool
3126 173428 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3127 : location_t loc, bool evaluated,
3128 : bool *non_constant_p, bool *overflow_p)
3129 : {
3130 173428 : if (*non_constant_p)
3131 : return true;
3132 :
3133 173428 : tree eval, jmp_target = NULL_TREE;
3134 173428 : if (!evaluated)
3135 : {
3136 173428 : if (!potential_rvalue_constant_expression (arg))
3137 16 : return true;
3138 :
3139 173412 : constexpr_ctx new_ctx = *ctx;
3140 173412 : new_ctx.quiet = true;
3141 173412 : bool new_non_constant_p = false, new_overflow_p = false;
3142 : /* Avoid modification of existing values. */
3143 173412 : modifiable_tracker ms (new_ctx.global);
3144 173412 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3145 : &new_non_constant_p,
3146 : &new_overflow_p, &jmp_target);
3147 173412 : }
3148 : else
3149 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3150 : non_constant_p,
3151 : overflow_p, &jmp_target);
3152 173412 : if (jmp_target)
3153 : return true;
3154 :
3155 173412 : if (!*non_constant_p && integer_zerop (eval))
3156 : {
3157 42 : if (!ctx->quiet)
3158 : {
3159 : /* See if we can find which clause was failing
3160 : (for logical AND). */
3161 13 : tree bad = find_failing_clause (ctx, arg);
3162 : /* If not, or its location is unusable, fall back to the
3163 : previous location. */
3164 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3165 :
3166 : /* Report the error. */
3167 13 : auto_diagnostic_group d;
3168 13 : error_at (cloc, msg);
3169 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3170 13 : return bad;
3171 13 : }
3172 29 : *non_constant_p = true;
3173 29 : return false;
3174 : }
3175 :
3176 : return true;
3177 : }
3178 :
3179 : /* Evaluate a call T to a GCC internal function when possible and return
3180 : the evaluated result or, under the control of CTX, give an error, set
3181 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3182 :
3183 : static tree
3184 364854 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3185 : value_cat lval,
3186 : bool *non_constant_p, bool *overflow_p,
3187 : tree *jump_target)
3188 : {
3189 364854 : enum tree_code opcode = ERROR_MARK;
3190 :
3191 364854 : switch (CALL_EXPR_IFN (t))
3192 : {
3193 183 : case IFN_UBSAN_NULL:
3194 183 : case IFN_UBSAN_BOUNDS:
3195 183 : case IFN_UBSAN_VPTR:
3196 183 : case IFN_FALLTHROUGH:
3197 183 : return void_node;
3198 :
3199 173428 : case IFN_ASSUME:
3200 173428 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3201 : G_("failed %<assume%> attribute assumption"),
3202 173428 : EXPR_LOCATION (t), /*eval*/false,
3203 : non_constant_p, overflow_p))
3204 : return t;
3205 173399 : return void_node;
3206 :
3207 : case IFN_ADD_OVERFLOW:
3208 : opcode = PLUS_EXPR;
3209 : break;
3210 360 : case IFN_SUB_OVERFLOW:
3211 360 : opcode = MINUS_EXPR;
3212 360 : break;
3213 190310 : case IFN_MUL_OVERFLOW:
3214 190310 : opcode = MULT_EXPR;
3215 190310 : break;
3216 :
3217 74 : case IFN_LAUNDER:
3218 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3219 : vc_prvalue, non_constant_p,
3220 74 : overflow_p, jump_target);
3221 :
3222 0 : case IFN_DEFERRED_INIT:
3223 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3224 :
3225 32 : case IFN_VEC_CONVERT:
3226 32 : {
3227 32 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3228 : vc_prvalue, non_constant_p,
3229 : overflow_p, jump_target);
3230 32 : if (*jump_target)
3231 : return NULL_TREE;
3232 32 : if (TREE_CODE (arg) == VECTOR_CST)
3233 19 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3234 : return r;
3235 : }
3236 : /* FALLTHRU */
3237 :
3238 18 : default:
3239 18 : if (!ctx->quiet)
3240 0 : error_at (cp_expr_loc_or_input_loc (t),
3241 : "call to internal function %qE", t);
3242 18 : *non_constant_p = true;
3243 18 : return t;
3244 : }
3245 :
3246 : /* Evaluate constant arguments using OPCODE and return a complex
3247 : number containing the result and the overflow bit. */
3248 191137 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3249 : non_constant_p, overflow_p,
3250 : jump_target);
3251 191137 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3252 : non_constant_p, overflow_p,
3253 : jump_target);
3254 191137 : if (*jump_target)
3255 : return NULL_TREE;
3256 191137 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3257 : {
3258 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3259 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3260 2 : tree result = fold_binary_loc (loc, opcode, type,
3261 : fold_convert_loc (loc, type, arg0),
3262 : fold_convert_loc (loc, type, arg1));
3263 2 : tree ovf
3264 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3265 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3266 2 : if (TREE_OVERFLOW (result))
3267 0 : TREE_OVERFLOW (result) = 0;
3268 :
3269 2 : return build_complex (TREE_TYPE (t), result, ovf);
3270 : }
3271 :
3272 191135 : *non_constant_p = true;
3273 191135 : return t;
3274 : }
3275 :
3276 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3277 :
3278 : static void
3279 6149539 : clear_no_implicit_zero (tree ctor)
3280 : {
3281 6149539 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3282 : {
3283 1511650 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3284 6705856 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3285 2239850 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3286 394289 : clear_no_implicit_zero (e.value);
3287 : }
3288 6149539 : }
3289 :
3290 : /* Complain about a const object OBJ being modified in a constant expression.
3291 : EXPR is the MODIFY_EXPR expression performing the modification. */
3292 :
3293 : static void
3294 63 : modifying_const_object_error (tree expr, tree obj)
3295 : {
3296 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3297 63 : auto_diagnostic_group d;
3298 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3299 63 : "a constant expression", TREE_OPERAND (expr, 0));
3300 :
3301 : /* Find the underlying object that was declared as const. */
3302 63 : location_t decl_loc = UNKNOWN_LOCATION;
3303 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3304 75 : switch (TREE_CODE (probe))
3305 : {
3306 39 : case BIT_FIELD_REF:
3307 39 : case COMPONENT_REF:
3308 39 : {
3309 39 : tree elt = TREE_OPERAND (probe, 1);
3310 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3311 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3312 39 : probe = TREE_OPERAND (probe, 0);
3313 : }
3314 39 : break;
3315 :
3316 0 : case ARRAY_REF:
3317 0 : case REALPART_EXPR:
3318 0 : case IMAGPART_EXPR:
3319 0 : probe = TREE_OPERAND (probe, 0);
3320 0 : break;
3321 :
3322 36 : default:
3323 36 : decl_loc = location_of (probe);
3324 36 : break;
3325 : }
3326 63 : inform (decl_loc, "originally declared %<const%> here");
3327 63 : }
3328 :
3329 : /* Return true if FNDECL is a replaceable global allocation function that
3330 : should be useable during constant expression evaluation. */
3331 :
3332 : static inline bool
3333 42671774 : cxx_replaceable_global_alloc_fn (tree fndecl)
3334 : {
3335 42671774 : return (cxx_dialect >= cxx20
3336 41109977 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3337 2376532 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3338 45047916 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3339 1204497 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3340 : }
3341 :
3342 : /* Return true if FNDECL is a placement new function that should be
3343 : useable during constant expression evaluation of std::construct_at. */
3344 :
3345 : static inline bool
3346 41309422 : cxx_placement_new_fn (tree fndecl)
3347 : {
3348 41309422 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3349 : }
3350 :
3351 : /* Return true if FNDECL is std::construct_at. */
3352 :
3353 : static inline bool
3354 1421518 : is_std_construct_at (tree fndecl)
3355 : {
3356 1421518 : if (!decl_in_std_namespace_p (fndecl))
3357 : return false;
3358 :
3359 1395357 : tree name = DECL_NAME (fndecl);
3360 1395357 : return name && id_equal (name, "construct_at");
3361 : }
3362 :
3363 : /* Overload for the above taking constexpr_call*. */
3364 :
3365 : static inline bool
3366 1053905 : is_std_construct_at (const constexpr_call *call)
3367 : {
3368 1053905 : return (call
3369 784943 : && call->fundef
3370 1838848 : && is_std_construct_at (call->fundef->decl));
3371 : }
3372 :
3373 : /* True if CTX is an instance of std::NAME class. */
3374 :
3375 : bool
3376 17865732 : is_std_class (tree ctx, const char *name)
3377 : {
3378 17865732 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3379 : return false;
3380 :
3381 17864852 : tree decl = TYPE_MAIN_DECL (ctx);
3382 17864852 : tree dname = DECL_NAME (decl);
3383 17864852 : if (dname == NULL_TREE || !id_equal (dname, name))
3384 : return false;
3385 :
3386 567078 : return decl_in_std_namespace_p (decl);
3387 : }
3388 :
3389 : /* True if CTX is an instance of std::allocator. */
3390 :
3391 : bool
3392 420381 : is_std_allocator (tree ctx)
3393 : {
3394 420381 : return is_std_class (ctx, "allocator");
3395 : }
3396 :
3397 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3398 :
3399 : static bool
3400 435296 : is_std_allocator_allocate (tree fndecl)
3401 : {
3402 435296 : tree name = DECL_NAME (fndecl);
3403 435296 : if (name == NULL_TREE
3404 435296 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3405 : return false;
3406 :
3407 415905 : return is_std_allocator (DECL_CONTEXT (fndecl));
3408 : }
3409 :
3410 : /* Overload for the above taking constexpr_call*. */
3411 :
3412 : static inline bool
3413 264665 : is_std_allocator_allocate (const constexpr_call *call)
3414 : {
3415 264665 : return (call
3416 175072 : && call->fundef
3417 439737 : && is_std_allocator_allocate (call->fundef->decl));
3418 : }
3419 :
3420 : /* Return true if FNDECL is std::source_location::current. */
3421 :
3422 : static inline bool
3423 33452 : is_std_source_location_current (tree fndecl)
3424 : {
3425 33452 : if (!decl_in_std_namespace_p (fndecl))
3426 : return false;
3427 :
3428 24373 : tree name = DECL_NAME (fndecl);
3429 24373 : if (name == NULL_TREE || !id_equal (name, "current"))
3430 : return false;
3431 :
3432 159 : tree ctx = DECL_CONTEXT (fndecl);
3433 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3434 : return false;
3435 :
3436 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3437 159 : return name && id_equal (name, "source_location");
3438 : }
3439 :
3440 : /* Overload for the above taking constexpr_call*. */
3441 :
3442 : static inline bool
3443 41646 : is_std_source_location_current (const constexpr_call *call)
3444 : {
3445 41646 : return (call
3446 33452 : && call->fundef
3447 75098 : && is_std_source_location_current (call->fundef->decl));
3448 : }
3449 :
3450 : /* Return true if FNDECL is __dynamic_cast. */
3451 :
3452 : static inline bool
3453 40895792 : cxx_dynamic_cast_fn_p (tree fndecl)
3454 : {
3455 40895792 : return (cxx_dialect >= cxx20
3456 39333991 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3457 5364 : && CP_DECL_CONTEXT (fndecl) == abi_node
3458 : /* Only consider implementation-detail __dynamic_cast calls that
3459 : correspond to a dynamic_cast, and ignore direct calls to
3460 : abi::__dynamic_cast. */
3461 40901156 : && DECL_ARTIFICIAL (fndecl));
3462 : }
3463 :
3464 : /* Often, we have an expression in the form of address + offset, e.g.
3465 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3466 :
3467 : static tree
3468 4611 : extract_obj_from_addr_offset (tree expr)
3469 : {
3470 4611 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3471 2174 : expr = TREE_OPERAND (expr, 0);
3472 4611 : STRIP_NOPS (expr);
3473 4611 : if (TREE_CODE (expr) == ADDR_EXPR)
3474 4600 : expr = TREE_OPERAND (expr, 0);
3475 4611 : return expr;
3476 : }
3477 :
3478 : /* Given a PATH like
3479 :
3480 : g.D.2181.D.2154.D.2102.D.2093
3481 :
3482 : find a component with type TYPE. Return NULL_TREE if not found, and
3483 : error_mark_node if the component is not accessible. If STOP is non-null,
3484 : this function will return NULL_TREE if STOP is found before TYPE. */
3485 :
3486 : static tree
3487 2219 : get_component_with_type (tree path, tree type, tree stop)
3488 : {
3489 6419 : while (true)
3490 : {
3491 4319 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3492 : /* Found it. */
3493 : return path;
3494 3149 : else if (stop
3495 3149 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3496 : stop)))
3497 : return NULL_TREE;
3498 3104 : else if (TREE_CODE (path) == COMPONENT_REF
3499 3104 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3500 : {
3501 : /* We need to check that the component we're accessing is in fact
3502 : accessible. */
3503 3104 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3504 3104 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3505 1004 : return error_mark_node;
3506 2100 : path = TREE_OPERAND (path, 0);
3507 : }
3508 : else
3509 : return NULL_TREE;
3510 : }
3511 : }
3512 :
3513 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3514 :
3515 : The declaration of __dynamic_cast is:
3516 :
3517 : void* __dynamic_cast (const void* __src_ptr,
3518 : const __class_type_info* __src_type,
3519 : const __class_type_info* __dst_type,
3520 : ptrdiff_t __src2dst);
3521 :
3522 : where src2dst has the following possible values
3523 :
3524 : >-1: src_type is a unique public non-virtual base of dst_type
3525 : dst_ptr + src2dst == src_ptr
3526 : -1: unspecified relationship
3527 : -2: src_type is not a public base of dst_type
3528 : -3: src_type is a multiple public non-virtual base of dst_type */
3529 :
3530 : static tree
3531 2556 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3532 : bool *non_constant_p, bool *overflow_p,
3533 : tree *jump_target)
3534 : {
3535 : /* T will be something like
3536 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3537 : dismantle it. */
3538 2556 : gcc_assert (call_expr_nargs (call) == 4);
3539 2556 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3540 2556 : tree obj = CALL_EXPR_ARG (call, 0);
3541 2556 : tree type = CALL_EXPR_ARG (call, 2);
3542 2556 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3543 2556 : location_t loc = cp_expr_loc_or_input_loc (call);
3544 :
3545 : /* Get the target type of the dynamic_cast. */
3546 2556 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3547 2556 : type = TREE_OPERAND (type, 0);
3548 2556 : type = TREE_TYPE (DECL_NAME (type));
3549 :
3550 : /* TYPE can only be either T* or T&. We can't know which of these it
3551 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3552 : and something like "(T*)(T&)(T*) x" in the second case.
3553 : This is true for the reference cases in C++ < 26 or when exceptions
3554 : aren't enabled, in that case we should diagnose errors. For C++26
3555 : with exceptions we should silently evaluate to null pointer and
3556 : let the callers call __cxa_bad_cast () later to throw an exception. */
3557 2556 : bool fail_for_non_constant_p = false;
3558 11601 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3559 : {
3560 9045 : if (cxx_dialect < cxx26 || !flag_exceptions)
3561 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3562 9045 : obj = TREE_OPERAND (obj, 0);
3563 : }
3564 :
3565 : /* Evaluate the object so that we know its dynamic type. */
3566 2556 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3567 : overflow_p, jump_target);
3568 2556 : if (*non_constant_p)
3569 : return call;
3570 2437 : if (*jump_target)
3571 : return NULL_TREE;
3572 :
3573 : /* For dynamic_cast from classes with virtual bases we can get something
3574 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3575 : d.D.1234 using cxx_fold_indirect_ref. */
3576 2437 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3577 : {
3578 609 : tree objo = obj;
3579 609 : STRIP_NOPS (objo);
3580 609 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3581 : {
3582 576 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3583 : obj, NULL, jump_target);
3584 576 : if (objo)
3585 576 : obj = build_fold_addr_expr (objo);
3586 : }
3587 : }
3588 :
3589 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3590 : but when HINT is > 0, it can also be something like
3591 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3592 2437 : obj = extract_obj_from_addr_offset (obj);
3593 2437 : const tree objtype = TREE_TYPE (obj);
3594 : /* If OBJ doesn't refer to a base field, we're done. */
3595 4800 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3596 2437 : ? TREE_OPERAND (obj, 1) : obj))
3597 2437 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3598 : {
3599 74 : if (fail_for_non_constant_p)
3600 : {
3601 55 : if (!ctx->quiet)
3602 : {
3603 2 : auto_diagnostic_group d;
3604 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3605 2 : inform (loc, "dynamic type %qT of its operand does "
3606 : "not have a base class of type %qT",
3607 : objtype, type);
3608 2 : }
3609 55 : *non_constant_p = true;
3610 : }
3611 74 : return integer_zero_node;
3612 : }
3613 :
3614 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3615 : or in a destructor ... if the operand of the dynamic_cast refers
3616 : to the object under construction or destruction, this object is
3617 : considered to be a most derived object that has the type of the
3618 : constructor or destructor's class. */
3619 2363 : tree vtable = build_vfield_ref (obj, objtype);
3620 2363 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3621 : non_constant_p, overflow_p,
3622 : jump_target);
3623 2363 : if (*non_constant_p)
3624 : return call;
3625 2186 : if (*jump_target)
3626 : return NULL_TREE;
3627 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3628 : so it's possible that we got a null pointer now. */
3629 2186 : if (integer_zerop (vtable))
3630 : {
3631 12 : if (!ctx->quiet)
3632 3 : error_at (loc, "virtual table pointer is used uninitialized");
3633 12 : *non_constant_p = true;
3634 12 : return integer_zero_node;
3635 : }
3636 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3637 2174 : vtable = extract_obj_from_addr_offset (vtable);
3638 2174 : const tree mdtype = DECL_CONTEXT (vtable);
3639 :
3640 : /* Given dynamic_cast<T>(v),
3641 :
3642 : [expr.dynamic.cast] If C is the class type to which T points or refers,
3643 : the runtime check logically executes as follows:
3644 :
3645 : If, in the most derived object pointed (referred) to by v, v points
3646 : (refers) to a public base class subobject of a C object, and if only
3647 : one object of type C is derived from the subobject pointed (referred)
3648 : to by v the result points (refers) to that C object.
3649 :
3650 : In this case, HINT >= 0 or -3. */
3651 2174 : if (hint >= 0 || hint == -3)
3652 : {
3653 : /* Look for a component with type TYPE. */
3654 664 : tree t = get_component_with_type (obj, type, mdtype);
3655 : /* If not accessible, give an error. */
3656 664 : if (t == error_mark_node)
3657 : {
3658 198 : if (fail_for_non_constant_p)
3659 : {
3660 108 : if (!ctx->quiet)
3661 : {
3662 12 : auto_diagnostic_group d;
3663 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3664 12 : inform (loc, "static type %qT of its operand is a "
3665 : "non-public base class of dynamic type %qT",
3666 : objtype, type);
3667 :
3668 12 : }
3669 108 : *non_constant_p = true;
3670 : }
3671 198 : return integer_zero_node;
3672 : }
3673 466 : else if (t)
3674 : /* The result points to the TYPE object. */
3675 421 : return cp_build_addr_expr (t, complain);
3676 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3677 : Fall through to the normal processing. */
3678 : }
3679 :
3680 : /* Otherwise, if v points (refers) to a public base class subobject of the
3681 : most derived object, and the type of the most derived object has a base
3682 : class, of type C, that is unambiguous and public, the result points
3683 : (refers) to the C subobject of the most derived object.
3684 :
3685 : But it can also be an invalid case. */
3686 :
3687 : /* Get the most derived object. */
3688 1555 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3689 1555 : if (obj == error_mark_node)
3690 : {
3691 806 : if (fail_for_non_constant_p)
3692 : {
3693 350 : if (!ctx->quiet)
3694 : {
3695 38 : auto_diagnostic_group d;
3696 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3697 38 : inform (loc, "static type %qT of its operand is a non-public"
3698 : " base class of dynamic type %qT", objtype, mdtype);
3699 38 : }
3700 350 : *non_constant_p = true;
3701 : }
3702 806 : return integer_zero_node;
3703 : }
3704 : else
3705 749 : gcc_assert (obj);
3706 :
3707 : /* Check that the type of the most derived object has a base class
3708 : of type TYPE that is unambiguous and public. */
3709 749 : base_kind b_kind;
3710 749 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3711 749 : if (!binfo || binfo == error_mark_node)
3712 : {
3713 339 : if (fail_for_non_constant_p)
3714 : {
3715 134 : if (!ctx->quiet)
3716 : {
3717 16 : auto_diagnostic_group d;
3718 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3719 16 : if (b_kind == bk_ambig)
3720 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3721 : "type %qT of its operand", type, mdtype);
3722 : else
3723 10 : inform (loc, "dynamic type %qT of its operand does not "
3724 : "have an unambiguous public base class %qT",
3725 : mdtype, type);
3726 16 : }
3727 134 : *non_constant_p = true;
3728 : }
3729 339 : return integer_zero_node;
3730 : }
3731 : /* If so, return the TYPE subobject of the most derived object. */
3732 410 : obj = convert_to_base_statically (obj, binfo);
3733 410 : return cp_build_addr_expr (obj, complain);
3734 : }
3735 :
3736 : /* Data structure used by replace_decl and replace_decl_r. */
3737 :
3738 : struct replace_decl_data
3739 : {
3740 : /* The _DECL we want to replace. */
3741 : tree decl;
3742 : /* The replacement for DECL. */
3743 : tree replacement;
3744 : /* Trees we've visited. */
3745 : hash_set<tree> *pset;
3746 : /* Whether we've performed any replacements. */
3747 : bool changed;
3748 : };
3749 :
3750 : /* Helper function for replace_decl, called through cp_walk_tree. */
3751 :
3752 : static tree
3753 13920577 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3754 : {
3755 13920577 : replace_decl_data *d = (replace_decl_data *) data;
3756 :
3757 : /* We could be replacing
3758 : &<retval>.bar -> &foo.bar
3759 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3760 : on the ADDR_EXPR around it. */
3761 13920577 : if (TREE_CODE (*tp) == ADDR_EXPR)
3762 : {
3763 1395767 : d->pset->add (*tp);
3764 1395767 : auto save_changed = d->changed;
3765 1395767 : d->changed = false;
3766 1395767 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3767 1395767 : if (d->changed)
3768 : {
3769 289 : cxx_mark_addressable (*tp);
3770 289 : recompute_tree_invariant_for_addr_expr (*tp);
3771 : }
3772 : else
3773 1395478 : d->changed = save_changed;
3774 1395767 : *walk_subtrees = 0;
3775 : }
3776 12524810 : else if (*tp == d->decl)
3777 : {
3778 500 : *tp = unshare_expr (d->replacement);
3779 500 : d->changed = true;
3780 500 : *walk_subtrees = 0;
3781 : }
3782 12524310 : else if (TYPE_P (*tp)
3783 12524310 : || d->pset->add (*tp))
3784 2294605 : *walk_subtrees = 0;
3785 :
3786 13920577 : return NULL_TREE;
3787 : }
3788 :
3789 : /* Replace every occurrence of DECL with (an unshared copy of)
3790 : REPLACEMENT within the expression *TP. Returns true iff a
3791 : replacement was performed. */
3792 :
3793 : bool
3794 1818794 : replace_decl (tree *tp, tree decl, tree replacement)
3795 : {
3796 1818794 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3797 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3798 1818794 : hash_set<tree> pset;
3799 1818794 : replace_decl_data data = { decl, replacement, &pset, false };
3800 1818794 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3801 1818794 : return data.changed;
3802 1818794 : }
3803 :
3804 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3805 :
3806 : static tree
3807 30 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3808 : value_cat lval,
3809 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3810 : {
3811 30 : tree function = THUNK_TARGET (thunk_fndecl);
3812 :
3813 30 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3814 : {
3815 13 : if (!ctx->quiet)
3816 : {
3817 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3818 : {
3819 0 : error ("call to non-%<constexpr%> function %qD", function);
3820 0 : explain_invalid_constexpr_fn (function);
3821 : }
3822 : else
3823 : /* virtual_offset is only set for virtual bases, which make the
3824 : class non-literal, so we don't need to handle it here. */
3825 3 : error ("calling constexpr member function %qD through virtual "
3826 : "base subobject", function);
3827 : }
3828 13 : *non_constant_p = true;
3829 13 : return t;
3830 : }
3831 :
3832 17 : tree new_call = copy_node (t);
3833 17 : CALL_EXPR_FN (new_call) = function;
3834 17 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3835 :
3836 17 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3837 :
3838 17 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3839 : {
3840 : /* 'this'-adjusting thunk. */
3841 11 : tree this_arg = CALL_EXPR_ARG (t, 0);
3842 11 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3843 : this_arg, offset);
3844 11 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3845 : }
3846 : else
3847 : /* Return-adjusting thunk. */
3848 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3849 : new_call, offset);
3850 :
3851 17 : return cxx_eval_constant_expression (ctx, new_call, lval,
3852 : non_constant_p, overflow_p,
3853 17 : jump_target);
3854 : }
3855 :
3856 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3857 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3858 : 'tors to detect modifying const objects in a constexpr context. */
3859 :
3860 : static void
3861 12071149 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3862 : bool readonly_p, bool *non_constant_p,
3863 : bool *overflow_p, tree *jump_target)
3864 : {
3865 24142298 : if (CLASS_TYPE_P (TREE_TYPE (object))
3866 24142298 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3867 : {
3868 : /* Subobjects might not be stored in ctx->global->values but we
3869 : can get its CONSTRUCTOR by evaluating *this. */
3870 1302296 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3871 : non_constant_p, overflow_p,
3872 : jump_target);
3873 1302296 : if (!*non_constant_p
3874 13180490 : && !throws (jump_target)
3875 2411637 : && TREE_CODE (e) == CONSTRUCTOR)
3876 1109341 : TREE_READONLY (e) = readonly_p;
3877 : }
3878 12071149 : }
3879 :
3880 : /* Allocate an exception for OBJECT and throw it. */
3881 :
3882 : tree
3883 1469 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
3884 : tree object)
3885 : {
3886 1469 : tree type = TREE_TYPE (object);
3887 : /* This simulates a call to __cxa_allocate_exception. We need
3888 : (struct exception *) &heap -- memory on the heap so that
3889 : it can survive the stack being unwound. */
3890 1469 : tree arr = build_array_of_n_type (type, 1);
3891 1469 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
3892 1469 : DECL_NAME (var) = heap_identifier;
3893 1469 : ctx->global->put_value (var, NULL_TREE);
3894 :
3895 : /* *(struct exception *) &heap = exc{ ... } */
3896 1469 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
3897 1469 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
3898 1469 : bool non_constant_p = false, overflow_p = false;
3899 1469 : tree jump_target = NULL_TREE;
3900 1469 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
3901 : &overflow_p, &jump_target);
3902 1469 : if (non_constant_p)
3903 : {
3904 0 : if (!ctx->quiet)
3905 0 : error_at (loc, "couldn%'t throw %qT", type);
3906 0 : return NULL_TREE;
3907 : }
3908 :
3909 : /* Now we can __cxa_throw. */
3910 1469 : DECL_EXCEPTION_REFCOUNT (var)
3911 1469 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
3912 1469 : ++ctx->global->uncaught_exceptions;
3913 :
3914 1469 : return var;
3915 : }
3916 :
3917 : /* Subroutine of cxx_eval_constant_expression.
3918 : Evaluate the call expression tree T in the context of OLD_CALL expression
3919 : evaluation. */
3920 :
3921 : static tree
3922 199404555 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3923 : value_cat lval,
3924 : bool *non_constant_p, bool *overflow_p,
3925 : tree *jump_target)
3926 : {
3927 199404555 : location_t loc = cp_expr_loc_or_input_loc (t);
3928 199404555 : tree fun = get_function_named_in_call (t);
3929 :
3930 199404555 : if (fun == NULL_TREE)
3931 364854 : return cxx_eval_internal_function (ctx, t, lval,
3932 : non_constant_p, overflow_p,
3933 364854 : jump_target);
3934 :
3935 199039701 : if (TREE_CODE (fun) != FUNCTION_DECL)
3936 : {
3937 : /* Might be a constexpr function pointer. */
3938 476389 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3939 : non_constant_p, overflow_p,
3940 : jump_target);
3941 476389 : if (*jump_target)
3942 : return NULL_TREE;
3943 476389 : STRIP_NOPS (fun);
3944 476389 : if (TREE_CODE (fun) == ADDR_EXPR)
3945 24153 : fun = TREE_OPERAND (fun, 0);
3946 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3947 : indirection, the called expression is a pointer into the
3948 : virtual table which should contain FDESC_EXPR. Extract the
3949 : FUNCTION_DECL from there. */
3950 : else if (TARGET_VTABLE_USES_DESCRIPTORS
3951 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
3952 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
3953 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
3954 : {
3955 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
3956 : if (VAR_P (d)
3957 : && DECL_VTABLE_OR_VTT_P (d)
3958 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
3959 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
3960 : && DECL_INITIAL (d)
3961 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
3962 : {
3963 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
3964 : TYPE_SIZE_UNIT (vtable_entry_type));
3965 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
3966 : if (idx >= 0)
3967 : {
3968 : tree fdesc
3969 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
3970 : if (TREE_CODE (fdesc) == FDESC_EXPR
3971 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
3972 : fun = TREE_OPERAND (fdesc, 0);
3973 : }
3974 : }
3975 : }
3976 : }
3977 199039701 : if (TREE_CODE (fun) != FUNCTION_DECL)
3978 : {
3979 452236 : if (!ctx->quiet && !*non_constant_p)
3980 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
3981 : "function", fun);
3982 452236 : *non_constant_p = true;
3983 452236 : return t;
3984 : }
3985 198587465 : tree orig_fun = fun;
3986 198587465 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
3987 28983349 : fun = DECL_CLONED_FUNCTION (fun);
3988 :
3989 198587465 : if (is_ubsan_builtin_p (fun))
3990 58 : return void_node;
3991 :
3992 198587407 : if (fndecl_built_in_p (fun))
3993 16863745 : return cxx_eval_builtin_function_call (ctx, t, fun,
3994 : lval, non_constant_p, overflow_p,
3995 16863744 : jump_target);
3996 181723662 : if (DECL_THUNK_P (fun))
3997 30 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
3998 30 : jump_target);
3999 181723632 : if (metafunction_p (fun))
4000 : {
4001 : /* To be able to evaluate a metafunction, we may have to instantiate
4002 : constexpr functions. If we're not allowed to instantiate, leave
4003 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4004 : otherwise we might fold those prematurely. See
4005 : g++.dg/reflect/p2996-17.C. */
4006 33925 : if (uid_sensitive_constexpr_evaluation_p ()
4007 33667 : || ctx->manifestly_const_eval == mce_unknown)
4008 : {
4009 8694 : *non_constant_p = true;
4010 8694 : return t;
4011 : }
4012 25231 : ctx->global->metafns_called = true;
4013 25231 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4014 : jump_target);
4015 25231 : if (*jump_target)
4016 : return NULL_TREE;
4017 23756 : if (*non_constant_p)
4018 : return t;
4019 23670 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4020 : non_constant_p, overflow_p,
4021 : jump_target);
4022 23670 : if (*jump_target)
4023 : return NULL_TREE;
4024 23670 : if (*non_constant_p)
4025 : return t;
4026 : return e;
4027 : }
4028 181689707 : bool non_constexpr_call = false;
4029 181689707 : if (!maybe_constexpr_fn (fun))
4030 : {
4031 939225 : if (TREE_CODE (t) == CALL_EXPR
4032 928051 : && cxx_replaceable_global_alloc_fn (fun)
4033 1514267 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4034 175462 : || is_std_allocator_allocate (ctx->call)))
4035 : {
4036 493643 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4037 493643 : const int nargs = call_expr_nargs (t);
4038 493643 : tree arg0 = NULL_TREE;
4039 788651 : for (int i = 0; i < nargs; ++i)
4040 : {
4041 494504 : tree arg = CALL_EXPR_ARG (t, i);
4042 494504 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4043 : non_constant_p, overflow_p,
4044 : jump_target);
4045 494504 : if (*jump_target)
4046 : return NULL_TREE;
4047 : /* Deleting a non-constant pointer has a better error message
4048 : below. */
4049 494496 : if (new_op_p || i != 0)
4050 447247 : VERIFY_CONSTANT (arg);
4051 247759 : if (i == 0)
4052 : arg0 = arg;
4053 : }
4054 294147 : gcc_assert (arg0);
4055 294147 : if (new_op_p)
4056 : {
4057 246898 : if (!tree_fits_uhwi_p (arg0))
4058 : {
4059 : /* We should not get here; the VERIFY_CONSTANT above
4060 : should have already caught it. */
4061 0 : gcc_checking_assert (false);
4062 : if (!ctx->quiet)
4063 : error_at (loc, "cannot allocate array: size not constant");
4064 : *non_constant_p = true;
4065 : return t;
4066 : }
4067 493796 : tree type = build_array_type_nelts (char_type_node,
4068 246898 : tree_to_uhwi (arg0));
4069 246898 : tree var = build_decl (loc, VAR_DECL,
4070 246898 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4071 : & OVL_OP_FLAG_VEC)
4072 : ? heap_vec_uninit_identifier
4073 : : heap_uninit_identifier,
4074 246898 : type);
4075 246898 : DECL_ARTIFICIAL (var) = 1;
4076 246898 : ctx->global->heap_vars.safe_push (var);
4077 246898 : ctx->global->put_value (var, NULL_TREE);
4078 246898 : return fold_convert (ptr_type_node, build_address (var));
4079 : }
4080 : else
4081 : {
4082 47249 : STRIP_NOPS (arg0);
4083 47249 : if (TREE_CODE (arg0) == ADDR_EXPR
4084 47249 : && VAR_P (TREE_OPERAND (arg0, 0)))
4085 : {
4086 47249 : tree var = TREE_OPERAND (arg0, 0);
4087 47249 : if (DECL_NAME (var) == heap_uninit_identifier
4088 47249 : || DECL_NAME (var) == heap_identifier)
4089 : {
4090 47083 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4091 : & OVL_OP_FLAG_VEC)
4092 : {
4093 9 : if (!ctx->quiet)
4094 : {
4095 3 : auto_diagnostic_group d;
4096 3 : error_at (loc, "array deallocation of object "
4097 : "allocated with non-array "
4098 : "allocation");
4099 3 : inform (DECL_SOURCE_LOCATION (var),
4100 : "allocation performed here");
4101 3 : }
4102 9 : *non_constant_p = true;
4103 9 : return t;
4104 : }
4105 47074 : DECL_NAME (var) = heap_deleted_identifier;
4106 47074 : ctx->global->destroy_value (var);
4107 47074 : ctx->global->heap_dealloc_count++;
4108 47074 : return void_node;
4109 : }
4110 166 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4111 166 : || DECL_NAME (var) == heap_vec_identifier)
4112 : {
4113 142 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4114 : & OVL_OP_FLAG_VEC) == 0)
4115 : {
4116 9 : if (!ctx->quiet)
4117 : {
4118 3 : auto_diagnostic_group d;
4119 3 : error_at (loc, "non-array deallocation of "
4120 : "object allocated with array "
4121 : "allocation");
4122 3 : inform (DECL_SOURCE_LOCATION (var),
4123 : "allocation performed here");
4124 3 : }
4125 9 : *non_constant_p = true;
4126 9 : return t;
4127 : }
4128 133 : DECL_NAME (var) = heap_deleted_identifier;
4129 133 : ctx->global->destroy_value (var);
4130 133 : ctx->global->heap_dealloc_count++;
4131 133 : return void_node;
4132 : }
4133 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4134 : {
4135 15 : if (!ctx->quiet)
4136 6 : error_at (loc, "deallocation of already deallocated "
4137 : "storage");
4138 15 : *non_constant_p = true;
4139 15 : return t;
4140 : }
4141 : }
4142 9 : if (!ctx->quiet)
4143 3 : error_at (loc, "deallocation of storage that was "
4144 : "not previously allocated");
4145 9 : *non_constant_p = true;
4146 9 : return t;
4147 : }
4148 : }
4149 : /* Allow placement new in std::construct_at, just return the second
4150 : argument. */
4151 445582 : if (TREE_CODE (t) == CALL_EXPR
4152 434408 : && cxx_placement_new_fn (fun)
4153 737327 : && is_std_construct_at (ctx->call))
4154 : {
4155 30942 : const int nargs = call_expr_nargs (t);
4156 30942 : tree arg1 = NULL_TREE;
4157 92826 : for (int i = 0; i < nargs; ++i)
4158 : {
4159 61884 : tree arg = CALL_EXPR_ARG (t, i);
4160 61884 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4161 : non_constant_p, overflow_p,
4162 : jump_target);
4163 61884 : if (*jump_target)
4164 : return NULL_TREE;
4165 61884 : if (i == 1)
4166 : arg1 = arg;
4167 : else
4168 61884 : VERIFY_CONSTANT (arg);
4169 : }
4170 30942 : gcc_assert (arg1);
4171 : return arg1;
4172 : }
4173 414640 : else if (cxx_dynamic_cast_fn_p (fun))
4174 2556 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4175 2556 : jump_target);
4176 412084 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4177 24835 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4178 : non_constant_p, overflow_p,
4179 24835 : jump_target);
4180 :
4181 : /* Calls to non-constexpr functions can be diagnosed right away
4182 : before C++26, though in C++26 evaluation of the arguments might
4183 : throw and if caught it could be still constant expression.
4184 : So for C++26 this is diagnosed only after
4185 : cxx_bind_parameters_in_call. */
4186 387249 : if (cxx_dialect >= cxx26)
4187 : non_constexpr_call = true;
4188 : else
4189 : {
4190 341029 : if (!ctx->quiet)
4191 : {
4192 188 : if (!lambda_static_thunk_p (fun))
4193 188 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4194 188 : explain_invalid_constexpr_fn (fun);
4195 : }
4196 341029 : *non_constant_p = true;
4197 341029 : return t;
4198 : }
4199 : }
4200 :
4201 180796702 : constexpr_ctx new_ctx = *ctx;
4202 180796702 : ctx = &new_ctx;
4203 208597571 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4204 185312195 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4205 : {
4206 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4207 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4208 1530 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4209 1530 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4210 1530 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4211 1530 : ctx->global->put_value (new_ctx.object, ctor);
4212 : }
4213 : /* An immediate invocation is manifestly constant evaluated including the
4214 : arguments of the call, so use mce_true even for the argument
4215 : evaluation. */
4216 361593404 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4217 4185808 : new_ctx.manifestly_const_eval = mce_true;
4218 :
4219 : /* We used to shortcut trivial constructor/op= here, but nowadays
4220 : we can only get a trivial function here with -fno-elide-constructors. */
4221 180796702 : gcc_checking_assert (!trivial_fn_p (fun)
4222 : || !flag_elide_constructors
4223 : /* Or it's a call from maybe_thunk_body (111075). */
4224 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4225 : : AGGR_INIT_FROM_THUNK_P (t))
4226 : /* We don't elide constructors when processing
4227 : a noexcept-expression. */
4228 : || cp_noexcept_operand);
4229 :
4230 180796702 : bool non_constant_args = false;
4231 180796702 : constexpr_call new_call;
4232 180796702 : new_call.bindings
4233 180796702 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4234 : overflow_p, &non_constant_args,
4235 : jump_target);
4236 :
4237 : /* We build up the bindings list before we know whether we already have this
4238 : call cached. If we don't end up saving these bindings, ggc_free them when
4239 : this function exits. */
4240 180796702 : class free_bindings
4241 : {
4242 : tree *bindings;
4243 : public:
4244 180796702 : free_bindings (tree &b): bindings (&b) { }
4245 180796700 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4246 5075617 : void preserve () { bindings = NULL; }
4247 180796702 : } fb (new_call.bindings);
4248 :
4249 180796702 : if (*jump_target)
4250 : return NULL_TREE;
4251 180796692 : if (*non_constant_p)
4252 : return t;
4253 97012305 : if (non_constexpr_call)
4254 : {
4255 4108 : if (!ctx->quiet)
4256 : {
4257 193 : if (!lambda_static_thunk_p (fun))
4258 193 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4259 193 : explain_invalid_constexpr_fn (fun);
4260 : }
4261 4108 : *non_constant_p = true;
4262 4108 : return t;
4263 : }
4264 :
4265 : /* We can't defer instantiating the function any longer. */
4266 97008197 : if (!DECL_INITIAL (fun)
4267 3548345 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4268 97008197 : && !uid_sensitive_constexpr_evaluation_p ())
4269 : {
4270 3201284 : location_t save_loc = input_location;
4271 3201284 : input_location = loc;
4272 3201284 : ++function_depth;
4273 3201284 : if (ctx->manifestly_const_eval == mce_true)
4274 392143 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4275 3201284 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4276 3201277 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4277 : else
4278 7 : synthesize_method (fun);
4279 3201284 : --function_depth;
4280 3201284 : input_location = save_loc;
4281 : }
4282 :
4283 : /* If in direct recursive call, optimize definition search. */
4284 97008197 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4285 27549 : new_call.fundef = ctx->call->fundef;
4286 : else
4287 : {
4288 96980648 : new_call.fundef = retrieve_constexpr_fundef (fun);
4289 96980648 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4290 96384321 : || new_call.fundef->result == error_mark_node
4291 96316486 : || fun == current_function_decl)
4292 : {
4293 664165 : if (!ctx->quiet)
4294 : {
4295 : /* We need to check for current_function_decl here in case we're
4296 : being called during cp_fold_function, because at that point
4297 : DECL_INITIAL is set properly and we have a fundef but we
4298 : haven't lowered invisirefs yet (c++/70344). */
4299 168 : if (DECL_INITIAL (fun) == error_mark_node
4300 168 : || fun == current_function_decl)
4301 15 : error_at (loc, "%qD called in a constant expression before its "
4302 : "definition is complete", fun);
4303 153 : else if (DECL_INITIAL (fun))
4304 : {
4305 : /* The definition of fun was somehow unsuitable. But pretend
4306 : that lambda static thunks don't exist. */
4307 116 : if (!lambda_static_thunk_p (fun))
4308 112 : error_at (loc, "%qD called in a constant expression", fun);
4309 116 : explain_invalid_constexpr_fn (fun);
4310 : }
4311 : else
4312 37 : error_at (loc, "%qD used before its definition", fun);
4313 : }
4314 664165 : *non_constant_p = true;
4315 664165 : return t;
4316 : }
4317 : }
4318 :
4319 : /* Don't complain about problems evaluating an ill-formed function. */
4320 96344032 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4321 96344032 : if (f->language->erroneous)
4322 664 : new_ctx.quiet = true;
4323 :
4324 96344032 : int depth_ok = push_cx_call_context (t);
4325 :
4326 : /* Remember the object we are constructing or destructing. */
4327 96344032 : tree new_obj = NULL_TREE;
4328 192688064 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4329 : {
4330 : /* In a cdtor, it should be the first `this' argument.
4331 : At this point it has already been evaluated in the call
4332 : to cxx_bind_parameters_in_call. */
4333 14952320 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4334 14952320 : bool empty_base = false;
4335 14952320 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4336 : &empty_base, jump_target);
4337 14952320 : if (*jump_target)
4338 0 : return NULL_TREE;
4339 : /* If we're initializing an empty class, don't set constness, because
4340 : cxx_fold_indirect_ref will return the wrong object to set constness
4341 : of. */
4342 14952320 : if (empty_base)
4343 : new_obj = NULL_TREE;
4344 6099764 : else if (ctx->call && ctx->call->fundef
4345 24310929 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4346 : {
4347 3561508 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4348 3561508 : STRIP_NOPS (cur_obj);
4349 3561508 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4350 3560336 : cur_obj = TREE_OPERAND (cur_obj, 0);
4351 3561508 : if (new_obj == cur_obj)
4352 : /* We're calling the target constructor of a delegating
4353 : constructor, or accessing a base subobject through a
4354 : NOP_EXPR as part of a call to a base constructor, so
4355 : there is no new (sub)object. */
4356 2881157 : new_obj = NULL_TREE;
4357 : }
4358 : }
4359 :
4360 96344032 : tree result = NULL_TREE;
4361 :
4362 96344032 : constexpr_call *entry = NULL;
4363 96344032 : if (depth_ok && !non_constant_args && ctx->strict)
4364 : {
4365 32625105 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4366 32625105 : new_call.hash
4367 32625105 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4368 :
4369 : /* If we have seen this call before, we are done. */
4370 32625105 : maybe_initialize_constexpr_call_table ();
4371 32625105 : bool insert = depth_ok < constexpr_cache_depth;
4372 32625105 : constexpr_call **slot
4373 39065927 : = constexpr_call_table->find_slot (&new_call,
4374 : insert ? INSERT : NO_INSERT);
4375 32625105 : entry = slot ? *slot : NULL;
4376 31620643 : if (entry == NULL)
4377 : {
4378 : /* Only cache up to constexpr_cache_depth to limit memory use. */
4379 6080079 : if (insert)
4380 : {
4381 : /* We need to keep a pointer to the entry, not just the slot, as
4382 : the slot can move during evaluation of the body. */
4383 5075617 : *slot = entry = ggc_alloc<constexpr_call> ();
4384 5075617 : *entry = new_call;
4385 5075617 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4386 5075617 : fb.preserve ();
4387 : }
4388 : }
4389 : /* Calls that are in progress have their result set to unknown_type_node,
4390 : so that we can detect circular dependencies. Now that we only cache
4391 : up to constexpr_cache_depth this won't catch circular dependencies that
4392 : start deeper, but they'll hit the recursion or ops limit. */
4393 26545026 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4394 : {
4395 6 : if (!ctx->quiet)
4396 0 : error ("call has circular dependency");
4397 6 : *non_constant_p = true;
4398 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4399 : }
4400 : else
4401 26545020 : result = entry->result (ctx->manifestly_const_eval);
4402 : }
4403 :
4404 96344032 : if (!depth_ok)
4405 : {
4406 30 : if (!ctx->quiet)
4407 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4408 : "%<-fconstexpr-depth=%> to increase the maximum)",
4409 : max_constexpr_depth);
4410 30 : *non_constant_p = true;
4411 30 : result = error_mark_node;
4412 : }
4413 : else
4414 : {
4415 96344002 : bool cacheable = !!entry;
4416 96344002 : if (result && result != error_mark_node)
4417 : /* OK */;
4418 76699559 : else if (!DECL_SAVED_TREE (fun))
4419 : {
4420 : /* When at_eof >= 3, cgraph has started throwing away
4421 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4422 : late code generation for VEC_INIT_EXPR, which needs to be
4423 : completely reconsidered. */
4424 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4425 0 : *non_constant_p = true;
4426 : }
4427 76699559 : else if (tree copy = get_fundef_copy (new_call.fundef))
4428 : {
4429 76699559 : tree body, parms, res;
4430 76699559 : releasing_vec ctors;
4431 :
4432 : /* Reuse or create a new unshared copy of this function's body. */
4433 76699559 : body = TREE_PURPOSE (copy);
4434 76699559 : parms = TREE_VALUE (copy);
4435 76699559 : res = TREE_TYPE (copy);
4436 :
4437 : /* Associate the bindings with the remapped parms. */
4438 76699559 : tree bound = new_call.bindings;
4439 76699559 : tree remapped = parms;
4440 178483908 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4441 : {
4442 101784349 : tree arg = TREE_VEC_ELT (bound, i);
4443 101784349 : if (entry)
4444 : {
4445 : /* Unshare args going into the hash table to separate them
4446 : from the caller's context, for better GC and to avoid
4447 : problems with verify_gimple. */
4448 3663174 : arg = unshare_expr_without_location (arg);
4449 3663174 : TREE_VEC_ELT (bound, i) = arg;
4450 :
4451 : /* And then unshare again so the callee doesn't change the
4452 : argument values in the hash table. XXX Could we unshare
4453 : lazily in cxx_eval_store_expression? */
4454 3663174 : arg = unshare_constructor (arg);
4455 3663174 : if (TREE_CODE (arg) == CONSTRUCTOR)
4456 866766 : vec_safe_push (ctors, arg);
4457 : }
4458 101784349 : ctx->global->put_value (remapped, arg);
4459 101784349 : remapped = DECL_CHAIN (remapped);
4460 : }
4461 76699559 : if (remapped)
4462 : {
4463 : /* We shouldn't have any parms without args, but fail gracefully
4464 : in error recovery. */
4465 6 : gcc_checking_assert (seen_error ());
4466 6 : *non_constant_p = true;
4467 : }
4468 : /* Add the RESULT_DECL to the values map, too. */
4469 76699559 : gcc_assert (!DECL_BY_REFERENCE (res));
4470 76699559 : ctx->global->put_value (res, NULL_TREE);
4471 :
4472 : /* Remember the current call we're evaluating. */
4473 76699559 : constexpr_ctx call_ctx = *ctx;
4474 76699559 : call_ctx.call = &new_call;
4475 76699559 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4476 76699559 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4477 76699559 : bool save_metafns_called = ctx->global->metafns_called;
4478 :
4479 : /* Make sure we fold std::is_constant_evaluated to true in an
4480 : immediate function. */
4481 153399118 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4482 2330593 : call_ctx.manifestly_const_eval = mce_true;
4483 :
4484 : /* If this is a constexpr destructor, the object's const and volatile
4485 : semantics are no longer in effect; see [class.dtor]p5. */
4486 88770708 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4487 992865 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4488 : non_constant_p, overflow_p, jump_target);
4489 :
4490 : /* If this is a constructor, we are beginning the lifetime of the
4491 : object we are initializing. */
4492 76699559 : if (new_obj
4493 24142298 : && DECL_CONSTRUCTOR_P (fun)
4494 11078284 : && TREE_CODE (new_obj) == COMPONENT_REF
4495 79644985 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4496 : {
4497 4586 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4498 4586 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4499 4586 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4500 : new_obj, ctor);
4501 4586 : cxx_eval_constant_expression (ctx, activate,
4502 : lval, non_constant_p, overflow_p,
4503 : jump_target);
4504 4586 : ggc_free (activate);
4505 4586 : if (*jump_target)
4506 0 : return NULL_TREE;
4507 : }
4508 :
4509 76699559 : ctx->global->metafns_called = false;
4510 :
4511 76699559 : tree jmp_target = NULL_TREE;
4512 76699559 : cxx_eval_constant_expression (&call_ctx, body,
4513 : vc_discard, non_constant_p, overflow_p,
4514 : &jmp_target);
4515 :
4516 76699557 : if (!*non_constant_p && throws (&jmp_target))
4517 : {
4518 1277 : result = NULL_TREE;
4519 1277 : cacheable = false;
4520 1277 : *jump_target = jmp_target;
4521 : }
4522 153396560 : else if (DECL_CONSTRUCTOR_P (fun))
4523 : /* This can be null for a subobject constructor call, in
4524 : which case what we care about is the initialization
4525 : side-effects rather than the value. We could get at the
4526 : value by evaluating *this, but we don't bother; there's
4527 : no need to put such a call in the hash table. */
4528 13876919 : result = lval ? ctx->object : ctx->ctor;
4529 62821361 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4530 4429584 : result = void_node;
4531 : else
4532 : {
4533 58391777 : result = ctx->global->get_value (res);
4534 23331697 : if (result == NULL_TREE && !*non_constant_p
4535 58391983 : && !DECL_DESTRUCTOR_P (fun))
4536 : {
4537 103 : if (!ctx->quiet)
4538 6 : error ("%<constexpr%> call flows off the end "
4539 : "of the function");
4540 103 : *non_constant_p = true;
4541 : }
4542 : }
4543 :
4544 76699557 : if (ctx->global->metafns_called)
4545 29155 : cacheable = false;
4546 76699557 : ctx->global->metafns_called |= save_metafns_called;
4547 :
4548 : /* At this point, the object's constructor will have run, so
4549 : the object is no longer under construction, and its possible
4550 : 'const' semantics now apply. Make a note of this fact by
4551 : marking the CONSTRUCTOR TREE_READONLY. */
4552 88770706 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4553 11078284 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4554 : non_constant_p, overflow_p, jump_target);
4555 :
4556 : /* Remove the parms/result from the values map. */
4557 76699557 : destroy_value_checked (ctx, res, non_constant_p);
4558 178483911 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4559 101784354 : destroy_value_checked (ctx, parm, non_constant_p);
4560 :
4561 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4562 77566323 : while (!ctors->is_empty ())
4563 : {
4564 866766 : tree c = ctors->pop ();
4565 866766 : if (c != result)
4566 866766 : free_constructor (c);
4567 : }
4568 :
4569 : /* Make the unshared function copy we used available for re-use. */
4570 76699557 : save_fundef_copy (fun, copy);
4571 :
4572 : /* If the call allocated some heap object that hasn't been
4573 : deallocated during the call, or if it deallocated some heap
4574 : object it has not allocated, the call isn't really stateless
4575 : for the constexpr evaluation and should not be cached.
4576 : It is fine if the call allocates something and deallocates it
4577 : too. */
4578 76699557 : if (cacheable
4579 88673603 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4580 11971431 : || (save_heap_dealloc_count
4581 11971431 : != ctx->global->heap_dealloc_count)))
4582 : {
4583 2615 : tree heap_var;
4584 2615 : unsigned int i;
4585 2615 : if ((ctx->global->heap_vars.length ()
4586 2615 : - ctx->global->heap_dealloc_count)
4587 2615 : != save_heap_alloc_count - save_heap_dealloc_count)
4588 : cacheable = false;
4589 : else
4590 85100 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4591 : save_heap_alloc_count)
4592 83533 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4593 : {
4594 : cacheable = false;
4595 : break;
4596 : }
4597 : }
4598 :
4599 : /* Rewrite all occurrences of the function's RESULT_DECL with the
4600 : current object under construction. */
4601 76699557 : if (!*non_constant_p
4602 46452391 : && ctx->object
4603 13768533 : && CLASS_TYPE_P (TREE_TYPE (res))
4604 79358704 : && !is_empty_class (TREE_TYPE (res)))
4605 : {
4606 1818674 : if (!same_type_ignoring_top_level_qualifiers_p
4607 1818674 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4608 0 : *non_constant_p = true;
4609 1818674 : else if (replace_decl (&result, res, ctx->object))
4610 : {
4611 236 : cacheable = false;
4612 236 : result = cxx_eval_constant_expression (ctx, result, lval,
4613 : non_constant_p,
4614 : overflow_p,
4615 : jump_target);
4616 236 : if (*jump_target)
4617 : {
4618 0 : cacheable = false;
4619 0 : result = NULL_TREE;
4620 : }
4621 : }
4622 : }
4623 :
4624 : /* Only cache a permitted result of a constant expression. */
4625 76699321 : if (cacheable && !reduced_constant_expression_p (result))
4626 : cacheable = false;
4627 76699557 : }
4628 : else
4629 : /* Couldn't get a function copy to evaluate. */
4630 0 : *non_constant_p = true;
4631 :
4632 96344000 : if (result == error_mark_node)
4633 0 : *non_constant_p = true;
4634 96344000 : if (*non_constant_p || *overflow_p)
4635 30247322 : result = error_mark_node;
4636 66096678 : else if (!result)
4637 4485496 : result = void_node;
4638 96344000 : if (entry)
4639 : {
4640 63241284 : entry->result (ctx->manifestly_const_eval)
4641 31620642 : = cacheable ? result : error_mark_node;
4642 :
4643 31620642 : if (result != error_mark_node
4644 24821837 : && ctx->manifestly_const_eval == mce_unknown)
4645 : {
4646 : /* Evaluation succeeded and was independent of whether we're in a
4647 : manifestly constant-evaluated context, so we can also reuse
4648 : this result when evaluating this call with a fixed context. */
4649 2366748 : if (!entry->result (mce_true))
4650 871427 : entry->result (mce_true) = entry->result (mce_unknown);
4651 2366748 : if (!entry->result (mce_false))
4652 835660 : entry->result (mce_false) = entry->result (mce_unknown);
4653 : }
4654 : }
4655 : }
4656 :
4657 : /* The result of a constexpr function must be completely initialized.
4658 :
4659 : However, in C++20, a constexpr constructor doesn't necessarily have
4660 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4661 : in order to detect reading an unitialized object in constexpr instead
4662 : of value-initializing it. (reduced_constant_expression_p is expected to
4663 : take care of clearing the flag.) */
4664 96344030 : if (TREE_CODE (result) == CONSTRUCTOR
4665 96344030 : && (cxx_dialect < cxx20
4666 22619988 : || !DECL_CONSTRUCTOR_P (fun)))
4667 5755250 : clear_no_implicit_zero (result);
4668 :
4669 96344030 : pop_cx_call_context ();
4670 96344030 : return result;
4671 180796700 : }
4672 :
4673 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4674 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4675 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4676 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4677 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4678 :
4679 : bool
4680 1195119420 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4681 : {
4682 1195119420 : if (t == NULL_TREE)
4683 : return false;
4684 :
4685 1188323698 : switch (TREE_CODE (t))
4686 : {
4687 : case PTRMEM_CST:
4688 : case REFLECT_EXPR:
4689 : /* Even if we can't lower this yet, it's constant. */
4690 : return true;
4691 :
4692 : case OMP_DECLARE_MAPPER:
4693 : return true;
4694 :
4695 68301587 : case CONSTRUCTOR:
4696 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4697 68301587 : tree field;
4698 68301587 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4699 : /* A constant vector would be folded to VECTOR_CST.
4700 : A CONSTRUCTOR of scalar type means uninitialized. */
4701 : return false;
4702 68285738 : if (CONSTRUCTOR_NO_CLEARING (t))
4703 : {
4704 4666036 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4705 : {
4706 : /* There must be a valid constant initializer at every array
4707 : index. */
4708 1931 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4709 1931 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4710 1931 : tree cursor = min;
4711 27382 : for (auto &e: CONSTRUCTOR_ELTS (t))
4712 : {
4713 21699 : if (!reduced_constant_expression_p (e.value))
4714 : return false;
4715 21649 : if (array_index_cmp (cursor, e.index) != 0)
4716 : return false;
4717 21589 : if (TREE_CODE (e.index) == RANGE_EXPR)
4718 0 : cursor = TREE_OPERAND (e.index, 1);
4719 21589 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4720 0 : cursor
4721 0 : = int_const_binop (PLUS_EXPR, cursor,
4722 0 : size_int (RAW_DATA_LENGTH (e.value)));
4723 : else
4724 21589 : cursor = int_const_binop (PLUS_EXPR, cursor,
4725 21589 : size_one_node);
4726 : }
4727 1821 : if (find_array_ctor_elt (t, max) == -1)
4728 : return false;
4729 1733 : goto ok;
4730 : }
4731 4664105 : else if (cxx_dialect >= cxx20
4732 4664105 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4733 : {
4734 9125535 : if (CONSTRUCTOR_NELTS (t) == 0)
4735 : /* An initialized union has a constructor element. */
4736 : return false;
4737 : /* And it only initializes one member. */
4738 : field = NULL_TREE;
4739 : }
4740 : else
4741 4662137 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4742 : }
4743 : else
4744 : field = NULL_TREE;
4745 597705100 : for (auto &e: CONSTRUCTOR_ELTS (t))
4746 : {
4747 : /* If VAL is null, we're in the middle of initializing this
4748 : element. */
4749 456957677 : if (!reduced_constant_expression_p (e.value,
4750 456957677 : (e.index
4751 456957659 : && (TREE_CODE (e.index)
4752 : == FIELD_DECL))
4753 70810644 : ? DECL_SIZE (e.index)
4754 : : NULL_TREE))
4755 : return false;
4756 : /* We want to remove initializers for empty fields in a struct to
4757 : avoid confusing output_constructor. */
4758 455272334 : if (is_empty_field (e.index)
4759 455272334 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4760 : return false;
4761 : /* Check for non-empty fields between initialized fields when
4762 : CONSTRUCTOR_NO_CLEARING. */
4763 455012402 : for (; field && e.index != field;
4764 362261 : field = next_subobject_field (DECL_CHAIN (field)))
4765 362563 : if (!is_really_empty_class (TREE_TYPE (field),
4766 : /*ignore_vptr*/false))
4767 : return false;
4768 454649839 : if (field)
4769 5208167 : field = next_subobject_field (DECL_CHAIN (field));
4770 : }
4771 : /* There could be a non-empty field at the end. */
4772 66308734 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4773 338947 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4774 : {
4775 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4776 : the parent FIELD_DECL (if any) for classes with virtual
4777 : bases. */
4778 4214 : if (cxx_dialect >= cxx26
4779 2206 : && sz
4780 4595 : && tree_int_cst_le (sz, bit_position (field)))
4781 : break;
4782 3960 : return false;
4783 : }
4784 65969787 : ok:
4785 65971774 : if (CONSTRUCTOR_NO_CLEARING (t))
4786 : /* All the fields are initialized. */
4787 4195762 : CONSTRUCTOR_NO_CLEARING (t) = false;
4788 : return true;
4789 :
4790 1119833605 : default:
4791 : /* FIXME are we calling this too much? */
4792 1119833605 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4793 : }
4794 : }
4795 :
4796 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4797 : why and suggest what could be done about it. */
4798 :
4799 : static tree
4800 989 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4801 : {
4802 989 : bool ref_p = false;
4803 :
4804 : /* No need to look into types or unevaluated operands. */
4805 989 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4806 : {
4807 0 : *walk_subtrees = false;
4808 0 : return NULL_TREE;
4809 : }
4810 :
4811 989 : switch (TREE_CODE (*tp))
4812 : {
4813 81 : CASE_CONVERT:
4814 81 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4815 : break;
4816 62 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4817 62 : *tp = TREE_OPERAND (*tp, 0);
4818 194 : gcc_fallthrough ();
4819 194 : case ADDR_EXPR:
4820 194 : {
4821 194 : tree op = TREE_OPERAND (*tp, 0);
4822 194 : if (VAR_P (op)
4823 111 : && DECL_DECLARED_CONSTEXPR_P (op)
4824 39 : && !TREE_STATIC (op)
4825 : /* ??? We should also say something about temporaries. */
4826 230 : && !DECL_ARTIFICIAL (op))
4827 : {
4828 33 : if (ref_p)
4829 12 : inform (location_of (*tp), "reference to %qD is not a constant "
4830 : "expression", op);
4831 : else
4832 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4833 : "expression", op);
4834 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4835 33 : rich_location richloc (line_table, op_loc);
4836 33 : richloc.add_fixit_insert_before (op_loc, "static ");
4837 33 : inform (&richloc,
4838 : "address of non-static constexpr variable %qD may differ on "
4839 : "each invocation of the enclosing function; add %<static%> "
4840 : "to give it a constant address", op);
4841 33 : }
4842 : break;
4843 : }
4844 : default:
4845 : break;
4846 : }
4847 :
4848 : return NULL_TREE;
4849 : }
4850 :
4851 : /* Some expressions may have constant operands but are not constant
4852 : themselves, such as 1/0. Call this function to check for that
4853 : condition.
4854 :
4855 : We only call this in places that require an arithmetic constant, not in
4856 : places where we might have a non-constant expression that can be a
4857 : component of a constant expression, such as the address of a constexpr
4858 : variable that might be dereferenced later. */
4859 :
4860 : static bool
4861 630773949 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4862 : bool *overflow_p)
4863 : {
4864 611467559 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4865 635966052 : && t != void_node)
4866 : {
4867 5191905 : if (!allow_non_constant)
4868 : {
4869 260 : auto_diagnostic_group d;
4870 373 : error_at (cp_expr_loc_or_input_loc (t),
4871 : "%q+E is not a constant expression", t);
4872 260 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4873 : nullptr);
4874 260 : }
4875 5191905 : *non_constant_p = true;
4876 : }
4877 630773949 : if (TREE_OVERFLOW_P (t))
4878 : {
4879 678 : if (!allow_non_constant)
4880 : {
4881 155 : permerror (input_location, "overflow in constant expression");
4882 : /* If we're being permissive (and are in an enforcing
4883 : context), ignore the overflow. */
4884 155 : if (flag_permissive)
4885 75 : return *non_constant_p;
4886 : }
4887 603 : *overflow_p = true;
4888 : }
4889 630773874 : return *non_constant_p;
4890 : }
4891 :
4892 : /* Check whether the shift operation with code CODE and type TYPE on LHS
4893 : and RHS is undefined. If it is, give an error with an explanation,
4894 : and return true; return false otherwise. */
4895 :
4896 : static bool
4897 59229352 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4898 : enum tree_code code, tree type, tree lhs, tree rhs)
4899 : {
4900 59229352 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4901 3238001 : || TREE_CODE (lhs) != INTEGER_CST
4902 3238001 : || TREE_CODE (rhs) != INTEGER_CST)
4903 : return false;
4904 :
4905 3238001 : tree lhstype = TREE_TYPE (lhs);
4906 3238001 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4907 :
4908 : /* [expr.shift] The behavior is undefined if the right operand
4909 : is negative, or greater than or equal to the length in bits
4910 : of the promoted left operand. */
4911 3238001 : if (tree_int_cst_sgn (rhs) == -1)
4912 : {
4913 126 : if (!ctx->quiet)
4914 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4915 : build2_loc (loc, code, type, lhs, rhs));
4916 126 : return (!flag_permissive || ctx->quiet);
4917 : }
4918 3237875 : if (compare_tree_int (rhs, uprec) >= 0)
4919 : {
4920 200 : if (!ctx->quiet)
4921 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4922 : "than or equal to the precision %wu of the left operand",
4923 : build2_loc (loc, code, type, lhs, rhs), uprec);
4924 200 : return (!flag_permissive || ctx->quiet);
4925 : }
4926 :
4927 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4928 : if E1 has a signed type and non-negative value, and E1x2^E2 is
4929 : representable in the corresponding unsigned type of the result type,
4930 : then that value, converted to the result type, is the resulting value;
4931 : otherwise, the behavior is undefined.
4932 : For C++20:
4933 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4934 : 2^N, where N is the range exponent of the type of the result. */
4935 3237675 : if (code == LSHIFT_EXPR
4936 3065545 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4937 2056578 : && cxx_dialect >= cxx11
4938 5275540 : && cxx_dialect < cxx20)
4939 : {
4940 53478 : if (tree_int_cst_sgn (lhs) == -1)
4941 : {
4942 74 : if (!ctx->quiet)
4943 18 : permerror (loc,
4944 : "left operand of shift expression %q+E is negative",
4945 : build2_loc (loc, code, type, lhs, rhs));
4946 74 : return (!flag_permissive || ctx->quiet);
4947 : }
4948 : /* For signed x << y the following:
4949 : (unsigned) x >> ((prec (lhs) - 1) - y)
4950 : if > 1, is undefined. The right-hand side of this formula
4951 : is the highest bit of the LHS that can be set (starting from 0),
4952 : so that the shift doesn't overflow. We then right-shift the LHS
4953 : to see whether any other bit is set making the original shift
4954 : undefined -- the result is not representable in the corresponding
4955 : unsigned type. */
4956 53404 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
4957 53404 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
4958 53404 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
4959 53404 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
4960 53404 : if (tree_int_cst_lt (integer_one_node, t))
4961 : {
4962 41 : if (!ctx->quiet)
4963 7 : permerror (loc, "shift expression %q+E overflows",
4964 : build2_loc (loc, code, type, lhs, rhs));
4965 41 : return (!flag_permissive || ctx->quiet);
4966 : }
4967 : }
4968 : return false;
4969 : }
4970 :
4971 : /* Subroutine of cxx_eval_constant_expression.
4972 : Attempt to reduce the unary expression tree T to a compile time value.
4973 : If successful, return the value. Otherwise issue a diagnostic
4974 : and return error_mark_node. */
4975 :
4976 : static tree
4977 23999293 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
4978 : bool /*lval*/,
4979 : bool *non_constant_p, bool *overflow_p,
4980 : tree *jump_target)
4981 : {
4982 23999293 : tree r;
4983 23999293 : tree orig_arg = TREE_OPERAND (t, 0);
4984 23999293 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
4985 : non_constant_p, overflow_p,
4986 : jump_target);
4987 23999292 : if (*jump_target)
4988 : return NULL_TREE;
4989 23999281 : VERIFY_CONSTANT (arg);
4990 18830556 : location_t loc = EXPR_LOCATION (t);
4991 18830556 : enum tree_code code = TREE_CODE (t);
4992 18830556 : tree type = TREE_TYPE (t);
4993 18830556 : r = fold_unary_loc (loc, code, type, arg);
4994 18830556 : if (r == NULL_TREE)
4995 : {
4996 17 : if (arg == orig_arg)
4997 : r = t;
4998 : else
4999 13 : r = build1_loc (loc, code, type, arg);
5000 : }
5001 18830556 : VERIFY_CONSTANT (r);
5002 : return r;
5003 : }
5004 :
5005 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5006 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5007 : generic folding should be used. */
5008 :
5009 : static tree
5010 8391652 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5011 : tree lhs, tree rhs, bool *non_constant_p,
5012 : bool *overflow_p, tree *jump_target)
5013 : {
5014 8391652 : STRIP_NOPS (lhs);
5015 8391652 : if (TREE_CODE (lhs) != ADDR_EXPR)
5016 : return NULL_TREE;
5017 :
5018 7469666 : lhs = TREE_OPERAND (lhs, 0);
5019 :
5020 : /* &A[i] p+ j => &A[i + j] */
5021 7469666 : if (TREE_CODE (lhs) == ARRAY_REF
5022 94218 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5023 94218 : && TREE_CODE (rhs) == INTEGER_CST
5024 94218 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5025 7563884 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5026 : {
5027 94218 : tree orig_type = TREE_TYPE (t);
5028 94218 : location_t loc = EXPR_LOCATION (t);
5029 94218 : tree type = TREE_TYPE (lhs);
5030 :
5031 94218 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5032 94218 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5033 94218 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5034 : non_constant_p, overflow_p,
5035 : jump_target);
5036 94218 : if (*non_constant_p)
5037 : return NULL_TREE;
5038 94206 : if (*jump_target)
5039 : return NULL_TREE;
5040 : /* Don't fold an out-of-bound access. */
5041 94206 : if (!tree_int_cst_le (t, nelts))
5042 : return NULL_TREE;
5043 94206 : rhs = cp_fold_convert (ssizetype, rhs);
5044 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5045 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5046 94206 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5047 94206 : rhs, TYPE_SIZE_UNIT (type))))
5048 : return NULL_TREE;
5049 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5050 : as signed. */
5051 94174 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5052 94174 : TYPE_SIZE_UNIT (type));
5053 94174 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5054 94174 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5055 : t, NULL_TREE, NULL_TREE);
5056 94174 : t = cp_build_addr_expr (t, tf_warning_or_error);
5057 94174 : t = cp_fold_convert (orig_type, t);
5058 94174 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5059 : non_constant_p, overflow_p,
5060 94174 : jump_target);
5061 : }
5062 :
5063 : return NULL_TREE;
5064 : }
5065 :
5066 : /* Try to fold expressions like
5067 : (struct S *) (&a[0].D.2378 + 12)
5068 : into
5069 : &MEM <struct T> [(void *)&a + 12B]
5070 : This is something normally done by gimple_fold_stmt_to_constant_1
5071 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5072 : dereference the address because some details are lost.
5073 : For pointer comparisons we want such folding though so that
5074 : match.pd address_compare optimization works. */
5075 :
5076 : static tree
5077 7242778 : cxx_maybe_fold_addr_pointer_plus (tree t)
5078 : {
5079 14485559 : while (CONVERT_EXPR_P (t)
5080 7707794 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5081 465013 : t = TREE_OPERAND (t, 0);
5082 7242778 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5083 : return NULL_TREE;
5084 6119317 : tree op0 = TREE_OPERAND (t, 0);
5085 6119317 : tree op1 = TREE_OPERAND (t, 1);
5086 6119317 : if (TREE_CODE (op1) != INTEGER_CST)
5087 : return NULL_TREE;
5088 6119317 : while (CONVERT_EXPR_P (op0)
5089 12236603 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5090 6117286 : op0 = TREE_OPERAND (op0, 0);
5091 6119317 : if (TREE_CODE (op0) != ADDR_EXPR)
5092 : return NULL_TREE;
5093 6119317 : op1 = fold_convert (ptr_type_node, op1);
5094 6119317 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5095 6119317 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5096 : }
5097 :
5098 : /* Subroutine of cxx_eval_constant_expression.
5099 : Like cxx_eval_unary_expression, except for binary expressions. */
5100 :
5101 : static tree
5102 92687443 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5103 : value_cat lval,
5104 : bool *non_constant_p, bool *overflow_p,
5105 : tree *jump_target)
5106 : {
5107 92687443 : tree r = NULL_TREE;
5108 92687443 : tree orig_lhs = TREE_OPERAND (t, 0);
5109 92687443 : tree orig_rhs = TREE_OPERAND (t, 1);
5110 92687443 : tree lhs, rhs;
5111 92687443 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5112 : non_constant_p, overflow_p,
5113 : jump_target);
5114 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5115 : subtraction. */
5116 92687442 : if (*non_constant_p)
5117 : return t;
5118 67039938 : if (*jump_target)
5119 : return NULL_TREE;
5120 67039887 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5121 : non_constant_p, overflow_p,
5122 : jump_target);
5123 67039887 : if (*non_constant_p)
5124 : return t;
5125 65755146 : if (*jump_target)
5126 : return NULL_TREE;
5127 :
5128 65755141 : location_t loc = EXPR_LOCATION (t);
5129 65755141 : enum tree_code code = TREE_CODE (t);
5130 65755141 : tree type = TREE_TYPE (t);
5131 :
5132 65755141 : if (code == EQ_EXPR || code == NE_EXPR)
5133 : {
5134 10296824 : bool is_code_eq = (code == EQ_EXPR);
5135 :
5136 10296824 : if (TREE_CODE (lhs) == PTRMEM_CST
5137 192 : && TREE_CODE (rhs) == PTRMEM_CST)
5138 : {
5139 61 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5140 61 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5141 61 : bool eq;
5142 61 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5143 61 : && TREE_CODE (lmem) == FIELD_DECL
5144 61 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5145 88 : && same_type_p (DECL_CONTEXT (lmem),
5146 : DECL_CONTEXT (rmem)))
5147 : /* If both refer to (possibly different) members of the same union
5148 : (12.3), they compare equal. */
5149 : eq = true;
5150 : else
5151 34 : eq = cp_tree_equal (lhs, rhs);
5152 61 : r = constant_boolean_node (eq == is_code_eq, type);
5153 61 : }
5154 10296763 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5155 10296632 : || TREE_CODE (rhs) == PTRMEM_CST)
5156 10296763 : && (null_member_pointer_value_p (lhs)
5157 131 : || null_member_pointer_value_p (rhs)))
5158 131 : r = constant_boolean_node (!is_code_eq, type);
5159 10296632 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5160 0 : lhs = cplus_expand_constant (lhs);
5161 10296632 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5162 0 : rhs = cplus_expand_constant (rhs);
5163 10296632 : else if (REFLECT_EXPR_P (lhs) && REFLECT_EXPR_P (rhs))
5164 : {
5165 1744 : const bool eq = compare_reflections (lhs, rhs);
5166 1744 : r = constant_boolean_node (eq == is_code_eq, type);
5167 : }
5168 : }
5169 0 : if (r == NULL_TREE
5170 65753205 : && TREE_CODE_CLASS (code) == tcc_comparison
5171 25434417 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5172 : {
5173 3621389 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5174 2986758 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5175 3621389 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5176 3132559 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5177 : }
5178 8391807 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5179 74146948 : && integer_zerop (lhs) && !integer_zerop (rhs))
5180 : {
5181 155 : if (!ctx->quiet)
5182 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5183 155 : *non_constant_p = true;
5184 155 : return t;
5185 : }
5186 65754986 : else if (code == POINTER_PLUS_EXPR)
5187 : {
5188 8391652 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5189 : overflow_p, jump_target);
5190 8391652 : if (*jump_target)
5191 : return NULL_TREE;
5192 : }
5193 57363334 : else if (code == SPACESHIP_EXPR)
5194 : {
5195 25402 : r = genericize_spaceship (loc, type, lhs, rhs);
5196 25402 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5197 25402 : overflow_p, jump_target);
5198 : }
5199 :
5200 65729584 : if (r == NULL_TREE)
5201 : {
5202 65633474 : if (ctx->manifestly_const_eval == mce_true
5203 45111769 : && (flag_constexpr_fp_except
5204 45111766 : || TREE_CODE (type) != REAL_TYPE))
5205 : {
5206 45063714 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5207 45063714 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5208 45063714 : }
5209 : else
5210 20569760 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5211 : }
5212 :
5213 65633474 : if (r == NULL_TREE
5214 6500330 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5215 100 : && TREE_CODE (lhs) == INTEGER_CST
5216 98 : && TREE_CODE (rhs) == INTEGER_CST
5217 72229914 : && wi::neg_p (wi::to_wide (rhs)))
5218 : {
5219 : /* For diagnostics and -fpermissive emulate previous behavior of
5220 : handling shifts by negative amount. */
5221 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5222 98 : if (nrhs)
5223 119 : r = fold_binary_loc (loc,
5224 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5225 : type, lhs, nrhs);
5226 : }
5227 :
5228 65729584 : if (r == NULL_TREE)
5229 : {
5230 6500232 : if (lhs == orig_lhs && rhs == orig_rhs)
5231 : r = t;
5232 : else
5233 2511353 : r = build2_loc (loc, code, type, lhs, rhs);
5234 : }
5235 59229352 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5236 433 : *non_constant_p = true;
5237 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5238 : a local array in a constexpr function. */
5239 65729584 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5240 53211223 : if (!ptr)
5241 53211223 : VERIFY_CONSTANT (r);
5242 : return r;
5243 : }
5244 :
5245 : /* Subroutine of cxx_eval_constant_expression.
5246 : Attempt to evaluate condition expressions. */
5247 :
5248 : static tree
5249 26231958 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5250 : value_cat lval,
5251 : bool *non_constant_p, bool *overflow_p,
5252 : tree *jump_target)
5253 : {
5254 26231958 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5255 : vc_prvalue,
5256 : non_constant_p, overflow_p,
5257 : jump_target);
5258 26231958 : if (*jump_target)
5259 : return NULL_TREE;
5260 26231939 : VERIFY_CONSTANT (val);
5261 20456099 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5262 : {
5263 : /* Evaluate the condition as if it was
5264 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5265 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5266 : without manifestly_const_eval even expressions or parts thereof which
5267 : will later be manifestly const_eval evaluated), otherwise fold it to
5268 : true. */
5269 652112 : if (ctx->manifestly_const_eval == mce_unknown)
5270 : {
5271 644239 : *non_constant_p = true;
5272 644239 : return t;
5273 : }
5274 7873 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5275 : boolean_type_node);
5276 : }
5277 : /* Don't VERIFY_CONSTANT the other operands. */
5278 19811860 : const bool zero_p = integer_zerop (val);
5279 19811860 : if (zero_p)
5280 13253888 : val = TREE_OPERAND (t, 2);
5281 : else
5282 6557972 : val = TREE_OPERAND (t, 1);
5283 19811860 : if (TREE_CODE (t) == IF_STMT && !val)
5284 7193234 : val = void_node;
5285 :
5286 : /* P2564: If we aren't in immediate function context (including a manifestly
5287 : constant-evaluated expression), check any uses of immediate functions in
5288 : the arm we're discarding. But don't do this inside a call; we already
5289 : checked when parsing the function. */
5290 19811860 : if (ctx->manifestly_const_eval != mce_true
5291 5161125 : && !in_immediate_context ()
5292 4733691 : && !ctx->call
5293 20583303 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5294 771443 : ctx->manifestly_const_eval))
5295 : {
5296 7 : *non_constant_p = true;
5297 7 : return t;
5298 : }
5299 :
5300 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5301 : serve as the initializer for the same object as the outer TARGET_EXPR,
5302 : as in
5303 : A a = true ? A{} : A{};
5304 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5305 19811853 : if (TREE_CODE (val) == TARGET_EXPR)
5306 2250 : val = TARGET_EXPR_INITIAL (val);
5307 19811853 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5308 19811853 : overflow_p, jump_target);
5309 : }
5310 :
5311 : /* Subroutine of cxx_eval_constant_expression.
5312 : Attempt to evaluate vector condition expressions. Unlike
5313 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5314 : ternary arithmetics operation, where all 3 arguments have to be
5315 : evaluated as constants and then folding computes the result from
5316 : them. */
5317 :
5318 : static tree
5319 1073 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5320 : bool *non_constant_p, bool *overflow_p,
5321 : tree *jump_target)
5322 : {
5323 1073 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5324 : vc_prvalue,
5325 : non_constant_p, overflow_p,
5326 : jump_target);
5327 1073 : if (*jump_target)
5328 : return NULL_TREE;
5329 1073 : VERIFY_CONSTANT (arg1);
5330 657 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5331 : vc_prvalue,
5332 : non_constant_p, overflow_p,
5333 : jump_target);
5334 657 : if (*jump_target)
5335 : return NULL_TREE;
5336 657 : VERIFY_CONSTANT (arg2);
5337 657 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5338 : vc_prvalue,
5339 : non_constant_p, overflow_p,
5340 : jump_target);
5341 657 : if (*jump_target)
5342 : return NULL_TREE;
5343 657 : VERIFY_CONSTANT (arg3);
5344 657 : location_t loc = EXPR_LOCATION (t);
5345 657 : tree type = TREE_TYPE (t);
5346 657 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5347 657 : if (r == NULL_TREE)
5348 : {
5349 0 : if (arg1 == TREE_OPERAND (t, 0)
5350 0 : && arg2 == TREE_OPERAND (t, 1)
5351 0 : && arg3 == TREE_OPERAND (t, 2))
5352 : r = t;
5353 : else
5354 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5355 : }
5356 657 : VERIFY_CONSTANT (r);
5357 : return r;
5358 : }
5359 :
5360 : /* Returns less than, equal to, or greater than zero if KEY is found to be
5361 : less than, to match, or to be greater than the constructor_elt's INDEX. */
5362 :
5363 : static int
5364 392927 : array_index_cmp (tree key, tree index)
5365 : {
5366 392927 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5367 :
5368 392927 : switch (TREE_CODE (index))
5369 : {
5370 390042 : case INTEGER_CST:
5371 390042 : return tree_int_cst_compare (key, index);
5372 2885 : case RANGE_EXPR:
5373 2885 : {
5374 2885 : tree lo = TREE_OPERAND (index, 0);
5375 2885 : tree hi = TREE_OPERAND (index, 1);
5376 2885 : if (tree_int_cst_lt (key, lo))
5377 : return -1;
5378 2593 : else if (tree_int_cst_lt (hi, key))
5379 : return 1;
5380 : else
5381 2593 : return 0;
5382 : }
5383 0 : default:
5384 0 : gcc_unreachable ();
5385 : }
5386 : }
5387 :
5388 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5389 : relative index OFF. */
5390 :
5391 : static tree
5392 29073 : raw_data_cst_elt (tree raw_data, unsigned int off)
5393 : {
5394 29073 : return build_int_cst (TREE_TYPE (raw_data),
5395 29073 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5396 58146 : ? (HOST_WIDE_INT)
5397 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
5398 : : (HOST_WIDE_INT)
5399 0 : RAW_DATA_SCHAR_ELT (raw_data, off));
5400 : }
5401 :
5402 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5403 : if none. If INSERT is true, insert a matching element rather than fail. */
5404 :
5405 : static HOST_WIDE_INT
5406 7278781 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5407 : {
5408 7278781 : if (tree_int_cst_sgn (dindex) < 0)
5409 : return -1;
5410 :
5411 7278781 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5412 7278781 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5413 7278781 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5414 :
5415 10583067 : unsigned HOST_WIDE_INT end = len;
5416 10583067 : unsigned HOST_WIDE_INT begin = 0;
5417 :
5418 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5419 : that the same is true of the other elements and index directly. */
5420 6791168 : if (end > 0)
5421 : {
5422 6677374 : tree cindex = (*elts)[end - 1].index;
5423 6677374 : if (cindex == NULL_TREE)
5424 : {
5425 : /* Verify that if the last index is missing, all indexes
5426 : are missing and there is no RAW_DATA_CST. */
5427 19500 : if (flag_checking)
5428 204078 : for (unsigned int j = 0; j < len - 1; ++j)
5429 184578 : gcc_assert ((*elts)[j].index == NULL_TREE
5430 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5431 19500 : if (i < end)
5432 19500 : return i;
5433 : else
5434 : {
5435 0 : begin = end;
5436 0 : if (i == end)
5437 : /* If the element is to be added right at the end,
5438 : make sure it is added with cleared index too. */
5439 3791899 : dindex = NULL_TREE;
5440 0 : else if (insert)
5441 : /* Otherwise, in order not to break the assumption
5442 : that CONSTRUCTOR either has all indexes or none,
5443 : we need to add indexes to all elements. */
5444 0 : for (unsigned int j = 0; j < len; ++j)
5445 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5446 : }
5447 : }
5448 6657874 : else if (TREE_CODE (cindex) == INTEGER_CST
5449 6657874 : && compare_tree_int (cindex, end - 1) == 0)
5450 : {
5451 6425511 : tree value = (*elts)[end - 1].value;
5452 6425511 : if (i < end)
5453 : {
5454 3467472 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5455 : begin = end - 1;
5456 : else
5457 3467382 : return i;
5458 : }
5459 2958039 : else if (TREE_CODE (value) == RAW_DATA_CST
5460 2990195 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5461 32156 : + RAW_DATA_LENGTH (value)))
5462 16078 : begin = end - 1;
5463 : else
5464 2941961 : begin = end;
5465 : }
5466 : }
5467 :
5468 : /* Otherwise, find a matching index by means of a binary search. */
5469 4002170 : while (begin != end)
5470 : {
5471 371275 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5472 371275 : constructor_elt &elt = (*elts)[middle];
5473 371275 : tree idx = elt.index;
5474 :
5475 371275 : int cmp = array_index_cmp (dindex, idx);
5476 371275 : if (cmp > 0
5477 62464 : && TREE_CODE (elt.value) == RAW_DATA_CST
5478 454127 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5479 82852 : + RAW_DATA_LENGTH (elt.value)))
5480 28939 : cmp = 0;
5481 371275 : if (cmp < 0)
5482 : end = middle;
5483 194529 : else if (cmp > 0)
5484 33525 : begin = middle + 1;
5485 : else
5486 : {
5487 161004 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5488 : {
5489 : /* We need to split the RAW_DATA_CST elt. */
5490 122 : constructor_elt e;
5491 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5492 244 : unsigned int off = (wi::to_offset (dindex)
5493 244 : - wi::to_offset (idx)).to_uhwi ();
5494 122 : tree value = elt.value;
5495 122 : unsigned int len = RAW_DATA_LENGTH (value);
5496 122 : if (off > 1 && len >= off + 3)
5497 22 : value = copy_node (elt.value);
5498 122 : if (off)
5499 : {
5500 33 : if (off > 1)
5501 33 : RAW_DATA_LENGTH (elt.value) = off;
5502 : else
5503 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5504 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5505 : build_int_cst (TREE_TYPE (elt.index),
5506 : off));
5507 33 : e.value = NULL_TREE;
5508 33 : ++middle;
5509 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5510 : }
5511 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5512 122 : if (len >= off + 2)
5513 : {
5514 111 : e.index = (*elts)[middle].index;
5515 111 : e.index = size_binop (PLUS_EXPR, e.index,
5516 : build_one_cst (TREE_TYPE (e.index)));
5517 111 : if (len >= off + 3)
5518 : {
5519 111 : RAW_DATA_LENGTH (value) -= off + 1;
5520 111 : RAW_DATA_POINTER (value) += off + 1;
5521 111 : e.value = value;
5522 : }
5523 : else
5524 0 : e.value = raw_data_cst_elt (value, off + 1);
5525 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5526 : }
5527 122 : return middle;
5528 : }
5529 31669 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5530 : {
5531 : /* We need to split the range. */
5532 345 : constructor_elt e;
5533 345 : tree lo = TREE_OPERAND (idx, 0);
5534 345 : tree hi = TREE_OPERAND (idx, 1);
5535 345 : tree value = elt.value;
5536 345 : dindex = fold_convert (sizetype, dindex);
5537 345 : if (tree_int_cst_lt (lo, dindex))
5538 : {
5539 : /* There are still some lower elts; shorten the range. */
5540 170 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5541 85 : size_one_node);
5542 85 : if (tree_int_cst_equal (lo, new_hi))
5543 : /* Only one element left, no longer a range. */
5544 33 : elt.index = lo;
5545 : else
5546 52 : TREE_OPERAND (idx, 1) = new_hi;
5547 : /* Append the element we want to insert. */
5548 85 : ++middle;
5549 85 : e.index = dindex;
5550 85 : e.value = unshare_constructor (value);
5551 85 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5552 : }
5553 : else
5554 : /* No lower elts, the range elt is now ours. */
5555 260 : elt.index = dindex;
5556 :
5557 345 : if (tree_int_cst_lt (dindex, hi))
5558 : {
5559 : /* There are still some higher elts; append a range. */
5560 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5561 239 : size_one_node);
5562 239 : if (tree_int_cst_equal (new_lo, hi))
5563 98 : e.index = hi;
5564 : else
5565 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5566 239 : e.value = unshare_constructor (value);
5567 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5568 : }
5569 : }
5570 160882 : return middle;
5571 : }
5572 : }
5573 :
5574 3630895 : if (insert)
5575 : {
5576 3525212 : constructor_elt e = { dindex, NULL_TREE };
5577 3525212 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5578 3525212 : return end;
5579 : }
5580 :
5581 : return -1;
5582 : }
5583 :
5584 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5585 : matching constructor_elt exists, then add one to CTOR.
5586 :
5587 : As an optimization, if POS_HINT is non-negative then it is used as a guess
5588 : for the (integer) index of the matching constructor_elt within CTOR.
5589 :
5590 : If POS_HINT is -2, it means do not insert. */
5591 :
5592 : static constructor_elt *
5593 33353031 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5594 : {
5595 : /* Check the hint first. */
5596 3210716 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5597 36563747 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5598 : return CONSTRUCTOR_ELT (ctor, pos_hint);
5599 :
5600 30142326 : bool insertp = (pos_hint != -2);
5601 30142326 : tree type = TREE_TYPE (ctor);
5602 30142326 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5603 : {
5604 128 : gcc_assert (insertp);
5605 128 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5606 128 : return &CONSTRUCTOR_ELTS (ctor)->last();
5607 : }
5608 30142198 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5609 : {
5610 4689866 : if (TREE_CODE (index) == RANGE_EXPR)
5611 : {
5612 : /* Support for RANGE_EXPR index lookups is currently limited to
5613 : accessing an existing element via POS_HINT, or appending a new
5614 : element to the end of CTOR. ??? Support for other access
5615 : patterns may also be needed. */
5616 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5617 3 : if (vec_safe_length (elts))
5618 : {
5619 3 : tree lo = TREE_OPERAND (index, 0);
5620 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5621 : }
5622 3 : gcc_assert (insertp);
5623 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5624 3 : return &elts->last();
5625 : }
5626 :
5627 4689863 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5628 4689863 : if (i < 0)
5629 : {
5630 915 : gcc_assert (!insertp);
5631 : return nullptr;
5632 : }
5633 4688948 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5634 4688948 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5635 : {
5636 : /* Split a range even if we aren't inserting new entries. */
5637 0 : gcc_assert (!insertp);
5638 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5639 0 : gcc_assert (i >= 0);
5640 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5641 : }
5642 4688948 : gcc_assert (cep->index == NULL_TREE
5643 : || TREE_CODE (cep->index) != RANGE_EXPR);
5644 : return cep;
5645 : }
5646 : else
5647 : {
5648 25452332 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5649 : && (same_type_ignoring_top_level_qualifiers_p
5650 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5651 :
5652 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5653 : Usually we meet initializers in that order, but it is
5654 : possible for base types to be placed not in program
5655 : order. */
5656 25452332 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5657 25452332 : unsigned HOST_WIDE_INT idx = 0;
5658 25452332 : constructor_elt *cep = NULL;
5659 :
5660 : /* Check if we're changing the active member of a union. */
5661 453720 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5662 25593304 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5663 2569 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5664 : /* If the bit offset of INDEX is larger than that of the last
5665 : constructor_elt, then we can just immediately append a new
5666 : constructor_elt to the end of CTOR. */
5667 25449763 : else if (CONSTRUCTOR_NELTS (ctor)
5668 31008795 : && tree_int_cst_compare (bit_position (index),
5669 30112284 : bit_position (CONSTRUCTOR_ELTS (ctor)
5670 15056142 : ->last().index)) > 0)
5671 : {
5672 5850366 : idx = CONSTRUCTOR_NELTS (ctor);
5673 5850366 : goto insert;
5674 : }
5675 :
5676 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5677 : appropriately. */
5678 :
5679 21211718 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5680 1609752 : idx++, fields = DECL_CHAIN (fields))
5681 : {
5682 10815528 : if (index == cep->index)
5683 9183646 : goto found;
5684 :
5685 : /* The field we're initializing must be on the field
5686 : list. Look to see if it is present before the
5687 : field the current ELT initializes. */
5688 17290173 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5689 15680421 : if (index == fields)
5690 22130 : goto insert;
5691 : }
5692 : /* We fell off the end of the CONSTRUCTOR, so insert a new
5693 : entry at the end. */
5694 :
5695 16268686 : insert:
5696 16268686 : if (!insertp)
5697 : return nullptr;
5698 :
5699 16268683 : {
5700 16268683 : constructor_elt ce = { index, NULL_TREE };
5701 :
5702 16268683 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5703 16268683 : cep = CONSTRUCTOR_ELT (ctor, idx);
5704 : }
5705 25452329 : found:;
5706 :
5707 25452329 : return cep;
5708 : }
5709 : }
5710 :
5711 : /* Under the control of CTX, issue a detailed diagnostic for
5712 : an out-of-bounds subscript INDEX into the expression ARRAY. */
5713 :
5714 : static void
5715 566 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5716 : {
5717 566 : if (!ctx->quiet)
5718 : {
5719 116 : tree arraytype = TREE_TYPE (array);
5720 :
5721 : /* Convert the unsigned array subscript to a signed integer to avoid
5722 : printing huge numbers for small negative values. */
5723 116 : tree sidx = fold_convert (ssizetype, index);
5724 116 : STRIP_ANY_LOCATION_WRAPPER (array);
5725 116 : if (DECL_P (array))
5726 : {
5727 75 : auto_diagnostic_group d;
5728 75 : if (TYPE_DOMAIN (arraytype))
5729 69 : error_at (loc, "array subscript value %qE is outside the bounds "
5730 : "of array %qD of type %qT", sidx, array, arraytype);
5731 : else
5732 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5733 : "type %qT with unknown bounds", sidx, array, arraytype);
5734 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5735 75 : }
5736 41 : else if (TYPE_DOMAIN (arraytype))
5737 38 : error_at (loc, "array subscript value %qE is outside the bounds "
5738 : "of array type %qT", sidx, arraytype);
5739 : else
5740 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5741 : "with unknown bounds", sidx, arraytype);
5742 : }
5743 566 : }
5744 :
5745 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5746 : a VECTOR_TYPE). */
5747 :
5748 : static tree
5749 14732188 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5750 : bool *non_constant_p, bool *overflow_p,
5751 : tree *jump_target)
5752 : {
5753 14732188 : tree nelts;
5754 14732188 : if (TREE_CODE (type) == ARRAY_TYPE)
5755 : {
5756 14732188 : if (TYPE_DOMAIN (type))
5757 14731940 : nelts = array_type_nelts_top (type);
5758 : else
5759 248 : nelts = size_zero_node;
5760 : }
5761 0 : else if (VECTOR_TYPE_P (type))
5762 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5763 : else
5764 0 : gcc_unreachable ();
5765 :
5766 : /* For VLAs, the number of elements won't be an integer constant. */
5767 14732188 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5768 : non_constant_p, overflow_p,
5769 : jump_target);
5770 14732188 : return nelts;
5771 : }
5772 :
5773 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5774 : STRING_CST STRING. */
5775 :
5776 : static tree
5777 2686157 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5778 : {
5779 2686157 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5780 2686157 : tree r;
5781 :
5782 2686157 : if (chars_per_elt == 1)
5783 2004772 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5784 : else
5785 : {
5786 681385 : const unsigned char *ptr
5787 681385 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5788 681385 : + index * chars_per_elt);
5789 681385 : r = native_interpret_expr (type, ptr, chars_per_elt);
5790 : }
5791 2686157 : return r;
5792 : }
5793 :
5794 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5795 : subscript, diagnose any problems with it, and return the result. */
5796 :
5797 : static tree
5798 14916342 : eval_and_check_array_index (const constexpr_ctx *ctx,
5799 : tree t, bool allow_one_past,
5800 : bool *non_constant_p, bool *overflow_p,
5801 : tree *jump_target)
5802 : {
5803 14916342 : location_t loc = cp_expr_loc_or_input_loc (t);
5804 14916342 : tree ary = TREE_OPERAND (t, 0);
5805 14916342 : t = TREE_OPERAND (t, 1);
5806 14916342 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5807 : non_constant_p, overflow_p,
5808 : jump_target);
5809 14916342 : if (*jump_target)
5810 : return NULL_TREE;
5811 14916342 : VERIFY_CONSTANT (index);
5812 :
5813 14731654 : if (!tree_fits_shwi_p (index)
5814 14731654 : || tree_int_cst_sgn (index) < 0)
5815 : {
5816 108 : diag_array_subscript (loc, ctx, ary, index);
5817 108 : *non_constant_p = true;
5818 108 : return t;
5819 : }
5820 :
5821 14731546 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5822 : overflow_p, jump_target);
5823 14731546 : if (*jump_target)
5824 : return NULL_TREE;
5825 14731546 : VERIFY_CONSTANT (nelts);
5826 14731483 : if (allow_one_past
5827 14731483 : ? !tree_int_cst_le (index, nelts)
5828 9764730 : : !tree_int_cst_lt (index, nelts))
5829 : {
5830 458 : diag_array_subscript (loc, ctx, ary, index);
5831 458 : *non_constant_p = true;
5832 458 : return t;
5833 : }
5834 :
5835 : return index;
5836 : }
5837 :
5838 : /* Subroutine of cxx_eval_constant_expression.
5839 : Attempt to reduce a reference to an array slot. */
5840 :
5841 : static tree
5842 10623027 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5843 : value_cat lval,
5844 : bool *non_constant_p, bool *overflow_p,
5845 : tree *jump_target)
5846 : {
5847 10623027 : tree oldary = TREE_OPERAND (t, 0);
5848 10623027 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5849 : lval,
5850 : non_constant_p, overflow_p,
5851 : jump_target);
5852 10623027 : if (*non_constant_p)
5853 : return t;
5854 10427643 : if (*jump_target)
5855 : return NULL_TREE;
5856 10427643 : if (!lval
5857 5459617 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5858 13228 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5859 10440871 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5860 13228 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5861 13228 : ary = TREE_OPERAND (ary, 0);
5862 :
5863 10427643 : tree oldidx = TREE_OPERAND (t, 1);
5864 10427643 : tree index = eval_and_check_array_index (ctx, t, lval,
5865 : non_constant_p, overflow_p,
5866 : jump_target);
5867 10427643 : if (*non_constant_p)
5868 : return t;
5869 10242392 : if (*jump_target)
5870 : return NULL_TREE;
5871 :
5872 10242392 : if (lval && ary == oldary && index == oldidx)
5873 : return t;
5874 6456479 : else if (lval == vc_discard)
5875 : return t;
5876 6456479 : else if (lval)
5877 1180600 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5878 :
5879 5275879 : unsigned len = 0, elem_nchars = 1;
5880 5275879 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5881 5275879 : if (TREE_CODE (ary) == CONSTRUCTOR)
5882 2587097 : len = CONSTRUCTOR_NELTS (ary);
5883 2688782 : else if (TREE_CODE (ary) == STRING_CST)
5884 : {
5885 2675562 : elem_nchars = (TYPE_PRECISION (elem_type)
5886 2675562 : / TYPE_PRECISION (char_type_node));
5887 2675562 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5888 : }
5889 13220 : else if (TREE_CODE (ary) == VECTOR_CST)
5890 : /* We don't create variable-length VECTOR_CSTs. */
5891 13220 : len = VECTOR_CST_NELTS (ary).to_constant ();
5892 : else
5893 : {
5894 : /* We can't do anything with other tree codes, so use
5895 : VERIFY_CONSTANT to complain and fail. */
5896 0 : VERIFY_CONSTANT (ary);
5897 0 : gcc_unreachable ();
5898 : }
5899 :
5900 5275879 : bool found;
5901 5275879 : HOST_WIDE_INT i = 0;
5902 5275879 : if (TREE_CODE (ary) == CONSTRUCTOR)
5903 : {
5904 2587097 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5905 2587097 : found = (ix >= 0);
5906 2587097 : if (found)
5907 2482417 : i = ix;
5908 : }
5909 : else
5910 : {
5911 2688782 : i = tree_to_shwi (index);
5912 2688782 : found = (i < len);
5913 : }
5914 :
5915 5275879 : if (found)
5916 : {
5917 5170748 : tree r;
5918 5170748 : if (TREE_CODE (ary) == CONSTRUCTOR)
5919 : {
5920 2482417 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5921 2482417 : if (TREE_CODE (r) == RAW_DATA_CST)
5922 : {
5923 28951 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5924 28951 : gcc_checking_assert (ridx);
5925 28951 : unsigned int off
5926 28951 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5927 28951 : r = raw_data_cst_elt (r, off);
5928 : }
5929 : }
5930 2688331 : else if (TREE_CODE (ary) == VECTOR_CST)
5931 13220 : r = VECTOR_CST_ELT (ary, i);
5932 : else
5933 2675111 : r = extract_string_elt (ary, elem_nchars, i);
5934 :
5935 2717282 : if (r)
5936 : /* Don't VERIFY_CONSTANT here. */
5937 5170748 : return r;
5938 :
5939 : /* Otherwise the element doesn't have a value yet. */
5940 : }
5941 :
5942 : /* Not found. */
5943 :
5944 105131 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
5945 63 : return build_constructor (elem_type, NULL);
5946 :
5947 105068 : if (TREE_CODE (ary) == CONSTRUCTOR
5948 105068 : && CONSTRUCTOR_NO_CLEARING (ary))
5949 : {
5950 : /* 'ary' is part of the aggregate initializer we're currently
5951 : building; if there's no initializer for this element yet,
5952 : that's an error. */
5953 51 : if (!ctx->quiet)
5954 16 : error ("accessing uninitialized array element");
5955 51 : *non_constant_p = true;
5956 51 : return t;
5957 : }
5958 :
5959 : /* If it's within the array bounds but doesn't have an explicit
5960 : initializer, it's initialized from {}. But use build_value_init
5961 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
5962 105017 : tree val;
5963 105017 : constexpr_ctx new_ctx;
5964 105017 : if (CP_AGGREGATE_TYPE_P (elem_type))
5965 : {
5966 479 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
5967 479 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
5968 : }
5969 : else
5970 104538 : val = build_value_init (elem_type, tf_warning_or_error);
5971 :
5972 : /* Create a new constructor only if we don't already have a suitable one. */
5973 105017 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
5974 105528 : && (!ctx->ctor
5975 45 : || !same_type_ignoring_top_level_qualifiers_p
5976 45 : (elem_type, TREE_TYPE (ctx->ctor))));
5977 502 : if (new_ctor)
5978 : {
5979 502 : new_ctx = *ctx;
5980 : /* We clear the object here. We used to replace it with T, but that
5981 : caused problems (101371, 108158); and anyway, T is the initializer,
5982 : not the target object. */
5983 502 : new_ctx.object = NULL_TREE;
5984 502 : new_ctx.ctor = build_constructor (elem_type, NULL);
5985 502 : ctx = &new_ctx;
5986 : }
5987 105017 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5988 : overflow_p, jump_target);
5989 105017 : if (new_ctor && t != ctx->ctor)
5990 481 : free_constructor (ctx->ctor);
5991 : return t;
5992 : }
5993 :
5994 : /* Subroutine of cxx_eval_constant_expression.
5995 : Attempt to reduce a field access of a value of class type. */
5996 :
5997 : static tree
5998 105743540 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
5999 : value_cat lval,
6000 : bool *non_constant_p, bool *overflow_p,
6001 : tree *jump_target)
6002 : {
6003 105743540 : unsigned HOST_WIDE_INT i;
6004 105743540 : tree field;
6005 105743540 : tree value;
6006 105743540 : tree part = TREE_OPERAND (t, 1);
6007 105743540 : tree orig_whole = TREE_OPERAND (t, 0);
6008 105743540 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6009 : lval,
6010 : non_constant_p, overflow_p,
6011 : jump_target);
6012 105743540 : if (*non_constant_p)
6013 : return t;
6014 63089782 : if (*jump_target)
6015 : return NULL_TREE;
6016 63089782 : if (INDIRECT_REF_P (whole)
6017 63089782 : && integer_zerop (TREE_OPERAND (whole, 0)))
6018 : {
6019 177 : if (!ctx->quiet)
6020 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6021 177 : *non_constant_p = true;
6022 177 : return t;
6023 : }
6024 :
6025 63089605 : if (TREE_CODE (whole) == PTRMEM_CST)
6026 1463 : whole = cplus_expand_constant (whole);
6027 63089605 : if (whole == orig_whole)
6028 : return t;
6029 42589380 : if (lval == vc_discard)
6030 : return t;
6031 42589356 : if (lval)
6032 17312292 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6033 : whole, part, NULL_TREE);
6034 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6035 : CONSTRUCTOR. */
6036 25277064 : if (TREE_CODE (whole) != CONSTRUCTOR)
6037 : {
6038 0 : if (!ctx->quiet)
6039 0 : error ("%qE is not a constant expression", orig_whole);
6040 0 : *non_constant_p = true;
6041 0 : return t;
6042 : }
6043 25275566 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6044 25278744 : && DECL_MUTABLE_P (part))
6045 : {
6046 144 : if (!ctx->quiet)
6047 16 : error ("mutable %qD is not usable in a constant expression", part);
6048 144 : *non_constant_p = true;
6049 144 : return t;
6050 : }
6051 :
6052 25276920 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6053 36112045 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6054 : {
6055 : /* Use name match for PMF fields, as a variant will have a
6056 : different FIELD_DECL with a different type. */
6057 36000029 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6058 : : field == part)
6059 : {
6060 25164904 : if (value == void_node)
6061 3 : goto uninit;
6062 25164901 : if (value)
6063 : {
6064 25164883 : STRIP_ANY_LOCATION_WRAPPER (value);
6065 25164883 : return value;
6066 : }
6067 : else
6068 : /* We're in the middle of initializing it. */
6069 : break;
6070 : }
6071 : }
6072 112034 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6073 : {
6074 106 : if (CONSTRUCTOR_NELTS (whole) > 0)
6075 : {
6076 : /* DR 1188 says we don't have to deal with this. */
6077 91 : if (!ctx->quiet)
6078 : {
6079 19 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6080 19 : if (cep->value == NULL_TREE)
6081 9 : error ("accessing uninitialized member %qD", part);
6082 : else
6083 10 : error ("accessing %qD member instead of active %qD member "
6084 : "in constant expression", part, cep->index);
6085 : }
6086 91 : *non_constant_p = true;
6087 91 : return t;
6088 : }
6089 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6090 : {
6091 : /* Value-initialized union, check if looking at the first member. */
6092 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6093 12 : if (first != part)
6094 : {
6095 9 : if (!ctx->quiet)
6096 3 : error ("accessing %qD member instead of initialized %qD "
6097 : "member in constant expression", part, first);
6098 9 : *non_constant_p = true;
6099 9 : return t;
6100 : }
6101 : }
6102 : }
6103 :
6104 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6105 : classes never get represented; throw together a value now. */
6106 111934 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6107 103366 : return build_constructor (TREE_TYPE (t), NULL);
6108 :
6109 8568 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6110 :
6111 8568 : if (CONSTRUCTOR_NO_CLEARING (whole))
6112 : {
6113 102 : uninit:
6114 : /* 'whole' is part of the aggregate initializer we're currently
6115 : building; if there's no initializer for this member yet, that's an
6116 : error. */
6117 105 : if (!ctx->quiet)
6118 22 : error ("accessing uninitialized member %qD", part);
6119 105 : *non_constant_p = true;
6120 105 : return t;
6121 : }
6122 :
6123 : /* If there's no explicit init for this field, it's value-initialized. */
6124 :
6125 8466 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6126 : {
6127 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6128 : and copy the flags. */
6129 7970 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6130 7970 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6131 7970 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6132 7970 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6133 7970 : return value;
6134 : }
6135 :
6136 496 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6137 496 : return cxx_eval_constant_expression (ctx, value,
6138 : lval,
6139 : non_constant_p, overflow_p,
6140 496 : jump_target);
6141 : }
6142 :
6143 : /* Subroutine of cxx_eval_constant_expression.
6144 : Attempt to reduce a field access of a value of class type that is
6145 : expressed as a BIT_FIELD_REF. */
6146 :
6147 : static tree
6148 1914 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6149 : value_cat lval,
6150 : bool *non_constant_p, bool *overflow_p,
6151 : tree *jump_target)
6152 : {
6153 1914 : tree orig_whole = TREE_OPERAND (t, 0);
6154 1914 : tree retval, fldval, utype, mask;
6155 1914 : bool fld_seen = false;
6156 1914 : HOST_WIDE_INT istart, isize;
6157 1914 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6158 : lval,
6159 : non_constant_p, overflow_p,
6160 : jump_target);
6161 1914 : tree start, field, value;
6162 1914 : unsigned HOST_WIDE_INT i;
6163 :
6164 1914 : if (*jump_target)
6165 : return NULL_TREE;
6166 1914 : if (whole == orig_whole)
6167 : return t;
6168 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6169 : CONSTRUCTOR. */
6170 1741 : if (!*non_constant_p
6171 1741 : && TREE_CODE (whole) != VECTOR_CST
6172 1739 : && TREE_CODE (whole) != CONSTRUCTOR)
6173 : {
6174 0 : if (!ctx->quiet)
6175 0 : error ("%qE is not a constant expression", orig_whole);
6176 0 : *non_constant_p = true;
6177 : }
6178 1741 : if (*non_constant_p)
6179 : return t;
6180 :
6181 1741 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6182 : {
6183 2 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6184 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6185 : return r;
6186 0 : if (!ctx->quiet)
6187 0 : error ("%qE is not a constant expression", orig_whole);
6188 0 : *non_constant_p = true;
6189 0 : return t;
6190 : }
6191 :
6192 1739 : start = TREE_OPERAND (t, 2);
6193 1739 : istart = tree_to_shwi (start);
6194 1739 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6195 1739 : utype = TREE_TYPE (t);
6196 1739 : if (!TYPE_UNSIGNED (utype))
6197 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6198 1739 : retval = build_int_cst (utype, 0);
6199 24346 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6200 : {
6201 22607 : tree bitpos = bit_position (field);
6202 22607 : STRIP_ANY_LOCATION_WRAPPER (value);
6203 22607 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6204 : return value;
6205 22607 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6206 22607 : && TREE_CODE (value) == INTEGER_CST
6207 22607 : && tree_fits_shwi_p (bitpos)
6208 45214 : && tree_fits_shwi_p (DECL_SIZE (field)))
6209 : {
6210 22607 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6211 22607 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6212 22607 : HOST_WIDE_INT shift;
6213 22607 : if (bit >= istart && bit + sz <= istart + isize)
6214 : {
6215 5217 : fldval = fold_convert (utype, value);
6216 5217 : mask = build_int_cst_type (utype, -1);
6217 5217 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6218 : size_int (TYPE_PRECISION (utype) - sz));
6219 5217 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6220 : size_int (TYPE_PRECISION (utype) - sz));
6221 5217 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6222 5217 : shift = bit - istart;
6223 5217 : if (BYTES_BIG_ENDIAN)
6224 : shift = TYPE_PRECISION (utype) - shift - sz;
6225 5217 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6226 : size_int (shift));
6227 5217 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6228 5217 : fld_seen = true;
6229 : }
6230 : }
6231 : }
6232 1739 : if (fld_seen)
6233 1739 : return fold_convert (TREE_TYPE (t), retval);
6234 0 : gcc_unreachable ();
6235 : return error_mark_node;
6236 : }
6237 :
6238 : /* Helper for cxx_eval_bit_cast.
6239 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6240 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6241 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6242 : data members of reference type. */
6243 :
6244 : static bool
6245 4632 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6246 : tree orig_type)
6247 : {
6248 5143 : if (TREE_CODE (type) == UNION_TYPE)
6249 : {
6250 63 : if (!ctx->quiet)
6251 : {
6252 21 : if (type == orig_type)
6253 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6254 : "a union type", "__builtin_bit_cast", type);
6255 : else
6256 15 : error_at (loc, "%qs is not a constant expression because %qT "
6257 : "contains a union type", "__builtin_bit_cast",
6258 : orig_type);
6259 : }
6260 63 : return true;
6261 : }
6262 : if (TREE_CODE (type) == POINTER_TYPE)
6263 : {
6264 57 : if (!ctx->quiet)
6265 : {
6266 18 : if (type == orig_type)
6267 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6268 : "a pointer type", "__builtin_bit_cast", type);
6269 : else
6270 12 : error_at (loc, "%qs is not a constant expression because %qT "
6271 : "contains a pointer type", "__builtin_bit_cast",
6272 : orig_type);
6273 : }
6274 57 : return true;
6275 : }
6276 : if (TREE_CODE (type) == REFERENCE_TYPE)
6277 : {
6278 0 : if (!ctx->quiet)
6279 : {
6280 0 : if (type == orig_type)
6281 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6282 : "a reference type", "__builtin_bit_cast", type);
6283 : else
6284 0 : error_at (loc, "%qs is not a constant expression because %qT "
6285 : "contains a reference type", "__builtin_bit_cast",
6286 : orig_type);
6287 : }
6288 0 : return true;
6289 : }
6290 1383 : if (TYPE_PTRMEM_P (type))
6291 : {
6292 36 : if (!ctx->quiet)
6293 : {
6294 12 : if (type == orig_type)
6295 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6296 : "a pointer to member type", "__builtin_bit_cast",
6297 : type);
6298 : else
6299 0 : error_at (loc, "%qs is not a constant expression because %qT "
6300 : "contains a pointer to member type",
6301 : "__builtin_bit_cast", orig_type);
6302 : }
6303 36 : return true;
6304 : }
6305 4987 : if (TYPE_VOLATILE (type))
6306 : {
6307 0 : if (!ctx->quiet)
6308 : {
6309 0 : if (type == orig_type)
6310 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6311 : "volatile", "__builtin_bit_cast", type);
6312 : else
6313 0 : error_at (loc, "%qs is not a constant expression because %qT "
6314 : "contains a volatile subobject",
6315 : "__builtin_bit_cast", orig_type);
6316 : }
6317 0 : return true;
6318 : }
6319 4987 : if (TREE_CODE (type) == RECORD_TYPE)
6320 28046 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6321 26771 : if (TREE_CODE (field) == FIELD_DECL
6322 26771 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6323 : return true;
6324 4897 : if (TREE_CODE (type) == ARRAY_TYPE)
6325 511 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6326 : return false;
6327 : }
6328 :
6329 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6330 : std::byte members of CONSTRUCTOR (recursively) if they contain
6331 : some indeterminate bits (as set in MASK), remove the ctor elts,
6332 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6333 : bits in MASK. */
6334 :
6335 : static void
6336 569 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6337 : {
6338 569 : if (TREE_CODE (t) != CONSTRUCTOR)
6339 : return;
6340 :
6341 : unsigned i, j = 0;
6342 : tree index, value;
6343 1982 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6344 : {
6345 1413 : tree type = TREE_TYPE (value);
6346 1413 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6347 2387 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6348 : {
6349 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6350 : {
6351 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6352 135 : gcc_assert (fldsz != 0);
6353 135 : HOST_WIDE_INT pos = int_byte_position (index);
6354 135 : HOST_WIDE_INT bpos
6355 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6356 135 : bpos %= BITS_PER_UNIT;
6357 135 : HOST_WIDE_INT end
6358 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6359 135 : gcc_assert (end == 1 || end == 2);
6360 135 : unsigned char *p = mask + pos;
6361 135 : unsigned char mask_save[2];
6362 135 : mask_save[0] = mask[pos];
6363 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6364 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6365 : sorry_at (loc, "PDP11 bit-field handling unsupported"
6366 : " in %qs", "__builtin_bit_cast");
6367 135 : else if (BYTES_BIG_ENDIAN)
6368 : {
6369 : /* Big endian. */
6370 : if (bpos + fldsz <= BITS_PER_UNIT)
6371 : *p &= ~(((1 << fldsz) - 1)
6372 : << (BITS_PER_UNIT - bpos - fldsz));
6373 : else
6374 : {
6375 : gcc_assert (bpos);
6376 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6377 : p++;
6378 : fldsz -= BITS_PER_UNIT - bpos;
6379 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6380 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6381 : }
6382 : }
6383 : else
6384 : {
6385 : /* Little endian. */
6386 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6387 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6388 : else
6389 : {
6390 0 : gcc_assert (bpos);
6391 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6392 0 : p++;
6393 0 : fldsz -= BITS_PER_UNIT - bpos;
6394 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6395 0 : *p &= ~((1 << fldsz) - 1);
6396 : }
6397 : }
6398 135 : if (mask_save[0] != mask[pos]
6399 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6400 : {
6401 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6402 36 : continue;
6403 : }
6404 : }
6405 : }
6406 1143 : else if (is_byte_access_type_not_plain_char (type))
6407 : {
6408 228 : HOST_WIDE_INT pos;
6409 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6410 132 : pos = tree_to_shwi (index);
6411 : else
6412 96 : pos = int_byte_position (index);
6413 228 : if (mask[pos])
6414 : {
6415 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6416 48 : mask[pos] = 0;
6417 48 : continue;
6418 : }
6419 : }
6420 1329 : if (TREE_CODE (value) == CONSTRUCTOR)
6421 : {
6422 264 : HOST_WIDE_INT pos;
6423 264 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6424 144 : pos = tree_to_shwi (index)
6425 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6426 : else
6427 192 : pos = int_byte_position (index);
6428 264 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6429 : }
6430 1329 : if (i != j)
6431 : {
6432 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6433 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6434 : }
6435 1329 : ++j;
6436 : }
6437 1138 : if (CONSTRUCTOR_NELTS (t) != j)
6438 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6439 : }
6440 :
6441 : /* Subroutine of cxx_eval_constant_expression.
6442 : Attempt to evaluate a BIT_CAST_EXPR. */
6443 :
6444 : static tree
6445 923 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6446 : bool *overflow_p, tree *jump_target)
6447 : {
6448 923 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6449 923 : TREE_TYPE (t))
6450 3019 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6451 842 : EXPR_LOCATION (t)),
6452 842 : TREE_TYPE (TREE_OPERAND (t, 0)),
6453 842 : TREE_TYPE (TREE_OPERAND (t, 0))))
6454 : {
6455 156 : *non_constant_p = true;
6456 156 : return t;
6457 : }
6458 :
6459 767 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6460 : non_constant_p, overflow_p,
6461 : jump_target);
6462 767 : if (*non_constant_p)
6463 : return t;
6464 646 : if (*jump_target)
6465 : return NULL_TREE;
6466 :
6467 646 : location_t loc = EXPR_LOCATION (t);
6468 646 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6469 : {
6470 : if (!ctx->quiet)
6471 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6472 : "__builtin_bit_cast");
6473 : *non_constant_p = true;
6474 : return t;
6475 : }
6476 :
6477 646 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6478 : {
6479 0 : if (!ctx->quiet)
6480 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6481 : "type is too large", "__builtin_bit_cast");
6482 0 : *non_constant_p = true;
6483 0 : return t;
6484 : }
6485 :
6486 646 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6487 646 : if (len < 0 || (int) len != len)
6488 : {
6489 0 : if (!ctx->quiet)
6490 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6491 : "type is too large", "__builtin_bit_cast");
6492 0 : *non_constant_p = true;
6493 0 : return t;
6494 : }
6495 :
6496 646 : unsigned char buf[64];
6497 646 : unsigned char *ptr, *mask;
6498 646 : size_t alen = (size_t) len * 2;
6499 646 : if (alen <= sizeof (buf))
6500 : ptr = buf;
6501 : else
6502 3 : ptr = XNEWVEC (unsigned char, alen);
6503 646 : mask = ptr + (size_t) len;
6504 : /* At the beginning consider everything indeterminate. */
6505 646 : memset (mask, ~0, (size_t) len);
6506 :
6507 646 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6508 : {
6509 0 : if (!ctx->quiet)
6510 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6511 : "argument cannot be encoded", "__builtin_bit_cast");
6512 0 : *non_constant_p = true;
6513 0 : if (ptr != buf)
6514 0 : XDELETE (ptr);
6515 0 : return t;
6516 : }
6517 :
6518 646 : tree r = NULL_TREE;
6519 646 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6520 : {
6521 341 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6522 341 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6523 : {
6524 46 : gcc_assert (len == 1);
6525 46 : if (mask[0])
6526 : {
6527 24 : memset (mask, 0, len);
6528 24 : r = build_constructor (TREE_TYPE (r), NULL);
6529 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6530 : }
6531 : }
6532 : }
6533 305 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6534 : {
6535 305 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6536 305 : if (r != NULL_TREE)
6537 : {
6538 305 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6539 305 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6540 305 : if (CHECKING_P)
6541 : {
6542 305 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6543 : non_constant_p, overflow_p,
6544 : jump_target);
6545 305 : gcc_checking_assert (e == r && !*jump_target);
6546 : r = e;
6547 : }
6548 : }
6549 : }
6550 :
6551 646 : if (r != NULL_TREE)
6552 : {
6553 4925 : for (int i = 0; i < len; i++)
6554 4369 : if (mask[i])
6555 : {
6556 90 : if (!ctx->quiet)
6557 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6558 : "__builtin_bit_cast", i);
6559 90 : *non_constant_p = true;
6560 90 : r = t;
6561 90 : break;
6562 : }
6563 646 : if (ptr != buf)
6564 3 : XDELETE (ptr);
6565 646 : return r;
6566 : }
6567 :
6568 0 : if (!ctx->quiet)
6569 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6570 : "argument cannot be interpreted", "__builtin_bit_cast");
6571 0 : *non_constant_p = true;
6572 0 : if (ptr != buf)
6573 0 : XDELETE (ptr);
6574 : return t;
6575 : }
6576 :
6577 : /* Subroutine of cxx_eval_constant_expression.
6578 : Evaluate a short-circuited logical expression T in the context
6579 : of a given constexpr CALL. BAILOUT_VALUE is the value for
6580 : early return. CONTINUE_VALUE is used here purely for
6581 : sanity check purposes. */
6582 :
6583 : static tree
6584 15728484 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6585 : tree bailout_value, tree continue_value,
6586 : bool *non_constant_p, bool *overflow_p,
6587 : tree *jump_target)
6588 : {
6589 15728484 : tree r;
6590 15728484 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6591 : vc_prvalue, non_constant_p,
6592 : overflow_p, jump_target);
6593 15728483 : if (*jump_target)
6594 : return NULL_TREE;
6595 15728464 : VERIFY_CONSTANT (lhs);
6596 8630729 : if (tree_int_cst_equal (lhs, bailout_value))
6597 : return lhs;
6598 6746104 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6599 6746104 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6600 : vc_prvalue, non_constant_p,
6601 : overflow_p, jump_target);
6602 6746104 : if (*jump_target)
6603 : return NULL_TREE;
6604 6746094 : VERIFY_CONSTANT (r);
6605 : return r;
6606 : }
6607 :
6608 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6609 : CONSTRUCTOR elements to initialize (part of) an object containing that
6610 : field. Return a pointer to the constructor_elt corresponding to the
6611 : initialization of the field. */
6612 :
6613 : static constructor_elt *
6614 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6615 : {
6616 0 : tree aggr = TREE_OPERAND (ref, 0);
6617 0 : tree field = TREE_OPERAND (ref, 1);
6618 0 : HOST_WIDE_INT i;
6619 0 : constructor_elt *ce;
6620 :
6621 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6622 :
6623 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6624 : {
6625 0 : constructor_elt *base_ce
6626 0 : = base_field_constructor_elt (v, aggr);
6627 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6628 : }
6629 :
6630 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6631 0 : if (ce->index == field)
6632 0 : return ce;
6633 :
6634 0 : gcc_unreachable ();
6635 : return NULL;
6636 : }
6637 :
6638 : /* Some of the expressions fed to the constexpr mechanism are calls to
6639 : constructors, which have type void. In that case, return the type being
6640 : initialized by the constructor. */
6641 :
6642 : static tree
6643 612505703 : initialized_type (tree t)
6644 : {
6645 614941682 : if (TYPE_P (t))
6646 : return t;
6647 614941213 : tree type = TREE_TYPE (t);
6648 614941213 : if (TREE_CODE (t) == CALL_EXPR)
6649 : {
6650 : /* A constructor call has void type, so we need to look deeper. */
6651 88286481 : tree fn = get_function_named_in_call (t);
6652 88286466 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6653 176405714 : && DECL_CXX_CONSTRUCTOR_P (fn))
6654 5116654 : type = DECL_CONTEXT (fn);
6655 : }
6656 526654732 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6657 2435979 : return initialized_type (TREE_OPERAND (t, 1));
6658 524218753 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6659 436747 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6660 612505234 : return cv_unqualified (type);
6661 : }
6662 :
6663 : /* We're about to initialize element INDEX of an array or class from VALUE.
6664 : Set up NEW_CTX appropriately by adjusting .object to refer to the
6665 : subobject and creating a new CONSTRUCTOR if the element is itself
6666 : a class or array. */
6667 :
6668 : static void
6669 3312663 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6670 : tree index, tree &value)
6671 : {
6672 3312663 : new_ctx = *ctx;
6673 :
6674 3312663 : if (index && TREE_CODE (index) != INTEGER_CST
6675 3044361 : && TREE_CODE (index) != FIELD_DECL
6676 3 : && TREE_CODE (index) != RANGE_EXPR)
6677 : /* This won't have an element in the new CONSTRUCTOR. */
6678 : return;
6679 :
6680 3312663 : tree type = initialized_type (value);
6681 3312663 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6682 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6683 : return;
6684 731094 : if (VECTOR_TYPE_P (type)
6685 1289 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
6686 731130 : && index == NULL_TREE)
6687 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6688 : vector is constructed from smaller vectors, doesn't get its own
6689 : CONSTRUCTOR either. */
6690 : return;
6691 :
6692 : /* The sub-aggregate initializer might contain a placeholder;
6693 : update object to refer to the subobject and ctor to refer to
6694 : the (newly created) sub-initializer. */
6695 731058 : if (ctx->object)
6696 : {
6697 543561 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6698 : /* There's no well-defined subobject for this index. */
6699 3 : new_ctx.object = NULL_TREE;
6700 : else
6701 543558 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6702 : }
6703 :
6704 731058 : if (is_empty_class (type))
6705 : /* Leave ctor null for an empty subobject, they aren't represented in the
6706 : result of evaluation. */
6707 626918 : new_ctx.ctor = NULL_TREE;
6708 : else
6709 : {
6710 104140 : tree elt = build_constructor (type, NULL);
6711 104140 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6712 104140 : new_ctx.ctor = elt;
6713 : }
6714 :
6715 731058 : if (TREE_CODE (value) == TARGET_EXPR)
6716 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6717 93233 : value = TARGET_EXPR_INITIAL (value);
6718 : }
6719 :
6720 : /* We're about to process an initializer for a class or array TYPE. Make
6721 : sure that CTX is set up appropriately. */
6722 :
6723 : static void
6724 2219034 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6725 : {
6726 : /* We don't bother building a ctor for an empty base subobject. */
6727 2219034 : if (is_empty_class (type))
6728 : return;
6729 :
6730 : /* We're in the middle of an initializer that might involve placeholders;
6731 : our caller should have created a CONSTRUCTOR for us to put the
6732 : initializer into. We will either return that constructor or T. */
6733 1597356 : gcc_assert (ctx->ctor);
6734 1597356 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6735 : (type, TREE_TYPE (ctx->ctor)));
6736 : /* We used to check that ctx->ctor was empty, but that isn't the case when
6737 : the object is zero-initialized before calling the constructor. */
6738 1597356 : if (ctx->object)
6739 : {
6740 1191414 : tree otype = TREE_TYPE (ctx->object);
6741 1191414 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6742 : /* Handle flexible array members. */
6743 : || (TREE_CODE (otype) == ARRAY_TYPE
6744 : && TYPE_DOMAIN (otype) == NULL_TREE
6745 : && TREE_CODE (type) == ARRAY_TYPE
6746 : && (same_type_ignoring_top_level_qualifiers_p
6747 : (TREE_TYPE (type), TREE_TYPE (otype)))));
6748 : }
6749 1597356 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6750 : || ctx->global->get_value (ctx->object) == ctx->ctor);
6751 : }
6752 :
6753 : /* Subroutine of cxx_eval_constant_expression.
6754 : The expression tree T denotes a C-style array or a C-style
6755 : aggregate. Reduce it to a constant expression. */
6756 :
6757 : static tree
6758 2218392 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6759 : value_cat lval,
6760 : bool *non_constant_p, bool *overflow_p,
6761 : tree *jump_target)
6762 : {
6763 2218392 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6764 2218392 : bool changed = false;
6765 2218392 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6766 2218392 : tree type = TREE_TYPE (t);
6767 :
6768 2218392 : constexpr_ctx new_ctx;
6769 2218392 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6770 : {
6771 : /* We don't really need the ctx->ctor business for a PMF or
6772 : vector, but it's simpler to use the same code. */
6773 26343 : new_ctx = *ctx;
6774 26343 : new_ctx.ctor = build_constructor (type, NULL);
6775 26343 : new_ctx.object = NULL_TREE;
6776 26343 : ctx = &new_ctx;
6777 2218392 : };
6778 2218392 : verify_ctor_sanity (ctx, type);
6779 2218392 : vec<constructor_elt, va_gc> **p = nullptr;
6780 2218392 : if (ctx->ctor)
6781 : {
6782 2218384 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6783 4432731 : vec_alloc (*p, vec_safe_length (v));
6784 2218384 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6785 16053 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6786 : }
6787 :
6788 2218392 : unsigned i;
6789 2218392 : tree index, value;
6790 2218392 : bool constant_p = true;
6791 2218392 : bool side_effects_p = false;
6792 4819728 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6793 : {
6794 3306509 : tree orig_value = value;
6795 3306509 : init_subob_ctx (ctx, new_ctx, index, value);
6796 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6797 3306509 : bool no_slot = new_ctx.ctor == NULL_TREE;
6798 3306509 : int pos_hint = -1;
6799 3306509 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6800 : {
6801 : /* If we built a new CONSTRUCTOR, attach it now so that other
6802 : initializers can refer to it. */
6803 98182 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6804 98182 : cep->value = new_ctx.ctor;
6805 98182 : pos_hint = cep - (*p)->begin();
6806 98182 : }
6807 3208327 : else if (TREE_CODE (type) == UNION_TYPE)
6808 : /* Otherwise if we're constructing a non-aggregate union member, set
6809 : the active union member now so that we can later detect and diagnose
6810 : if its initializer attempts to activate another member. */
6811 1110 : get_or_insert_ctor_field (ctx->ctor, index);
6812 3306509 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6813 : lval,
6814 : non_constant_p, overflow_p,
6815 : jump_target);
6816 3306509 : if (*jump_target)
6817 : return NULL_TREE;
6818 : /* Don't VERIFY_CONSTANT here. */
6819 3306509 : if (ctx->quiet && *non_constant_p)
6820 : break;
6821 2601336 : if (elt != orig_value)
6822 419401 : changed = true;
6823 :
6824 2601336 : if (!TREE_CONSTANT (elt))
6825 1552724 : constant_p = false;
6826 2601336 : if (TREE_SIDE_EFFECTS (elt))
6827 126 : side_effects_p = true;
6828 2601336 : if (index && TREE_CODE (index) == COMPONENT_REF)
6829 : {
6830 : /* This is an initialization of a vfield inside a base
6831 : subaggregate that we already initialized; push this
6832 : initialization into the previous initialization. */
6833 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6834 0 : inner->value = elt;
6835 0 : changed = true;
6836 0 : }
6837 2601336 : else if (no_slot)
6838 : /* This is an initializer for an empty field; now that we've
6839 : checked that it's constant, we can ignore it. */
6840 : changed = true;
6841 1974960 : else if (index
6842 1974832 : && (TREE_CODE (index) == NOP_EXPR
6843 1974832 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
6844 : {
6845 : /* Old representation of empty bases. FIXME remove. */
6846 0 : gcc_checking_assert (false);
6847 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
6848 : changed = true;
6849 : }
6850 : else
6851 : {
6852 1974960 : if (TREE_CODE (type) == UNION_TYPE
6853 1974960 : && (*p)->last().index != index)
6854 : /* The initializer erroneously changed the active union member that
6855 : we're initializing. */
6856 7 : gcc_assert (*non_constant_p);
6857 : else
6858 : {
6859 : /* The initializer might have mutated the underlying CONSTRUCTOR,
6860 : so recompute the location of the target constructer_elt. */
6861 1974953 : constructor_elt *cep
6862 1974953 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6863 1974953 : cep->value = elt;
6864 : }
6865 :
6866 : /* Adding or replacing an element might change the ctor's flags. */
6867 1974960 : TREE_CONSTANT (ctx->ctor) = constant_p;
6868 1974960 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6869 : }
6870 : }
6871 2218392 : if (*non_constant_p)
6872 : return t;
6873 1513171 : if (!changed)
6874 : {
6875 558906 : if (VECTOR_TYPE_P (type))
6876 14274 : t = fold (t);
6877 558906 : return t;
6878 : }
6879 954265 : t = ctx->ctor;
6880 954265 : if (!t)
6881 8 : t = build_constructor (type, NULL);
6882 : /* We're done building this CONSTRUCTOR, so now we can interpret an
6883 : element without an explicit initializer as value-initialized. */
6884 954265 : CONSTRUCTOR_NO_CLEARING (t) = false;
6885 954265 : TREE_CONSTANT (t) = constant_p;
6886 954265 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6887 954265 : if (VECTOR_TYPE_P (type))
6888 1048 : t = fold (t);
6889 : return t;
6890 : }
6891 :
6892 : /* Subroutine of cxx_eval_constant_expression.
6893 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6894 : initialization of a non-static data member of array type. Reduce it to a
6895 : CONSTRUCTOR.
6896 :
6897 : Note that apart from value-initialization (when VALUE_INIT is true),
6898 : this is only intended to support value-initialization and the
6899 : initializations done by defaulted constructors for classes with
6900 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6901 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6902 : for the copy/move constructor. */
6903 :
6904 : static tree
6905 642 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6906 : bool value_init, value_cat lval,
6907 : bool *non_constant_p, bool *overflow_p,
6908 : tree *jump_target)
6909 : {
6910 642 : tree elttype = TREE_TYPE (atype);
6911 642 : verify_ctor_sanity (ctx, atype);
6912 642 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6913 642 : bool pre_init = false;
6914 642 : unsigned HOST_WIDE_INT i;
6915 642 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6916 :
6917 642 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6918 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6919 0 : non_constant_p, overflow_p, jump_target);
6920 :
6921 : /* We already checked access when building the VEC_INIT_EXPR. */
6922 642 : deferring_access_check_sentinel acs (dk_deferred);
6923 :
6924 : /* For the default constructor, build up a call to the default
6925 : constructor of the element type. We only need to handle class types
6926 : here, as for a constructor to be constexpr, all members must be
6927 : initialized, which for a defaulted default constructor means they must
6928 : be of a class type with a constexpr default constructor. */
6929 642 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6930 : /* We only do this at the lowest level. */;
6931 593 : else if (value_init)
6932 : {
6933 182 : init = build_value_init (elttype, complain);
6934 182 : pre_init = true;
6935 : }
6936 411 : else if (!init)
6937 : {
6938 268 : releasing_vec argvec;
6939 268 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6940 : &argvec, elttype, LOOKUP_NORMAL,
6941 : complain);
6942 268 : init = build_aggr_init_expr (elttype, init);
6943 268 : pre_init = true;
6944 268 : }
6945 :
6946 642 : bool zeroed_out = false;
6947 642 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
6948 : {
6949 : /* We're initializing an array object that had been zero-initialized
6950 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
6951 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
6952 : initializers we append to it. */
6953 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
6954 156 : zeroed_out = true;
6955 156 : vec_safe_truncate (*p, 0);
6956 : }
6957 :
6958 642 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
6959 : overflow_p, jump_target);
6960 642 : if (*jump_target)
6961 : return NULL_TREE;
6962 642 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
6963 6365 : for (i = 0; i < max; ++i)
6964 : {
6965 6154 : tree idx = build_int_cst (size_type_node, i);
6966 6154 : tree eltinit;
6967 6154 : bool reuse = false;
6968 6154 : constexpr_ctx new_ctx;
6969 6623 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
6970 6154 : bool no_slot = new_ctx.ctor == NULL_TREE;
6971 6154 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6972 : {
6973 5958 : if (zeroed_out)
6974 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
6975 5958 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
6976 : }
6977 6154 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6978 : {
6979 : /* A multidimensional array; recurse. */
6980 181 : if (value_init || init == NULL_TREE)
6981 : {
6982 171 : eltinit = NULL_TREE;
6983 171 : reuse = i == 0;
6984 : }
6985 : else
6986 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6987 181 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
6988 : value_init, lval, non_constant_p,
6989 : overflow_p, jump_target);
6990 : }
6991 5973 : else if (pre_init)
6992 : {
6993 : /* Initializing an element using value or default initialization
6994 : we just pre-built above. */
6995 5685 : if (init == void_node)
6996 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
6997 3 : return ctx->ctor;
6998 5682 : eltinit = init;
6999 5682 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7000 : /* Clarify what object is being initialized (118285). */
7001 5538 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7002 5682 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7003 : non_constant_p, overflow_p,
7004 : jump_target);
7005 5682 : reuse = i == 0;
7006 : }
7007 : else
7008 : {
7009 : /* Copying an element. */
7010 288 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7011 288 : if (!lvalue_p (init))
7012 257 : eltinit = move (eltinit);
7013 288 : eltinit = (perform_implicit_conversion_flags
7014 288 : (elttype, eltinit, complain,
7015 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7016 281 : if (CLASS_TYPE_P (elttype)
7017 281 : && new_ctx.object
7018 558 : && !error_operand_p (eltinit))
7019 : /* Clarify what object is being initialized (118285). */
7020 266 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7021 288 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7022 : non_constant_p, overflow_p,
7023 : jump_target);
7024 : }
7025 6151 : if (*jump_target)
7026 : return NULL_TREE;
7027 6151 : if (*non_constant_p)
7028 : break;
7029 5987 : if (no_slot)
7030 : {
7031 : /* This is an initializer for an empty subobject; now that we've
7032 : checked that it's constant, we can ignore it. */
7033 18 : gcc_checking_assert (i == 0);
7034 : break;
7035 : }
7036 5969 : else if (new_ctx.ctor != ctx->ctor)
7037 : {
7038 : /* We appended this element above; update the value. */
7039 5831 : gcc_assert ((*p)->last().index == idx);
7040 5831 : (*p)->last().value = eltinit;
7041 : }
7042 : else
7043 138 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7044 : /* Reuse the result of cxx_eval_constant_expression call
7045 : from the first iteration to all others if it is a constant
7046 : initializer that doesn't require relocations. */
7047 5969 : if (reuse
7048 5969 : && max > 1
7049 5969 : && (eltinit == NULL_TREE
7050 399 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7051 399 : == null_pointer_node)))
7052 : {
7053 246 : if (new_ctx.ctor != ctx->ctor)
7054 114 : eltinit = new_ctx.ctor;
7055 246 : tree range = build2 (RANGE_EXPR, size_type_node,
7056 : build_int_cst (size_type_node, 1),
7057 246 : build_int_cst (size_type_node, max - 1));
7058 246 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7059 246 : break;
7060 : }
7061 5723 : else if (i == 0)
7062 208 : vec_safe_reserve (*p, max);
7063 : }
7064 :
7065 639 : if (!*non_constant_p)
7066 : {
7067 475 : init = ctx->ctor;
7068 475 : CONSTRUCTOR_NO_CLEARING (init) = false;
7069 : }
7070 639 : return init;
7071 : }
7072 :
7073 : static tree
7074 524 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7075 : value_cat lval,
7076 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7077 : {
7078 524 : tree atype = TREE_TYPE (t);
7079 524 : tree init = VEC_INIT_EXPR_INIT (t);
7080 524 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7081 524 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7082 : ;
7083 74 : else if (CONSTRUCTOR_NELTS (init) == 0
7084 74 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7085 : {
7086 : /* Handle {} as value-init. */
7087 : init = NULL_TREE;
7088 : value_init = true;
7089 : }
7090 : else
7091 : {
7092 : /* This is a more complicated case, like needing to loop over trailing
7093 : elements; call build_vec_init and evaluate the result. */
7094 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7095 63 : constexpr_ctx new_ctx = *ctx;
7096 63 : if (!ctx->object)
7097 : {
7098 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7099 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7100 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7101 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7102 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7103 51 : ctx->global->put_value (new_ctx.object, ctor);
7104 51 : ctx = &new_ctx;
7105 : }
7106 63 : init = expand_vec_init_expr (ctx->object, t, complain);
7107 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7108 : overflow_p, jump_target);
7109 : }
7110 461 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7111 : lval, non_constant_p, overflow_p, jump_target);
7112 461 : if (*non_constant_p)
7113 : return t;
7114 : else
7115 320 : return r;
7116 : }
7117 :
7118 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7119 : where the desired type is an array of unknown bounds because the variable
7120 : has had its bounds deduced since the wrapping expression was created. */
7121 :
7122 : static bool
7123 351626285 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7124 : {
7125 351626285 : while (TREE_CODE (type1) == ARRAY_TYPE
7126 13295 : && TREE_CODE (type2) == ARRAY_TYPE
7127 351626289 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7128 : {
7129 2 : type1 = TREE_TYPE (type1);
7130 2 : type2 = TREE_TYPE (type2);
7131 : }
7132 351626285 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7133 : }
7134 :
7135 : /* Try to determine the currently active union member for an expression
7136 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7137 : otherwise return NULL_TREE. */
7138 :
7139 : static tree
7140 109 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7141 : {
7142 109 : constexpr_ctx new_ctx = *ctx;
7143 109 : new_ctx.quiet = true;
7144 109 : bool non_constant_p = false, overflow_p = false;
7145 109 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7146 : &non_constant_p,
7147 : &overflow_p, jump_target);
7148 109 : if (*jump_target)
7149 : return NULL_TREE;
7150 109 : if (TREE_CODE (ctor) == CONSTRUCTOR
7151 40 : && CONSTRUCTOR_NELTS (ctor) == 1
7152 16 : && CONSTRUCTOR_ELT (ctor, 0)->index
7153 125 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7154 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7155 : return NULL_TREE;
7156 : }
7157 :
7158 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7159 :
7160 : static tree
7161 12436272 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7162 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7163 : tree *jump_target)
7164 : {
7165 18907394 : tree optype = TREE_TYPE (op);
7166 18907394 : unsigned HOST_WIDE_INT const_nunits;
7167 18907394 : if (off == 0 && similar_type_p (optype, type))
7168 : return op;
7169 12433577 : else if (cxx_dialect >= cxx26
7170 3513661 : && VAR_P (op)
7171 1499660 : && DECL_VTABLE_OR_VTT_P (op)
7172 12787 : && same_type_ignoring_top_level_qualifiers_p (type,
7173 : ptrdiff_type_node)
7174 12434560 : && POINTER_TYPE_P (strip_array_types (optype)))
7175 : {
7176 : /* We often read some virtual table elements using ptrdiff_t rather
7177 : than pointer type. */
7178 983 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7179 : strip_array_types (optype),
7180 : op, off, empty_base,
7181 : jump_target))
7182 983 : return fold_convert (type, ret);
7183 : }
7184 12432594 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7185 12432594 : && similar_type_p (type, TREE_TYPE (optype)))
7186 : {
7187 : /* *(foo *)&complexfoo => __real__ complexfoo */
7188 0 : if (off == 0)
7189 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7190 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7191 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7192 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7193 : }
7194 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7195 12432594 : else if (VECTOR_TYPE_P (optype)
7196 0 : && similar_type_p (type, TREE_TYPE (optype))
7197 12432594 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7198 : {
7199 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7200 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7201 0 : if (off < max_offset && off % part_width == 0)
7202 : {
7203 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7204 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7205 0 : TYPE_SIZE (type), index);
7206 : }
7207 : }
7208 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7209 12432594 : else if (TREE_CODE (optype) == ARRAY_TYPE
7210 6471122 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7211 18903716 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7212 : {
7213 6471122 : tree type_domain = TYPE_DOMAIN (optype);
7214 6471122 : tree min_val = size_zero_node;
7215 6471122 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7216 6470983 : min_val = TYPE_MIN_VALUE (type_domain);
7217 6471122 : unsigned HOST_WIDE_INT el_sz
7218 6471122 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7219 6471122 : unsigned HOST_WIDE_INT idx = off / el_sz;
7220 6471122 : unsigned HOST_WIDE_INT rem = off % el_sz;
7221 6471122 : if (tree_fits_uhwi_p (min_val))
7222 : {
7223 6471122 : tree index = size_int (idx + tree_to_uhwi (min_val));
7224 6471122 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7225 : NULL_TREE, NULL_TREE);
7226 6471122 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7227 6471122 : empty_base, jump_target);
7228 : }
7229 : }
7230 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7231 5961472 : else if (TREE_CODE (optype) == RECORD_TYPE
7232 5961472 : || TREE_CODE (optype) == UNION_TYPE)
7233 : {
7234 5958036 : if (TREE_CODE (optype) == UNION_TYPE)
7235 : /* For unions prefer the currently active member. */
7236 109 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7237 : {
7238 16 : unsigned HOST_WIDE_INT el_sz
7239 16 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7240 16 : if (off < el_sz)
7241 : {
7242 16 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7243 : op, field, NULL_TREE);
7244 16 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7245 : off, empty_base,
7246 : jump_target))
7247 : return ret;
7248 : }
7249 : }
7250 :
7251 : /* Handle conversion to "as base" type. */
7252 5958026 : if (CLASS_TYPE_P (optype)
7253 11915888 : && CLASSTYPE_AS_BASE (optype) == type)
7254 : return op;
7255 :
7256 : /* Handle conversion to an empty base class, which is represented with a
7257 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7258 : which is likely to be a waste of time (109678). */
7259 5955192 : if (is_empty_class (type)
7260 5775432 : && CLASS_TYPE_P (optype)
7261 11730618 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
7262 : {
7263 2883038 : if (empty_base)
7264 2883038 : *empty_base = true;
7265 2883038 : return op;
7266 : }
7267 :
7268 3072154 : for (tree field = TYPE_FIELDS (optype);
7269 33334825 : field; field = DECL_CHAIN (field))
7270 33319320 : if (TREE_CODE (field) == FIELD_DECL
7271 3077942 : && TREE_TYPE (field) != error_mark_node
7272 36397262 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
7273 : {
7274 3077942 : tree pos = byte_position (field);
7275 3077942 : if (!tree_fits_uhwi_p (pos))
7276 0 : continue;
7277 3077942 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
7278 3077942 : unsigned HOST_WIDE_INT el_sz;
7279 3077942 : if (DECL_FIELD_IS_BASE (field)
7280 387108 : && CLASS_TYPE_P (optype)
7281 3465050 : && CLASSTYPE_VBASECLASSES (optype))
7282 6188 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
7283 : else
7284 3071754 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7285 3077942 : if (upos <= off && off < upos + el_sz)
7286 : {
7287 3072034 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7288 : op, field, NULL_TREE);
7289 3072034 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7290 : off - upos,
7291 : empty_base,
7292 : jump_target))
7293 : return ret;
7294 : }
7295 : }
7296 : }
7297 :
7298 : return NULL_TREE;
7299 : }
7300 :
7301 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7302 : match. We want to be less strict for simple *& folding; if we have a
7303 : non-const temporary that we access through a const pointer, that should
7304 : work. We handle this here rather than change fold_indirect_ref_1
7305 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7306 : don't really make sense outside of constant expression evaluation. Also
7307 : we want to allow folding to COMPONENT_REF, which could cause trouble
7308 : with TBAA in fold_indirect_ref_1. */
7309 :
7310 : static tree
7311 185128276 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7312 : tree op0, bool *empty_base, tree *jump_target)
7313 : {
7314 185128276 : tree sub = op0;
7315 185128276 : tree subtype;
7316 :
7317 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7318 384153947 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7319 520428964 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7320 : {
7321 148128952 : if (TREE_CODE (sub) == NOP_EXPR
7322 148128952 : && REINTERPRET_CAST_P (sub))
7323 : return NULL_TREE;
7324 148128952 : sub = TREE_OPERAND (sub, 0);
7325 : }
7326 :
7327 185128276 : subtype = TREE_TYPE (sub);
7328 185128276 : if (!INDIRECT_TYPE_P (subtype))
7329 : return NULL_TREE;
7330 :
7331 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7332 : the innermost component into the offset until it would make the
7333 : offset positive, so that cxx_fold_indirect_ref_1 can identify
7334 : more folding opportunities. */
7335 194491463 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7336 9363239 : if (cxx_dialect >= cxx26)
7337 : {
7338 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7339 : to access virtual base members. */
7340 2487878 : tree nobj = obj;
7341 2487878 : while (TREE_CODE (nobj) == COMPONENT_REF
7342 2506409 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7343 18531 : nobj = TREE_OPERAND (nobj, 0);
7344 2487878 : if (nobj != obj
7345 14459 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7346 2502337 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7347 2041 : while (obj != nobj)
7348 : {
7349 1175 : tree field = TREE_OPERAND (obj, 1);
7350 1175 : tree pos = byte_position (field);
7351 1175 : off = int_const_binop (PLUS_EXPR, off, pos);
7352 1175 : obj = TREE_OPERAND (obj, 0);
7353 : }
7354 : }
7355 12443536 : while (TREE_CODE (obj) == COMPONENT_REF
7356 : /* We need to preserve union member accesses so that we can
7357 : later properly diagnose accessing the wrong member. */
7358 6001538 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7359 18154847 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7360 : {
7361 3154999 : tree field = TREE_OPERAND (obj, 1);
7362 3154999 : tree pos = byte_position (field);
7363 3154999 : if (integer_zerop (off) && integer_nonzerop (pos))
7364 : /* If the offset is already 0, keep going as long as the
7365 : component is at position 0. */
7366 : break;
7367 3080297 : off = int_const_binop (PLUS_EXPR, off, pos);
7368 3080297 : obj = TREE_OPERAND (obj, 0);
7369 : }
7370 9363239 : };
7371 :
7372 185128224 : if (TREE_CODE (sub) == ADDR_EXPR)
7373 : {
7374 82725684 : tree op = TREE_OPERAND (sub, 0);
7375 82725684 : tree optype = TREE_TYPE (op);
7376 :
7377 : /* *&CONST_DECL -> to the value of the const decl. */
7378 82725684 : if (TREE_CODE (op) == CONST_DECL)
7379 0 : return DECL_INITIAL (op);
7380 : /* *&p => p; make sure to handle *&"str"[cst] here. */
7381 82725684 : if (similar_type_p (optype, type))
7382 : {
7383 78734094 : tree fop = fold_read_from_constant_string (op);
7384 78734094 : if (fop)
7385 : return fop;
7386 : else
7387 78638673 : return op;
7388 : }
7389 : else
7390 : {
7391 3991590 : tree off = integer_zero_node;
7392 3991590 : canonicalize_obj_off (op, off);
7393 3991590 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7394 : tree_to_uhwi (off), empty_base,
7395 : jump_target);
7396 : }
7397 : }
7398 102402540 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7399 102402540 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7400 : {
7401 5956715 : tree op00 = TREE_OPERAND (sub, 0);
7402 5956715 : tree off = TREE_OPERAND (sub, 1);
7403 :
7404 5956715 : STRIP_NOPS (op00);
7405 5956715 : if (TREE_CODE (op00) == ADDR_EXPR)
7406 : {
7407 5371649 : tree obj = TREE_OPERAND (op00, 0);
7408 5371649 : canonicalize_obj_off (obj, off);
7409 5371649 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7410 : tree_to_uhwi (off), empty_base,
7411 : jump_target);
7412 : }
7413 : }
7414 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7415 96445825 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7416 96445825 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7417 : {
7418 0 : tree type_domain;
7419 0 : tree min_val = size_zero_node;
7420 0 : tree newsub
7421 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7422 : jump_target);
7423 0 : if (*jump_target)
7424 : return NULL_TREE;
7425 0 : if (newsub)
7426 : sub = newsub;
7427 : else
7428 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7429 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7430 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7431 0 : min_val = TYPE_MIN_VALUE (type_domain);
7432 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7433 0 : NULL_TREE);
7434 : }
7435 :
7436 : return NULL_TREE;
7437 : }
7438 :
7439 : static tree
7440 101442620 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7441 : value_cat lval,
7442 : bool *non_constant_p, bool *overflow_p,
7443 : tree *jump_target)
7444 : {
7445 101442620 : tree orig_op0 = TREE_OPERAND (t, 0);
7446 101442620 : bool empty_base = false;
7447 :
7448 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7449 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7450 :
7451 101442620 : if (TREE_CODE (t) == MEM_REF
7452 101442620 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7453 : {
7454 9 : gcc_assert (ctx->quiet);
7455 9 : *non_constant_p = true;
7456 9 : return t;
7457 : }
7458 :
7459 : /* First try to simplify it directly. */
7460 101442611 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7461 : orig_op0, &empty_base, jump_target);
7462 101442611 : if (*jump_target)
7463 : return NULL_TREE;
7464 101442611 : if (!r)
7465 : {
7466 : /* If that didn't work, evaluate the operand first. */
7467 96341349 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7468 : vc_prvalue, non_constant_p,
7469 : overflow_p, jump_target);
7470 96341349 : if (*jump_target)
7471 : return NULL_TREE;
7472 : /* Don't VERIFY_CONSTANT here. */
7473 96341338 : if (*non_constant_p)
7474 : return t;
7475 :
7476 68722889 : if (!lval && integer_zerop (op0))
7477 : {
7478 70 : if (!ctx->quiet)
7479 12 : error ("dereferencing a null pointer");
7480 70 : *non_constant_p = true;
7481 70 : return t;
7482 : }
7483 :
7484 68722819 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7485 : &empty_base, jump_target);
7486 68722819 : if (*jump_target)
7487 : return NULL_TREE;
7488 68722819 : if (r == NULL_TREE)
7489 : {
7490 : /* We couldn't fold to a constant value. Make sure it's not
7491 : something we should have been able to fold. */
7492 693095 : tree sub = op0;
7493 693095 : STRIP_NOPS (sub);
7494 693095 : if (TREE_CODE (sub) == ADDR_EXPR)
7495 : {
7496 1694 : gcc_assert (!similar_type_p
7497 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7498 : /* DR 1188 says we don't have to deal with this. */
7499 1694 : if (!ctx->quiet)
7500 : {
7501 8 : auto_diagnostic_group d;
7502 13 : error_at (cp_expr_loc_or_input_loc (t),
7503 : "accessing value of %qT object through a %qT "
7504 : "glvalue in a constant expression",
7505 8 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7506 8 : tree ob = build_fold_indirect_ref (sub);
7507 8 : if (DECL_P (ob))
7508 : {
7509 8 : if (DECL_ARTIFICIAL (ob))
7510 1 : inform (DECL_SOURCE_LOCATION (ob),
7511 1 : "%qT object created here", TREE_TYPE (ob));
7512 : else
7513 7 : inform (DECL_SOURCE_LOCATION (ob),
7514 : "%q#D declared here", ob);
7515 : }
7516 8 : }
7517 1694 : *non_constant_p = true;
7518 1694 : return t;
7519 : }
7520 :
7521 691401 : if (lval == vc_glvalue && op0 != orig_op0)
7522 72273 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7523 619128 : if (!lval)
7524 518187 : VERIFY_CONSTANT (t);
7525 619128 : return t;
7526 : }
7527 : }
7528 :
7529 73130986 : r = cxx_eval_constant_expression (ctx, r,
7530 : lval, non_constant_p, overflow_p,
7531 : jump_target);
7532 73130985 : if (*jump_target)
7533 : return NULL_TREE;
7534 73130985 : if (*non_constant_p)
7535 : return t;
7536 :
7537 : /* If we're pulling out the value of an empty base, just return an empty
7538 : CONSTRUCTOR. */
7539 53479201 : if (empty_base && !lval)
7540 : {
7541 16376 : r = build_constructor (TREE_TYPE (t), NULL);
7542 16376 : TREE_CONSTANT (r) = true;
7543 : }
7544 :
7545 : return r;
7546 : }
7547 :
7548 : /* Complain about R, a DECL that is accessed outside its lifetime. */
7549 :
7550 : static void
7551 36 : outside_lifetime_error (location_t loc, tree r)
7552 : {
7553 36 : auto_diagnostic_group d;
7554 36 : if (DECL_NAME (r) == heap_deleted_identifier)
7555 : {
7556 : /* Provide a more accurate message for deleted variables. */
7557 6 : error_at (loc, "use of allocated storage after deallocation "
7558 : "in a constant expression");
7559 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7560 : }
7561 : else
7562 : {
7563 30 : error_at (loc, "accessing %qE outside its lifetime", r);
7564 30 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7565 : }
7566 36 : }
7567 :
7568 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7569 : FUNDEF_P is true if we're checking a constexpr function body.
7570 : Shared between potential_constant_expression and
7571 : cxx_eval_constant_expression. */
7572 :
7573 : static void
7574 334 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7575 : {
7576 334 : auto_diagnostic_group d;
7577 334 : tree type = TREE_TYPE (r);
7578 334 : if (DECL_NAME (r) == heap_uninit_identifier
7579 334 : || DECL_NAME (r) == heap_identifier
7580 331 : || DECL_NAME (r) == heap_vec_uninit_identifier
7581 665 : || DECL_NAME (r) == heap_vec_identifier)
7582 : {
7583 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7584 : "storage is not usable in a constant expression"))
7585 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7586 3 : return;
7587 : }
7588 331 : if (DECL_NAME (r) == heap_deleted_identifier)
7589 : {
7590 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7591 : "deallocation in a constant expression"))
7592 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7593 0 : return;
7594 : }
7595 331 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7596 : "a constant expression", r))
7597 : return;
7598 : /* Avoid error cascade. */
7599 328 : if (DECL_INITIAL (r) == error_mark_node)
7600 : return;
7601 314 : if (DECL_DECLARED_CONSTEXPR_P (r))
7602 3 : inform (DECL_SOURCE_LOCATION (r),
7603 : "%qD used in its own initializer", r);
7604 311 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7605 : {
7606 219 : if (!CP_TYPE_CONST_P (type))
7607 190 : inform (DECL_SOURCE_LOCATION (r),
7608 : "%q#D is not const", r);
7609 29 : else if (CP_TYPE_VOLATILE_P (type))
7610 0 : inform (DECL_SOURCE_LOCATION (r),
7611 : "%q#D is volatile", r);
7612 29 : else if (!DECL_INITIAL (r)
7613 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7614 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7615 29 : inform (DECL_SOURCE_LOCATION (r),
7616 : "%qD was not initialized with a constant "
7617 : "expression", r);
7618 : else
7619 0 : gcc_unreachable ();
7620 : }
7621 92 : else if (TYPE_REF_P (type))
7622 9 : inform (DECL_SOURCE_LOCATION (r),
7623 : "%qD was not initialized with a constant "
7624 : "expression", r);
7625 : else
7626 : {
7627 83 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7628 83 : inform (DECL_SOURCE_LOCATION (r),
7629 : "%qD was not declared %<constexpr%>", r);
7630 : else
7631 0 : inform (DECL_SOURCE_LOCATION (r),
7632 : "%qD does not have integral or enumeration type",
7633 : r);
7634 : }
7635 334 : }
7636 :
7637 : /* Subroutine of cxx_eval_constant_expression.
7638 : Like cxx_eval_unary_expression, except for trinary expressions. */
7639 :
7640 : static tree
7641 16 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7642 : value_cat lval,
7643 : bool *non_constant_p, bool *overflow_p,
7644 : tree *jump_target)
7645 : {
7646 16 : int i;
7647 16 : tree args[3];
7648 16 : tree val;
7649 :
7650 37 : for (i = 0; i < 3; i++)
7651 : {
7652 30 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7653 : lval,
7654 : non_constant_p, overflow_p,
7655 : jump_target);
7656 30 : if (*jump_target)
7657 : return NULL_TREE;
7658 30 : VERIFY_CONSTANT (args[i]);
7659 : }
7660 :
7661 7 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7662 : args[0], args[1], args[2]);
7663 7 : if (val == NULL_TREE)
7664 : return t;
7665 7 : VERIFY_CONSTANT (val);
7666 : return val;
7667 : }
7668 :
7669 : /* True if T was declared in a function declared to be constexpr, and
7670 : therefore potentially constant in C++14. */
7671 :
7672 : bool
7673 91302247 : var_in_constexpr_fn (tree t)
7674 : {
7675 91302247 : tree ctx = DECL_CONTEXT (t);
7676 91302247 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7677 174252360 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7678 : }
7679 :
7680 : /* True if a function might be constexpr: either a function that was
7681 : declared constexpr, or a C++17 lambda op(). */
7682 :
7683 : bool
7684 744939322 : maybe_constexpr_fn (tree t)
7685 : {
7686 744939322 : return (DECL_DECLARED_CONSTEXPR_P (t)
7687 211976843 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7688 947488848 : || (flag_implicit_constexpr
7689 92 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7690 : }
7691 :
7692 : /* True if T was declared in a function that might be constexpr: either a
7693 : function that was declared constexpr, or a C++17 lambda op(). */
7694 :
7695 : bool
7696 73501435 : var_in_maybe_constexpr_fn (tree t)
7697 : {
7698 147002272 : return (DECL_FUNCTION_SCOPE_P (t)
7699 135413306 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7700 : }
7701 :
7702 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7703 : build_over_call we implement trivial copy of a class with tail padding using
7704 : assignment of character arrays, which is valid in normal code, but not in
7705 : constexpr evaluation. We don't need to worry about clobbering tail padding
7706 : in constexpr evaluation, so strip the type punning. */
7707 :
7708 : static void
7709 83304906 : maybe_simplify_trivial_copy (tree &target, tree &init)
7710 : {
7711 83304906 : if (TREE_CODE (target) == MEM_REF
7712 3658 : && TREE_CODE (init) == MEM_REF
7713 3558 : && TREE_TYPE (target) == TREE_TYPE (init)
7714 3558 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7715 83308464 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7716 : {
7717 3558 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7718 3558 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7719 : }
7720 83304906 : }
7721 :
7722 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7723 : of constant type. This does not check for 'mutable', so the
7724 : caller is expected to be mindful of that. */
7725 :
7726 : static bool
7727 447 : cref_has_const_field (tree ref)
7728 : {
7729 516 : while (TREE_CODE (ref) == COMPONENT_REF)
7730 : {
7731 507 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7732 : return true;
7733 69 : ref = TREE_OPERAND (ref, 0);
7734 : }
7735 : return false;
7736 : }
7737 :
7738 : /* Return true if we are modifying something that is const during constant
7739 : expression evaluation. CODE is the code of the statement, OBJ is the
7740 : object in question, MUTABLE_P is true if one of the subobjects were
7741 : declared mutable. */
7742 :
7743 : static bool
7744 73337159 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7745 : {
7746 : /* If this is initialization, there's no problem. */
7747 73337159 : if (code != MODIFY_EXPR)
7748 : return false;
7749 :
7750 : /* [basic.type.qualifier] "A const object is an object of type
7751 : const T or a non-mutable subobject of a const object." */
7752 20103092 : if (mutable_p)
7753 : return false;
7754 :
7755 20102356 : if (TREE_READONLY (obj))
7756 : return true;
7757 :
7758 20098542 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7759 : {
7760 : /* Although a COMPONENT_REF may have a const type, we should
7761 : only consider it modifying a const object when any of the
7762 : field components is const. This can happen when using
7763 : constructs such as const_cast<const T &>(m), making something
7764 : const even though it wasn't declared const. */
7765 30424 : if (TREE_CODE (obj) == COMPONENT_REF)
7766 447 : return cref_has_const_field (obj);
7767 : else
7768 : return true;
7769 : }
7770 :
7771 : return false;
7772 : }
7773 :
7774 : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7775 :
7776 : static tree
7777 83304906 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7778 : value_cat lval,
7779 : bool *non_constant_p, bool *overflow_p,
7780 : tree *jump_target)
7781 : {
7782 83304906 : constexpr_ctx new_ctx = *ctx;
7783 :
7784 83304906 : tree init = TREE_OPERAND (t, 1);
7785 :
7786 : /* First we figure out where we're storing to. */
7787 83304906 : tree target = TREE_OPERAND (t, 0);
7788 :
7789 83304906 : maybe_simplify_trivial_copy (target, init);
7790 :
7791 83304906 : tree type = TREE_TYPE (target);
7792 83304906 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7793 64723263 : if (preeval && !TREE_CLOBBER_P (init))
7794 : {
7795 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7796 64210549 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7797 17186352 : && TREE_CODE (init) == CALL_EXPR
7798 2495614 : && CALL_EXPR_FN (init) == NULL_TREE
7799 64210553 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7800 4 : return void_node;
7801 :
7802 : /* Evaluate the value to be stored without knowing what object it will be
7803 : stored in, so that any side-effects happen first. */
7804 64210545 : if (!SCALAR_TYPE_P (type))
7805 70144 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7806 64210545 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7807 : non_constant_p, overflow_p,
7808 : jump_target);
7809 64210543 : if (*jump_target)
7810 : return NULL_TREE;
7811 64210445 : if (*non_constant_p)
7812 : return t;
7813 : }
7814 :
7815 63886466 : bool evaluated = false;
7816 63886466 : if (lval == vc_glvalue)
7817 : {
7818 : /* If we want to return a reference to the target, we need to evaluate it
7819 : as a whole; otherwise, only evaluate the innermost piece to avoid
7820 : building up unnecessary *_REFs. */
7821 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7822 : non_constant_p, overflow_p,
7823 : jump_target);
7824 0 : evaluated = true;
7825 0 : if (*jump_target)
7826 : return NULL_TREE;
7827 0 : if (*non_constant_p)
7828 : return t;
7829 : }
7830 :
7831 : /* Find the underlying variable. */
7832 63886466 : releasing_vec refs;
7833 63886466 : tree object = NULL_TREE;
7834 : /* If we're modifying a const object, save it. */
7835 63886466 : tree const_object_being_modified = NULL_TREE;
7836 63886466 : bool mutable_p = false;
7837 : /* If we see a union, we can't ignore clobbers. */
7838 63886466 : int seen_union = 0;
7839 219997135 : for (tree probe = target; object == NULL_TREE; )
7840 : {
7841 156111041 : switch (TREE_CODE (probe))
7842 : {
7843 28338426 : case BIT_FIELD_REF:
7844 28338426 : case COMPONENT_REF:
7845 28338426 : case ARRAY_REF:
7846 28338426 : {
7847 28338426 : tree ob = TREE_OPERAND (probe, 0);
7848 28338426 : tree elt = TREE_OPERAND (probe, 1);
7849 28338426 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7850 : mutable_p = true;
7851 28338426 : if (TREE_CODE (probe) == ARRAY_REF)
7852 : {
7853 4488699 : elt = eval_and_check_array_index (ctx, probe, false,
7854 : non_constant_p, overflow_p,
7855 : jump_target);
7856 4488699 : if (*jump_target)
7857 66 : return NULL_TREE;
7858 4488699 : if (*non_constant_p)
7859 : return t;
7860 : }
7861 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7862 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7863 : the array isn't const. Instead, check "a" in the next iteration;
7864 : that will detect modifying "const int a[10]". */
7865 23849727 : else if (evaluated
7866 9451065 : && modifying_const_object_p (TREE_CODE (t), probe,
7867 : mutable_p)
7868 23850250 : && const_object_being_modified == NULL_TREE)
7869 : const_object_being_modified = probe;
7870 :
7871 : /* Track named member accesses for unions to validate modifications
7872 : that change active member. */
7873 28338360 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7874 14398662 : vec_safe_push (refs, probe);
7875 : else
7876 13939698 : vec_safe_push (refs, NULL_TREE);
7877 :
7878 28338360 : vec_safe_push (refs, elt);
7879 28338360 : vec_safe_push (refs, TREE_TYPE (probe));
7880 28338360 : probe = ob;
7881 28338360 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
7882 452292 : ++seen_union;
7883 : }
7884 28338360 : break;
7885 :
7886 16 : case REALPART_EXPR:
7887 16 : gcc_assert (refs->is_empty ());
7888 16 : vec_safe_push (refs, NULL_TREE);
7889 16 : vec_safe_push (refs, probe);
7890 16 : vec_safe_push (refs, TREE_TYPE (probe));
7891 16 : probe = TREE_OPERAND (probe, 0);
7892 16 : break;
7893 :
7894 17 : case IMAGPART_EXPR:
7895 17 : gcc_assert (refs->is_empty ());
7896 17 : vec_safe_push (refs, NULL_TREE);
7897 17 : vec_safe_push (refs, probe);
7898 17 : vec_safe_push (refs, TREE_TYPE (probe));
7899 17 : probe = TREE_OPERAND (probe, 0);
7900 17 : break;
7901 :
7902 127772582 : default:
7903 127772582 : if (evaluated)
7904 : object = probe;
7905 : else
7906 : {
7907 63886488 : tree pvar = tree_strip_any_location_wrapper (probe);
7908 63886488 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7909 : {
7910 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7911 : active union member. */
7912 55 : probe = DECL_VALUE_EXPR (pvar);
7913 55 : break;
7914 : }
7915 63886433 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7916 : non_constant_p, overflow_p,
7917 : jump_target);
7918 63886433 : evaluated = true;
7919 63886433 : if (*jump_target)
7920 : return NULL_TREE;
7921 63886433 : if (*non_constant_p)
7922 : return t;
7923 : }
7924 : break;
7925 : }
7926 : }
7927 :
7928 63886094 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7929 63886094 : && const_object_being_modified == NULL_TREE)
7930 63886094 : const_object_being_modified = object;
7931 :
7932 63886094 : if (DECL_P (object)
7933 63885212 : && TREE_CLOBBER_P (init)
7934 64406389 : && DECL_NAME (object) == heap_deleted_identifier)
7935 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
7936 : message later when operator delete is called. */
7937 15 : return void_node;
7938 :
7939 : /* And then find/build up our initializer for the path to the subobject
7940 : we're initializing. */
7941 63886079 : tree *valp;
7942 63886079 : if (DECL_P (object))
7943 63885197 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7944 : else
7945 882 : valp = NULL;
7946 63886079 : if (!valp)
7947 : {
7948 : /* A constant-expression cannot modify objects from outside the
7949 : constant-expression. */
7950 6203 : if (!ctx->quiet)
7951 : {
7952 29 : auto_diagnostic_group d;
7953 29 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
7954 : {
7955 0 : error ("modification of allocated storage after deallocation "
7956 : "is not a constant expression");
7957 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
7958 : }
7959 29 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
7960 : {
7961 14 : if (TREE_CLOBBER_P (init))
7962 6 : error ("destroying %qE outside its lifetime", object);
7963 : else
7964 8 : error ("modification of %qE outside its lifetime "
7965 : "is not a constant expression", object);
7966 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
7967 : }
7968 : else
7969 : {
7970 15 : if (TREE_CLOBBER_P (init))
7971 6 : error ("destroying %qE from outside current evaluation "
7972 : "is not a constant expression", object);
7973 : else
7974 9 : error ("modification of %qE from outside current evaluation "
7975 : "is not a constant expression", object);
7976 : }
7977 29 : }
7978 6203 : *non_constant_p = true;
7979 6203 : return t;
7980 : }
7981 :
7982 : /* Handle explicit end-of-lifetime. */
7983 63879876 : if (TREE_CLOBBER_P (init))
7984 : {
7985 520230 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
7986 520230 : && refs->is_empty ())
7987 : {
7988 125600 : ctx->global->destroy_value (object);
7989 125600 : return void_node;
7990 : }
7991 :
7992 390020 : if (!seen_union && !*valp
7993 397892 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
7994 3228 : return void_node;
7995 :
7996 : /* Ending the lifetime of a const object is OK. */
7997 : const_object_being_modified = NULL_TREE;
7998 : }
7999 :
8000 63751048 : type = TREE_TYPE (object);
8001 63751048 : bool no_zero_init = true;
8002 63751048 : bool zero_padding_bits = false;
8003 :
8004 127502096 : auto_vec<tree *> ctors;
8005 127502096 : releasing_vec indexes;
8006 127502096 : auto_vec<int> index_pos_hints;
8007 63751048 : bool activated_union_member_p = false;
8008 63751048 : bool empty_base = false;
8009 91843252 : while (!refs->is_empty ())
8010 : {
8011 28324261 : if (*valp == NULL_TREE)
8012 : {
8013 3374828 : *valp = build_constructor (type, NULL);
8014 3374828 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8015 3374828 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8016 : }
8017 24949433 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8018 24949433 : TREE_CODE (*valp) == STRING_CST)
8019 : {
8020 : /* An array was initialized with a string constant, and now
8021 : we're writing into one of its elements. Explode the
8022 : single initialization into a set of element
8023 : initializations. */
8024 10892 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8025 :
8026 10892 : tree string = *valp;
8027 10892 : tree elt_type = TREE_TYPE (type);
8028 10892 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8029 10892 : / TYPE_PRECISION (char_type_node));
8030 10892 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8031 10892 : tree ary_ctor = build_constructor (type, NULL);
8032 :
8033 10892 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8034 21938 : for (unsigned ix = 0; ix != num_elts; ix++)
8035 : {
8036 11046 : constructor_elt elt =
8037 : {
8038 11046 : build_int_cst (size_type_node, ix),
8039 11046 : extract_string_elt (string, chars_per_elt, ix)
8040 11046 : };
8041 11046 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8042 : }
8043 :
8044 10892 : *valp = ary_ctor;
8045 : }
8046 :
8047 28324261 : enum tree_code code = TREE_CODE (type);
8048 28324261 : tree reftype = refs->pop();
8049 28324261 : tree index = refs->pop();
8050 28324261 : bool is_access_expr = refs->pop() != NULL_TREE;
8051 :
8052 28324261 : if (code == COMPLEX_TYPE)
8053 : {
8054 33 : if (TREE_CODE (*valp) == COMPLEX_CST)
8055 29 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8056 29 : TREE_IMAGPART (*valp));
8057 4 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8058 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8059 6 : && CONSTRUCTOR_NO_CLEARING (*valp))
8060 : {
8061 2 : tree r = build_constructor (reftype, NULL);
8062 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8063 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8064 : }
8065 33 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8066 33 : ctors.safe_push (valp);
8067 33 : vec_safe_push (indexes, index);
8068 33 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8069 33 : gcc_checking_assert (refs->is_empty ());
8070 : type = reftype;
8071 231139 : break;
8072 : }
8073 :
8074 : /* If the value of object is already zero-initialized, any new ctors for
8075 : subobjects will also be zero-initialized. Similarly with zeroing of
8076 : padding bits. */
8077 28324228 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8078 28324228 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8079 :
8080 28324228 : if (code == RECORD_TYPE && is_empty_field (index))
8081 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8082 : have no data and might have an offset lower than previously declared
8083 : fields, which confuses the middle-end. The code below will notice
8084 : that we don't have a CONSTRUCTOR for our inner target and just
8085 : return init. */
8086 : {
8087 : empty_base = true;
8088 : break;
8089 : }
8090 :
8091 : /* If a union is zero-initialized, its first non-static named data member
8092 : is zero-initialized (and therefore active). */
8093 28093122 : if (code == UNION_TYPE
8094 28093122 : && !no_zero_init
8095 28093122 : && CONSTRUCTOR_NELTS (*valp) == 0)
8096 77 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8097 77 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8098 :
8099 : /* Check for implicit change of active member for a union. */
8100 :
8101 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8102 : now allow initializing an array element to activate the array. */
8103 28127597 : auto only_array_refs = [](const releasing_vec &refs)
8104 : {
8105 34479 : for (unsigned i = 1; i < refs->length(); i += 3)
8106 34 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8107 : return false;
8108 : return true;
8109 : };
8110 :
8111 28093122 : if (code == UNION_TYPE
8112 451587 : && (CONSTRUCTOR_NELTS (*valp) == 0
8113 140943 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8114 : /* An INIT_EXPR of the last member in an access chain is always OK,
8115 : but still check implicit change of members earlier on; see
8116 : cpp2a/constexpr-union6.C. */
8117 28406329 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8118 : {
8119 278762 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8120 278762 : tree inner = strip_array_types (reftype);
8121 :
8122 278762 : if (has_active_member && cxx_dialect < cxx20)
8123 : {
8124 58 : if (!ctx->quiet)
8125 19 : error_at (cp_expr_loc_or_input_loc (t),
8126 : "change of the active member of a union "
8127 : "from %qD to %qD is not a constant expression "
8128 : "before C++20",
8129 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8130 : index);
8131 58 : *non_constant_p = true;
8132 : }
8133 278704 : else if (!is_access_expr
8134 25652 : || (TREE_CLOBBER_P (init)
8135 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8136 304356 : || (TREE_CODE (t) == MODIFY_EXPR
8137 25640 : && CLASS_TYPE_P (inner)
8138 15 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8139 : {
8140 : /* Diagnose changing active union member after initialization
8141 : without a valid member access expression, as described in
8142 : [class.union.general] p5. */
8143 253061 : if (!ctx->quiet)
8144 : {
8145 33 : auto_diagnostic_group d;
8146 33 : if (has_active_member)
8147 24 : error_at (cp_expr_loc_or_input_loc (t),
8148 : "accessing %qD member instead of initialized "
8149 : "%qD member in constant expression",
8150 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8151 : else
8152 9 : error_at (cp_expr_loc_or_input_loc (t),
8153 : "accessing uninitialized member %qD",
8154 : index);
8155 33 : if (is_access_expr)
8156 3 : inform (DECL_SOURCE_LOCATION (index),
8157 : "%qD does not implicitly begin its lifetime "
8158 : "because %qT does not have a non-deleted "
8159 : "trivial default constructor, use "
8160 : "%<std::construct_at%> instead",
8161 : index, inner);
8162 : else
8163 30 : inform (DECL_SOURCE_LOCATION (index),
8164 : "initializing %qD requires a member access "
8165 : "expression as the left operand of the assignment",
8166 : index);
8167 33 : }
8168 253061 : *non_constant_p = true;
8169 : }
8170 25643 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8171 : {
8172 : /* Diagnose changing the active union member while the union
8173 : is in the process of being initialized. */
8174 18 : if (!ctx->quiet)
8175 6 : error_at (cp_expr_loc_or_input_loc (t),
8176 : "change of the active member of a union "
8177 : "from %qD to %qD during initialization",
8178 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8179 : index);
8180 18 : *non_constant_p = true;
8181 : }
8182 : no_zero_init = true;
8183 : }
8184 :
8185 28093122 : ctors.safe_push (valp);
8186 28093122 : vec_safe_push (indexes, index);
8187 :
8188 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8189 : uninitialized. */
8190 26854370 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8191 887137 : && CONSTRUCTOR_NO_CLEARING (*valp)
8192 28883811 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8193 28093122 : constructor_elt *cep
8194 28093122 : = get_or_insert_ctor_field (*valp, index, pos);
8195 28093122 : if (cep == nullptr)
8196 918 : return void_node;
8197 28092204 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8198 :
8199 28092204 : if (code == UNION_TYPE)
8200 : {
8201 451587 : activated_union_member_p = true;
8202 451587 : --seen_union;
8203 : }
8204 :
8205 28092204 : valp = &cep->value;
8206 28092204 : type = reftype;
8207 : }
8208 :
8209 : /* Change an "as-base" clobber to the real type;
8210 : we don't need to worry about padding in constexpr. */
8211 63750130 : tree itype = initialized_type (init);
8212 63750130 : if (IS_FAKE_BASE_TYPE (itype))
8213 1949 : itype = TYPE_CONTEXT (itype);
8214 :
8215 : /* For initialization of an empty base, the original target will be
8216 : *(base*)this, evaluation of which resolves to the object
8217 : argument, which has the derived type rather than the base type. */
8218 127269154 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8219 63519024 : (itype, type)))
8220 : {
8221 12210 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8222 : empty_base = true;
8223 : }
8224 :
8225 : /* Detect modifying a constant object in constexpr evaluation.
8226 : We have found a const object that is being modified. Figure out
8227 : if we need to issue an error. Consider
8228 :
8229 : struct A {
8230 : int n;
8231 : constexpr A() : n(1) { n = 2; } // #1
8232 : };
8233 : struct B {
8234 : const A a;
8235 : constexpr B() { a.n = 3; } // #2
8236 : };
8237 : constexpr B b{};
8238 :
8239 : #1 is OK, since we're modifying an object under construction, but
8240 : #2 is wrong, since "a" is const and has been fully constructed.
8241 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8242 : which means that the object is read-only. For the example above, the
8243 : *ctors stack at the point of #2 will look like:
8244 :
8245 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
8246 : ctors[1] = {.n=2} TREE_READONLY = 1
8247 :
8248 : and we're modifying "b.a", so we search the stack and see if the
8249 : constructor for "b.a" has already run. */
8250 63750130 : if (const_object_being_modified)
8251 : {
8252 33154 : bool fail = false;
8253 33154 : tree const_objtype
8254 33154 : = strip_array_types (TREE_TYPE (const_object_being_modified));
8255 33154 : if (!CLASS_TYPE_P (const_objtype))
8256 : fail = true;
8257 : else
8258 : {
8259 : /* [class.ctor]p5 "A constructor can be invoked for a const,
8260 : volatile, or const volatile object. const and volatile
8261 : semantics are not applied on an object under construction.
8262 : They come into effect when the constructor for the most
8263 : derived object ends." */
8264 99570 : for (tree *elt : ctors)
8265 33472 : if (same_type_ignoring_top_level_qualifiers_p
8266 33472 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
8267 : {
8268 33049 : fail = TREE_READONLY (*elt);
8269 33049 : break;
8270 : }
8271 : }
8272 33049 : if (fail)
8273 : {
8274 198 : if (!ctx->quiet)
8275 63 : modifying_const_object_error (t, const_object_being_modified);
8276 198 : *non_constant_p = true;
8277 198 : return t;
8278 : }
8279 : }
8280 :
8281 63749932 : if (!preeval)
8282 : {
8283 : /* We're handling an INIT_EXPR of class type, so the value of the
8284 : initializer can depend on the object it's initializing. */
8285 :
8286 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
8287 : wants to modify it. */
8288 18573551 : if (*valp == NULL_TREE)
8289 : {
8290 17898727 : *valp = build_constructor (type, NULL);
8291 17898727 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8292 17898727 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8293 : }
8294 18573551 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8295 18573551 : new_ctx.object = target;
8296 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8297 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8298 : expansion of those trees uses ctx instead. */
8299 18573551 : if (TREE_CODE (init) == TARGET_EXPR)
8300 3392855 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8301 3392855 : init = tinit;
8302 18573551 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8303 : non_constant_p, overflow_p,
8304 : jump_target);
8305 18573551 : if (*jump_target)
8306 : return NULL_TREE;
8307 : /* The hash table might have moved since the get earlier, and the
8308 : initializer might have mutated the underlying CONSTRUCTORs, so we must
8309 : recompute VALP. */
8310 18573490 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8311 21751184 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8312 : {
8313 3177694 : ctors[i] = valp;
8314 3177694 : constructor_elt *cep
8315 3177694 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8316 3177694 : valp = &cep->value;
8317 : }
8318 : }
8319 :
8320 63749871 : if (*non_constant_p)
8321 : return t;
8322 :
8323 : /* Don't share a CONSTRUCTOR that might be changed later. */
8324 59941380 : init = unshare_constructor (init);
8325 :
8326 59941380 : gcc_checking_assert (!*valp
8327 : || *valp == void_node
8328 : || (same_type_ignoring_top_level_qualifiers_p
8329 : (TREE_TYPE (*valp), type)));
8330 59941380 : if (empty_base)
8331 : {
8332 : /* Just evaluate the initializer and return, since there's no actual data
8333 : to store, and we didn't build a CONSTRUCTOR. */
8334 235824 : if (!*valp)
8335 : {
8336 : /* But do make sure we have something in *valp. */
8337 0 : *valp = build_constructor (type, nullptr);
8338 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8339 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8340 : }
8341 : }
8342 59705556 : else if (TREE_CLOBBER_P (init))
8343 : {
8344 390484 : if (AGGREGATE_TYPE_P (type))
8345 : {
8346 262248 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
8347 262225 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8348 : else
8349 23 : *valp = build_constructor (type, nullptr);
8350 262248 : TREE_CONSTANT (*valp) = true;
8351 262248 : TREE_SIDE_EFFECTS (*valp) = false;
8352 262248 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8353 262248 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8354 : }
8355 : else
8356 128236 : *valp = void_node;
8357 : }
8358 59315072 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8359 14845472 : && TREE_CODE (init) == CONSTRUCTOR)
8360 : {
8361 : /* An outer ctx->ctor might be pointing to *valp, so replace
8362 : its contents. */
8363 3861657 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8364 3861657 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8365 3861657 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8366 11584971 : CONSTRUCTOR_NO_CLEARING (*valp)
8367 3861657 : = CONSTRUCTOR_NO_CLEARING (init);
8368 11584971 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8369 3861657 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8370 : }
8371 : else
8372 55453415 : *valp = init;
8373 :
8374 : /* After initialization, 'const' semantics apply to the value of the
8375 : object. Make a note of this fact by marking the CONSTRUCTOR
8376 : TREE_READONLY. */
8377 59941380 : if (TREE_CODE (t) == INIT_EXPR
8378 45126053 : && !empty_base
8379 44890229 : && TREE_CODE (*valp) == CONSTRUCTOR
8380 63739896 : && TYPE_READONLY (type))
8381 : {
8382 20790 : tree target_type = TREE_TYPE (target);
8383 20790 : if (IS_FAKE_BASE_TYPE (target_type))
8384 0 : target_type = TYPE_CONTEXT (target_type);
8385 20790 : if (INDIRECT_REF_P (target)
8386 35853 : && (is_this_parameter
8387 15063 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8388 : /* We've just initialized '*this' (perhaps via the target
8389 : constructor of a delegating constructor). Leave it up to the
8390 : caller that set 'this' to set TREE_READONLY appropriately. */
8391 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8392 : (target_type, type) || empty_base);
8393 : else
8394 20767 : TREE_READONLY (*valp) = true;
8395 : }
8396 :
8397 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8398 : CONSTRUCTORs, if any. */
8399 59941380 : bool c = TREE_CONSTANT (init);
8400 59941380 : bool s = TREE_SIDE_EFFECTS (init);
8401 59941380 : if (!indexes->is_empty ())
8402 : {
8403 16705558 : tree last = indexes->last ();
8404 16705558 : if (TREE_CODE (last) == REALPART_EXPR
8405 16705558 : || TREE_CODE (last) == IMAGPART_EXPR)
8406 : {
8407 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8408 : possible. */
8409 33 : tree *cexpr = ctors.last ();
8410 33 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8411 33 : TREE_OPERAND (*cexpr, 0),
8412 33 : TREE_OPERAND (*cexpr, 1)))
8413 31 : *cexpr = c;
8414 : else
8415 : {
8416 4 : TREE_CONSTANT (*cexpr)
8417 2 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8418 2 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8419 4 : TREE_SIDE_EFFECTS (*cexpr)
8420 4 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8421 2 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8422 : }
8423 33 : c = TREE_CONSTANT (*cexpr);
8424 33 : s = TREE_SIDE_EFFECTS (*cexpr);
8425 : }
8426 : }
8427 59941380 : if (!c || s || activated_union_member_p)
8428 39699342 : for (tree *elt : ctors)
8429 : {
8430 8341654 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8431 0 : continue;
8432 8341654 : if (!c)
8433 7528406 : TREE_CONSTANT (*elt) = false;
8434 8341654 : if (s)
8435 0 : TREE_SIDE_EFFECTS (*elt) = true;
8436 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8437 : this union. */
8438 8341654 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8439 198230 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8440 : }
8441 :
8442 59941380 : if (lval)
8443 : return target;
8444 : else
8445 351565 : return init;
8446 63886466 : }
8447 :
8448 : /* Evaluate a ++ or -- expression. */
8449 :
8450 : static tree
8451 5832508 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8452 : value_cat lval,
8453 : bool *non_constant_p, bool *overflow_p,
8454 : tree *jump_target)
8455 : {
8456 5832508 : enum tree_code code = TREE_CODE (t);
8457 5832508 : tree type = TREE_TYPE (t);
8458 5832508 : tree op = TREE_OPERAND (t, 0);
8459 5832508 : tree offset = TREE_OPERAND (t, 1);
8460 5832508 : gcc_assert (TREE_CONSTANT (offset));
8461 :
8462 : /* OFFSET is constant, but perhaps not constant enough. We need to
8463 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8464 5832508 : offset = fold_simple (offset);
8465 :
8466 : /* The operand as an lvalue. */
8467 5832508 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8468 : non_constant_p, overflow_p,
8469 : jump_target);
8470 5832508 : if (*jump_target)
8471 : return NULL_TREE;
8472 :
8473 : /* The operand as an rvalue. */
8474 5832508 : tree val
8475 5832508 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8476 : non_constant_p, overflow_p,
8477 : jump_target);
8478 5832508 : if (*jump_target)
8479 : return NULL_TREE;
8480 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8481 : a local array in a constexpr function. */
8482 5832508 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8483 2682664 : if (!ptr)
8484 2682664 : VERIFY_CONSTANT (val);
8485 :
8486 : /* The modified value. */
8487 5706863 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8488 5706863 : tree mod;
8489 5706863 : if (INDIRECT_TYPE_P (type))
8490 : {
8491 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8492 3149844 : offset = convert_to_ptrofftype (offset);
8493 3149844 : if (!inc)
8494 52348 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8495 3149844 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8496 : }
8497 2557019 : else if (c_promoting_integer_type_p (type)
8498 5085 : && !TYPE_UNSIGNED (type)
8499 2557143 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8500 : {
8501 124 : offset = fold_convert (integer_type_node, offset);
8502 124 : mod = fold_convert (integer_type_node, val);
8503 153 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8504 : mod, offset);
8505 124 : mod = fold_convert (type, t);
8506 124 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8507 9 : TREE_OVERFLOW (mod) = false;
8508 : }
8509 : else
8510 2863067 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8511 5706863 : if (!ptr)
8512 2557019 : VERIFY_CONSTANT (mod);
8513 :
8514 : /* Storing the modified value. */
8515 8805907 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8516 : MODIFY_EXPR, type, op, mod);
8517 5706863 : mod = cxx_eval_constant_expression (ctx, store, lval,
8518 : non_constant_p, overflow_p,
8519 : jump_target);
8520 5706863 : ggc_free (store);
8521 5706863 : if (*jump_target)
8522 : return NULL_TREE;
8523 5706863 : if (*non_constant_p)
8524 : return t;
8525 :
8526 : /* And the value of the expression. */
8527 5576656 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8528 : /* Prefix ops are lvalues, but the caller might want an rvalue;
8529 : lval has already been taken into account in the store above. */
8530 : return mod;
8531 : else
8532 : /* Postfix ops are rvalues. */
8533 351072 : return val;
8534 : }
8535 :
8536 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8537 : STMT matches *jump_target. If we're looking for a case label and we see
8538 : the default label, note it in ctx->css_state. */
8539 :
8540 : static bool
8541 336197 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8542 : {
8543 336197 : switch (TREE_CODE (*jump_target))
8544 : {
8545 90 : case LABEL_DECL:
8546 90 : if (TREE_CODE (stmt) == LABEL_EXPR
8547 90 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8548 : return true;
8549 : break;
8550 :
8551 333998 : case INTEGER_CST:
8552 333998 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8553 : {
8554 333982 : gcc_assert (ctx->css_state != NULL);
8555 333982 : if (!CASE_LOW (stmt))
8556 : {
8557 : /* default: should appear just once in a SWITCH_EXPR
8558 : body (excluding nested SWITCH_EXPR). */
8559 55316 : gcc_assert (*ctx->css_state != css_default_seen);
8560 : /* When evaluating SWITCH_EXPR body for the second time,
8561 : return true for the default: label. */
8562 55316 : if (*ctx->css_state == css_default_processing)
8563 : return true;
8564 27670 : *ctx->css_state = css_default_seen;
8565 : }
8566 278666 : else if (CASE_HIGH (stmt))
8567 : {
8568 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8569 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8570 : return true;
8571 : }
8572 278646 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8573 : return true;
8574 : }
8575 : break;
8576 :
8577 : case BREAK_STMT:
8578 : case CONTINUE_STMT:
8579 : /* These two are handled directly in cxx_eval_loop_expr by testing
8580 : breaks (jump_target) or continues (jump_target). */
8581 : break;
8582 :
8583 : case VAR_DECL:
8584 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8585 : and other places by testing throws (jump_target). */
8586 : break;
8587 :
8588 0 : default:
8589 0 : gcc_unreachable ();
8590 : }
8591 : return false;
8592 : }
8593 :
8594 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8595 : semantics, for switch, break, continue, and return. */
8596 :
8597 : static tree
8598 46033774 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8599 : bool *non_constant_p, bool *overflow_p,
8600 : tree *jump_target)
8601 : {
8602 : /* In a statement-expression we want to return the last value.
8603 : For empty statement expression return void_node. */
8604 46033774 : tree r = void_node;
8605 107633233 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8606 : {
8607 89701614 : tree stmt = *i;
8608 :
8609 : /* We've found a continue, so skip everything until we reach
8610 : the label its jumping to. */
8611 89701614 : if (continues (jump_target))
8612 : {
8613 2199 : if (label_matches (ctx, jump_target, stmt))
8614 : /* Found it. */
8615 18 : *jump_target = NULL_TREE;
8616 : else
8617 2181 : continue;
8618 : }
8619 89699433 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8620 11808984 : continue;
8621 :
8622 77890449 : value_cat lval = vc_discard;
8623 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8624 110121535 : if (tsi_one_before_end_p (i)
8625 32232291 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8626 : lval = vc_prvalue;
8627 :
8628 77890449 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8629 : non_constant_p, overflow_p,
8630 : jump_target);
8631 77890447 : if (*non_constant_p)
8632 : break;
8633 60833057 : if (returns (jump_target)
8634 49810125 : || breaks (jump_target)
8635 61599459 : || throws (jump_target))
8636 : break;
8637 : }
8638 46033772 : return r;
8639 : }
8640 :
8641 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8642 : semantics; continue semantics are covered by cxx_eval_statement_list. */
8643 :
8644 : static tree
8645 5467081 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8646 : bool *non_constant_p, bool *overflow_p,
8647 : tree *jump_target)
8648 : {
8649 5467081 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8650 5467081 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8651 5467081 : unsigned cond_cleanup_depth = 0;
8652 5467081 : int count = 0;
8653 5467081 : switch (TREE_CODE (t))
8654 : {
8655 1572 : case LOOP_EXPR:
8656 1572 : body = LOOP_EXPR_BODY (t);
8657 1572 : break;
8658 4754006 : case DO_STMT:
8659 4754006 : body = DO_BODY (t);
8660 4754006 : cond = DO_COND (t);
8661 4754006 : break;
8662 181986 : case WHILE_STMT:
8663 181986 : body = WHILE_BODY (t);
8664 181986 : cond = WHILE_COND (t);
8665 181986 : cond_prep = WHILE_COND_PREP (t);
8666 181986 : cond_cleanup = WHILE_COND_CLEANUP (t);
8667 181986 : count = -1;
8668 181986 : break;
8669 529517 : case FOR_STMT:
8670 529517 : if (FOR_INIT_STMT (t))
8671 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8672 : non_constant_p, overflow_p, jump_target);
8673 529517 : if (*non_constant_p)
8674 : return NULL_TREE;
8675 529517 : body = FOR_BODY (t);
8676 529517 : cond = FOR_COND (t);
8677 529517 : expr = FOR_EXPR (t);
8678 529517 : cond_prep = FOR_COND_PREP (t);
8679 529517 : cond_cleanup = FOR_COND_CLEANUP (t);
8680 529517 : count = -1;
8681 529517 : break;
8682 0 : default:
8683 0 : gcc_unreachable ();
8684 : }
8685 5467081 : if (cond_prep)
8686 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8687 25370269 : auto cleanup_cond = [&] {
8688 : /* Clean up the condition variable after each iteration. */
8689 19903188 : if (cond_cleanup_depth && !*non_constant_p)
8690 : {
8691 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8692 105 : tree s = BIND_EXPR_BODY (cond_prep);
8693 105 : unsigned i;
8694 210 : for (i = cond_cleanup_depth; i; --i)
8695 : {
8696 105 : tree_stmt_iterator iter = tsi_last (s);
8697 105 : s = tsi_stmt (iter);
8698 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8699 105 : s = CLEANUP_BODY (s);
8700 : }
8701 105 : tree c;
8702 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8703 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8704 : overflow_p, jump_target);
8705 105 : }
8706 19903188 : if (cond_prep)
8707 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8708 222 : decl; decl = DECL_CHAIN (decl))
8709 111 : destroy_value_checked (ctx, decl, non_constant_p);
8710 5467081 : };
8711 15149594 : do
8712 : {
8713 15149594 : if (count != -1)
8714 : {
8715 14438091 : if (body)
8716 14438091 : cxx_eval_constant_expression (ctx, body, vc_discard,
8717 : non_constant_p, overflow_p,
8718 : jump_target);
8719 14438091 : if (breaks (jump_target))
8720 : {
8721 1984 : *jump_target = NULL_TREE;
8722 1984 : break;
8723 : }
8724 :
8725 14436107 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8726 586 : *jump_target = NULL_TREE;
8727 :
8728 14436107 : if (expr)
8729 4003364 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8730 : non_constant_p, overflow_p,
8731 : jump_target);
8732 14436107 : cleanup_cond ();
8733 : }
8734 :
8735 15147610 : if (cond_prep)
8736 : {
8737 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8738 222 : decl; decl = DECL_CHAIN (decl))
8739 111 : ctx->global->clear_value (decl);
8740 111 : if (cond_cleanup)
8741 : {
8742 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8743 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8744 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8745 : at the end of those STATEMENT_LISTs. */
8746 111 : cond_cleanup_depth = 0;
8747 111 : tree s = BIND_EXPR_BODY (cond_prep);
8748 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8749 222 : depth; --depth)
8750 : {
8751 111 : for (tree_stmt_iterator i = tsi_start (s);
8752 321 : !tsi_end_p (i); ++i)
8753 : {
8754 321 : tree stmt = *i;
8755 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8756 0 : continue;
8757 321 : if (tsi_one_before_end_p (i))
8758 : {
8759 : /* The last statement in the STATEMENT_LIST
8760 : has to be a CLEANUP_STMT (verified in
8761 : finish_loop_cond_prep). We want to
8762 : evaluate just its CLEANUP_BODY part but not
8763 : CLEANUP_EXPR part just yet. */
8764 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8765 : /* If the CLEANUP_STMT is not actually to be
8766 : evaluated, don't increment cond_cleanup_depth
8767 : so that we don't evaluate the CLEANUP_EXPR
8768 : for it later either. */
8769 105 : if (*jump_target)
8770 : {
8771 : depth = 1;
8772 : break;
8773 : }
8774 105 : ++cond_cleanup_depth;
8775 : /* If not in the innermost one, next iteration
8776 : will handle CLEANUP_BODY similarly. */
8777 105 : if (depth > 1)
8778 : {
8779 0 : s = CLEANUP_BODY (stmt);
8780 0 : break;
8781 : }
8782 : /* The innermost one can be evaluated normally. */
8783 105 : cxx_eval_constant_expression (ctx,
8784 105 : CLEANUP_BODY (stmt),
8785 : vc_discard,
8786 : non_constant_p,
8787 : overflow_p,
8788 : jump_target);
8789 105 : break;
8790 : }
8791 : /* And so should be evaluated statements which aren't
8792 : last in the STATEMENT_LIST. */
8793 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8794 : non_constant_p, overflow_p,
8795 : jump_target);
8796 216 : if (*non_constant_p
8797 210 : || returns (jump_target)
8798 210 : || breaks (jump_target)
8799 210 : || continues (jump_target)
8800 531 : || throws (jump_target))
8801 : {
8802 : depth = 1;
8803 : break;
8804 : }
8805 : }
8806 : }
8807 : }
8808 : else
8809 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8810 : vc_discard, non_constant_p,
8811 : overflow_p, jump_target);
8812 : }
8813 :
8814 15147610 : if (cond)
8815 : {
8816 15145271 : tree res
8817 15145271 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8818 : non_constant_p, overflow_p,
8819 : jump_target);
8820 15145271 : if (res)
8821 : {
8822 15096406 : if (verify_constant (res, ctx->quiet, non_constant_p,
8823 : overflow_p))
8824 : break;
8825 14344578 : if (integer_zerop (res))
8826 : break;
8827 : }
8828 : else
8829 48865 : gcc_assert (*jump_target);
8830 : }
8831 :
8832 9731626 : if (++count >= constexpr_loop_limit)
8833 : {
8834 27 : if (!ctx->quiet)
8835 9 : error_at (cp_expr_loc_or_input_loc (t),
8836 : "%<constexpr%> loop iteration count exceeds limit of %d "
8837 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8838 : constexpr_loop_limit);
8839 27 : *non_constant_p = true;
8840 27 : break;
8841 : }
8842 : }
8843 29145907 : while (!returns (jump_target)
8844 9682709 : && !breaks (jump_target)
8845 9682709 : && !continues (jump_target)
8846 9682802 : && (!switches (jump_target) || count == 0)
8847 9682679 : && !throws (jump_target)
8848 19463359 : && !*non_constant_p);
8849 :
8850 5467081 : cleanup_cond ();
8851 :
8852 5467081 : return NULL_TREE;
8853 : }
8854 :
8855 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8856 : semantics. */
8857 :
8858 : static tree
8859 34548 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8860 : bool *non_constant_p, bool *overflow_p,
8861 : tree *jump_target)
8862 : {
8863 34548 : tree cond
8864 34548 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8865 34548 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8866 : non_constant_p, overflow_p,
8867 : jump_target);
8868 34548 : if (*jump_target)
8869 : return NULL_TREE;
8870 34548 : VERIFY_CONSTANT (cond);
8871 34305 : if (TREE_CODE (cond) != INTEGER_CST)
8872 : {
8873 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8874 : switch condition even if it's constant enough for other things
8875 : (c++/113545). */
8876 0 : gcc_checking_assert (ctx->quiet);
8877 0 : *non_constant_p = true;
8878 0 : return t;
8879 : }
8880 :
8881 34305 : *jump_target = cond;
8882 :
8883 34305 : tree body
8884 34305 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8885 34305 : constexpr_ctx new_ctx = *ctx;
8886 34305 : constexpr_switch_state css = css_default_not_seen;
8887 34305 : new_ctx.css_state = &css;
8888 34305 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8889 : non_constant_p, overflow_p, jump_target);
8890 61985 : if (switches (jump_target) && css == css_default_seen)
8891 : {
8892 : /* If the SWITCH_EXPR body has default: label, process it once again,
8893 : this time instructing label_matches to return true for default:
8894 : label on switches (jump_target). */
8895 27646 : css = css_default_processing;
8896 27646 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8897 : non_constant_p, overflow_p, jump_target);
8898 : }
8899 34305 : if (breaks (jump_target) || switches (jump_target))
8900 19942 : *jump_target = NULL_TREE;
8901 : return NULL_TREE;
8902 : }
8903 :
8904 : /* Find the object of TYPE under initialization in CTX. */
8905 :
8906 : static tree
8907 16621 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8908 : {
8909 16621 : if (!ctx)
8910 : return NULL_TREE;
8911 :
8912 : /* Prefer the outermost matching object, but don't cross
8913 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8914 16294 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8915 505 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8916 : return outer_ob;
8917 :
8918 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8919 : can is a minor optimization. */
8920 16116 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8921 300 : return ctx->ctor;
8922 :
8923 15816 : if (!ctx->object)
8924 : return NULL_TREE;
8925 :
8926 : /* Since an object cannot have a field of its own type, we can search outward
8927 : from ctx->object to find the unique containing object of TYPE. */
8928 : tree ob = ctx->object;
8929 15804 : while (ob)
8930 : {
8931 15804 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
8932 : break;
8933 219 : if (handled_component_p (ob))
8934 217 : ob = TREE_OPERAND (ob, 0);
8935 : else
8936 : ob = NULL_TREE;
8937 : }
8938 :
8939 : return ob;
8940 : }
8941 :
8942 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
8943 : true, we're checking a constexpr function body. */
8944 :
8945 : static void
8946 35 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
8947 : {
8948 35 : auto_diagnostic_group d;
8949 35 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
8950 : "constant expression"))
8951 35 : inform (loc, "only unevaluated inline assembly is allowed in a "
8952 : "%<constexpr%> function in C++20");
8953 35 : }
8954 :
8955 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
8956 : context; maybe complain about that. */
8957 :
8958 : static void
8959 107825386 : maybe_warn_about_constant_value (location_t loc, tree decl)
8960 : {
8961 107825386 : static bool explained = false;
8962 107825386 : if (cxx_dialect >= cxx17
8963 107663948 : && warn_interference_size
8964 107663948 : && !OPTION_SET_P (param_destruct_interfere_size)
8965 107663948 : && DECL_CONTEXT (decl) == std_node
8966 24099407 : && DECL_NAME (decl)
8967 24099401 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
8968 12 : && (LOCATION_FILE (input_location) != main_input_filename
8969 6 : || module_exporting_p ())
8970 107825389 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
8971 107825395 : && !explained)
8972 : {
8973 6 : explained = true;
8974 6 : inform (loc, "its value can vary between compiler versions or "
8975 : "with different %<-mtune%> or %<-mcpu%> flags");
8976 6 : inform (loc, "if this use is part of a public ABI, change it to "
8977 : "instead use a constant variable you define");
8978 6 : inform (loc, "the default value for the current CPU tuning "
8979 : "is %d bytes", param_destruct_interfere_size);
8980 6 : inform (loc, "you can stabilize this value with %<--param "
8981 : "hardware_destructive_interference_size=%d%>, or disable "
8982 : "this warning with %<-Wno-interference-size%>",
8983 : param_destruct_interfere_size);
8984 : }
8985 107825386 : }
8986 :
8987 : /* For element type ELT_TYPE, return the appropriate type of the heap object
8988 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
8989 : in bytes. If COOKIE_SIZE is NULL, return array type
8990 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
8991 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
8992 : where N is computed such that the size of the struct fits into FULL_SIZE.
8993 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
8994 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
8995 : will be also 0 and so it is not possible to determine the actual array
8996 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
8997 : expression evaluation of subexpressions of ARG_SIZE. */
8998 :
8999 : static tree
9000 70705 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9001 : tree cookie_size, tree full_size, tree arg_size,
9002 : bool *non_constant_p, bool *overflow_p,
9003 : tree *jump_target)
9004 : {
9005 70705 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9006 70705 : gcc_assert (tree_fits_uhwi_p (full_size));
9007 70705 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9008 70705 : if (arg_size)
9009 : {
9010 9 : STRIP_NOPS (arg_size);
9011 9 : if (cookie_size)
9012 : {
9013 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9014 : arg_size = NULL_TREE;
9015 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9016 0 : && tree_int_cst_equal (cookie_size,
9017 0 : TREE_OPERAND (arg_size, 0)))
9018 : {
9019 0 : arg_size = TREE_OPERAND (arg_size, 1);
9020 0 : STRIP_NOPS (arg_size);
9021 : }
9022 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9023 0 : && tree_int_cst_equal (cookie_size,
9024 0 : TREE_OPERAND (arg_size, 1)))
9025 : {
9026 0 : arg_size = TREE_OPERAND (arg_size, 0);
9027 0 : STRIP_NOPS (arg_size);
9028 : }
9029 : else
9030 : arg_size = NULL_TREE;
9031 : }
9032 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9033 : {
9034 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9035 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9036 9 : if (integer_zerop (op0))
9037 9 : arg_size
9038 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9039 : non_constant_p, overflow_p,
9040 : jump_target);
9041 0 : else if (integer_zerop (op1))
9042 0 : arg_size
9043 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9044 : non_constant_p, overflow_p,
9045 : jump_target);
9046 : else
9047 : arg_size = NULL_TREE;
9048 9 : if (*jump_target)
9049 : return NULL_TREE;
9050 : }
9051 : else
9052 : arg_size = NULL_TREE;
9053 : }
9054 :
9055 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9056 70705 : if (!arg_size)
9057 : {
9058 70696 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9059 70696 : gcc_assert (fsz >= csz);
9060 70696 : fsz -= csz;
9061 70696 : if (esz)
9062 70696 : fsz /= esz;
9063 : }
9064 70705 : tree itype2 = build_index_type (size_int (fsz - 1));
9065 70705 : if (!cookie_size)
9066 70696 : return build_cplus_array_type (elt_type, itype2);
9067 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9068 : }
9069 :
9070 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9071 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9072 : the expression which needed the cleanup threw. If both threw, diagnose
9073 : it and return NULL, otherwise return R. If only the cleanup threw, set
9074 : *JUMP_TARGET to the exception object from the cleanup. */
9075 :
9076 : static tree
9077 810996 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9078 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9079 : {
9080 810997 : if (!throws (&jmp_target))
9081 : return r;
9082 7 : if (throws (jump_target))
9083 : {
9084 : /* [except.throw]/9 - If the exception handling mechanism
9085 : handling an uncaught exception directly invokes a function
9086 : that exits via an exception, the function std::terminate is
9087 : invoked. */
9088 6 : if (!ctx->quiet)
9089 : {
9090 2 : auto_diagnostic_group d;
9091 2 : diagnose_std_terminate (loc, ctx, *jump_target);
9092 2 : inform (loc, "destructor exited with an exception");
9093 2 : }
9094 6 : *non_constant_p = true;
9095 6 : *jump_target = NULL_TREE;
9096 6 : return NULL_TREE;
9097 : }
9098 1 : *jump_target = jmp_target;
9099 1 : return r;
9100 : }
9101 :
9102 : /* Attempt to reduce the expression T to a constant value.
9103 : On failure, issue diagnostic and return error_mark_node. */
9104 : /* FIXME unify with c_fully_fold */
9105 : /* FIXME overflow_p is too global */
9106 :
9107 : tree
9108 2688545544 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9109 : value_cat lval,
9110 : bool *non_constant_p, bool *overflow_p,
9111 : tree *jump_target)
9112 : {
9113 2688545544 : if (*jump_target)
9114 : {
9115 : /* If we are jumping, ignore all statements/expressions except those
9116 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9117 1299773 : switch (TREE_CODE (t))
9118 : {
9119 : case BIND_EXPR:
9120 : case STATEMENT_LIST:
9121 : case LOOP_EXPR:
9122 : case COND_EXPR:
9123 : case IF_STMT:
9124 : case DO_STMT:
9125 : case WHILE_STMT:
9126 : case FOR_STMT:
9127 : break;
9128 333998 : case LABEL_EXPR:
9129 333998 : case CASE_LABEL_EXPR:
9130 333998 : if (label_matches (ctx, jump_target, t))
9131 : /* Found it. */
9132 34271 : *jump_target = NULL_TREE;
9133 333998 : return NULL_TREE;
9134 : default:
9135 : return NULL_TREE;
9136 : }
9137 : }
9138 2687463010 : if (error_operand_p (t))
9139 : {
9140 139 : *non_constant_p = true;
9141 139 : return t;
9142 : }
9143 :
9144 : /* Change the input location to the currently processed expression for
9145 : better error messages when a subexpression has no location. */
9146 2687462871 : location_t loc = cp_expr_loc_or_input_loc (t);
9147 5374920329 : iloc_sentinel sentinel (loc);
9148 :
9149 2687462871 : STRIP_ANY_LOCATION_WRAPPER (t);
9150 :
9151 2687462871 : if (CONSTANT_CLASS_P (t))
9152 : {
9153 407521832 : if (TREE_OVERFLOW (t))
9154 : {
9155 62 : if (!ctx->quiet)
9156 15 : permerror (input_location, "overflow in constant expression");
9157 62 : if (!flag_permissive || ctx->quiet)
9158 59 : *overflow_p = true;
9159 : }
9160 :
9161 407521832 : if (TREE_CODE (t) == INTEGER_CST
9162 389530989 : && TYPE_PTR_P (TREE_TYPE (t))
9163 : /* INTEGER_CST with pointer-to-method type is only used
9164 : for a virtual method in a pointer to member function.
9165 : Don't reject those. */
9166 2018713 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9167 409540275 : && !integer_zerop (t))
9168 : {
9169 5 : if (!ctx->quiet)
9170 0 : error ("value %qE of type %qT is not a constant expression",
9171 0 : t, TREE_TYPE (t));
9172 5 : *non_constant_p = true;
9173 : }
9174 :
9175 407521832 : return t;
9176 : }
9177 :
9178 : /* Avoid excessively long constexpr evaluations. */
9179 2279941039 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9180 : {
9181 9 : if (!ctx->quiet)
9182 3 : error_at (loc,
9183 : "%<constexpr%> evaluation operation count exceeds limit of "
9184 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9185 : constexpr_ops_limit);
9186 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9187 9 : *non_constant_p = true;
9188 9 : return t;
9189 : }
9190 :
9191 2279941030 : constexpr_ctx new_ctx;
9192 2279941030 : tree r = t;
9193 :
9194 2279941030 : tree_code tcode = TREE_CODE (t);
9195 2279941030 : switch (tcode)
9196 : {
9197 35909866 : case RESULT_DECL:
9198 35909866 : if (lval)
9199 : return t;
9200 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9201 : through an invisible reference, or in named return value
9202 : optimization. */
9203 39727 : if (tree v = ctx->global->get_value (t))
9204 : return v;
9205 : else
9206 : {
9207 3 : if (!ctx->quiet)
9208 0 : error ("%qE is not a constant expression", t);
9209 3 : *non_constant_p = true;
9210 : }
9211 3 : break;
9212 :
9213 307235162 : case VAR_DECL:
9214 307235162 : if (DECL_HAS_VALUE_EXPR_P (t))
9215 : {
9216 7918773 : if (is_normal_capture_proxy (t)
9217 7918773 : && current_function_decl == DECL_CONTEXT (t))
9218 : {
9219 : /* Function parms aren't constexpr within the function
9220 : definition, so don't try to look at the closure. But if the
9221 : captured variable is constant, try to evaluate it directly. */
9222 1446040 : r = DECL_CAPTURED_VARIABLE (t);
9223 : }
9224 : else
9225 6472733 : r = DECL_VALUE_EXPR (t);
9226 :
9227 7918773 : tree type = TREE_TYPE (t);
9228 7918773 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
9229 : {
9230 : /* Adjust r to match the reference-ness of t. */
9231 992577 : if (TYPE_REF_P (type))
9232 992526 : r = build_address (r);
9233 : else
9234 51 : r = convert_from_reference (r);
9235 : }
9236 7918773 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
9237 7918773 : overflow_p, jump_target);
9238 : }
9239 : /* fall through */
9240 311135805 : case CONST_DECL:
9241 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
9242 : CONST_DECL for aggregate constants. */
9243 311135805 : if (lval)
9244 : return t;
9245 222078200 : else if (t == ctx->object)
9246 1290351 : return ctx->ctor;
9247 220787849 : if (VAR_P (t))
9248 : {
9249 208968433 : if (tree v = ctx->global->get_value (t))
9250 : {
9251 : r = v;
9252 : break;
9253 : }
9254 165245670 : if (ctx->global->is_outside_lifetime (t))
9255 : {
9256 103 : if (!ctx->quiet)
9257 30 : outside_lifetime_error (loc, t);
9258 103 : *non_constant_p = true;
9259 103 : break;
9260 : }
9261 : }
9262 177064983 : if (ctx->manifestly_const_eval == mce_true)
9263 107825386 : maybe_warn_about_constant_value (loc, t);
9264 177064983 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
9265 177064983 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9266 : {
9267 : /* If the class is empty, we aren't actually loading anything. */
9268 112627 : r = build_constructor (TREE_TYPE (t), NULL);
9269 112627 : TREE_CONSTANT (r) = true;
9270 : }
9271 176952356 : else if (ctx->strict)
9272 176638120 : r = decl_really_constant_value (t, /*unshare_p=*/false);
9273 : else
9274 314236 : r = decl_constant_value (t, /*unshare_p=*/false);
9275 177062286 : if (TREE_CODE (r) == TARGET_EXPR
9276 177062286 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9277 0 : r = TARGET_EXPR_INITIAL (r);
9278 177062286 : if (DECL_P (r)
9279 : /* P2280 allows references to unknown. */
9280 177425194 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
9281 : {
9282 47599895 : if (!ctx->quiet)
9283 171 : non_const_var_error (loc, r, /*fundef_p*/false);
9284 47599895 : *non_constant_p = true;
9285 : }
9286 : break;
9287 :
9288 : case DEBUG_BEGIN_STMT:
9289 : /* ??? It might be nice to retain this information somehow, so
9290 : as to be able to step into a constexpr function call. */
9291 : /* Fall through. */
9292 :
9293 : case FUNCTION_DECL:
9294 : case TEMPLATE_DECL:
9295 : case LABEL_DECL:
9296 : case LABEL_EXPR:
9297 : case CASE_LABEL_EXPR:
9298 : case PREDICT_EXPR:
9299 : case OMP_DECLARE_MAPPER:
9300 : case REFLECT_EXPR:
9301 : return t;
9302 :
9303 249615727 : case PARM_DECL:
9304 249615727 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9305 : {
9306 : /* glvalue use. */
9307 13917660 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9308 146761 : if (tree v = ctx->global->get_value (t))
9309 1779769638 : r = v;
9310 : }
9311 235698067 : else if (tree v = ctx->global->get_value (t))
9312 : {
9313 117626268 : r = v;
9314 117626268 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9315 537 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9316 : non_constant_p, overflow_p,
9317 : jump_target);
9318 117626268 : if (*jump_target)
9319 : return NULL_TREE;
9320 : }
9321 118071799 : else if (lval)
9322 : /* Defer in case this is only used for its type. */;
9323 118071799 : else if (ctx->global->is_outside_lifetime (t))
9324 : {
9325 18 : if (!ctx->quiet)
9326 6 : outside_lifetime_error (loc, t);
9327 18 : *non_constant_p = true;
9328 18 : break;
9329 : }
9330 118071781 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9331 118071781 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9332 : {
9333 : /* If the class is empty, we aren't actually loading anything. */
9334 5797 : r = build_constructor (TREE_TYPE (t), NULL);
9335 5797 : TREE_CONSTANT (r) = true;
9336 : }
9337 118065984 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9338 : /* P2280 allows references to unknown... */;
9339 117813013 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9340 : /* ...as well as the this pointer. */;
9341 : else
9342 : {
9343 117348106 : if (!ctx->quiet)
9344 195 : error ("%qE is not a constant expression", t);
9345 117348106 : *non_constant_p = true;
9346 : }
9347 : break;
9348 :
9349 199404555 : case CALL_EXPR:
9350 199404555 : case AGGR_INIT_EXPR:
9351 199404555 : r = cxx_eval_call_expression (ctx, t, lval,
9352 : non_constant_p, overflow_p, jump_target);
9353 199404555 : break;
9354 :
9355 16386127 : case DECL_EXPR:
9356 16386127 : {
9357 16386127 : r = DECL_EXPR_DECL (t);
9358 16386127 : if (TREE_CODE (r) == USING_DECL)
9359 : {
9360 8988 : r = void_node;
9361 8988 : break;
9362 : }
9363 :
9364 16377139 : if (VAR_P (r)
9365 16377139 : && (TREE_STATIC (r)
9366 16254595 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9367 : /* Allow __FUNCTION__ etc. */
9368 122544 : && !DECL_ARTIFICIAL (r)
9369 16377171 : && !decl_constant_var_p (r))
9370 : {
9371 7 : if (!ctx->quiet)
9372 : {
9373 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9374 1 : error_at (loc, "control passes through definition of %qD "
9375 : "with thread storage duration", r);
9376 : else
9377 1 : error_at (loc, "control passes through definition of %qD "
9378 : "with static storage duration", r);
9379 : }
9380 7 : *non_constant_p = true;
9381 7 : break;
9382 : }
9383 :
9384 : /* make_rtl_for_nonlocal_decl could have deferred emission of
9385 : a local static var, but if it appears in a statement expression
9386 : which is constant expression evaluated to e.g. just the address
9387 : of the variable, its DECL_EXPR will never be seen during
9388 : gimple lowering's record_vars_into as the statement expression
9389 : will not be in the IL at all. */
9390 16377132 : if (VAR_P (r)
9391 16377132 : && TREE_STATIC (r)
9392 122537 : && !DECL_REALLY_EXTERN (r)
9393 122526 : && DECL_FUNCTION_SCOPE_P (r)
9394 122526 : && !var_in_maybe_constexpr_fn (r)
9395 16377135 : && decl_constant_var_p (r))
9396 : {
9397 3 : varpool_node *node = varpool_node::get (r);
9398 3 : if (node == NULL || !node->definition)
9399 3 : rest_of_decl_compilation (r, 0, at_eof);
9400 : }
9401 :
9402 32608633 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9403 31305927 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9404 : {
9405 1448543 : new_ctx = *ctx;
9406 1448543 : new_ctx.object = r;
9407 1448543 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9408 1448543 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9409 1448543 : ctx->global->put_value (r, new_ctx.ctor);
9410 1448543 : ctx = &new_ctx;
9411 : }
9412 :
9413 16377132 : if (tree init = DECL_INITIAL (r))
9414 : {
9415 6923435 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9416 : non_constant_p, overflow_p,
9417 : jump_target);
9418 6923435 : if (*jump_target)
9419 : return NULL_TREE;
9420 : /* Don't share a CONSTRUCTOR that might be changed. */
9421 6923435 : init = unshare_constructor (init);
9422 : /* Remember that a constant object's constructor has already
9423 : run. */
9424 13846870 : if (CLASS_TYPE_P (TREE_TYPE (r))
9425 7316454 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9426 34008 : TREE_READONLY (init) = true;
9427 6923435 : ctx->global->put_value (r, init);
9428 : }
9429 9453697 : else if (ctx == &new_ctx)
9430 : /* We gave it a CONSTRUCTOR above. */;
9431 : else
9432 8421180 : ctx->global->put_value (r, NULL_TREE);
9433 : }
9434 : break;
9435 :
9436 26152197 : case TARGET_EXPR:
9437 26152197 : {
9438 26152197 : tree type = TREE_TYPE (t);
9439 :
9440 26152197 : if (!literal_type_p (type))
9441 : {
9442 6247 : if (!ctx->quiet)
9443 : {
9444 0 : auto_diagnostic_group d;
9445 0 : error ("temporary of non-literal type %qT in a "
9446 : "constant expression", type);
9447 0 : explain_non_literal_class (type);
9448 0 : }
9449 6247 : *non_constant_p = true;
9450 13052010 : break;
9451 : }
9452 26145950 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9453 : /* Avoid evaluating a TARGET_EXPR more than once. */
9454 26145950 : tree slot = TARGET_EXPR_SLOT (t);
9455 26145950 : if (tree v = ctx->global->get_value (slot))
9456 : {
9457 1046 : if (lval)
9458 10522848 : return slot;
9459 : r = v;
9460 : break;
9461 : }
9462 26144904 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9463 : {
9464 : /* We're being expanded without an explicit target, so start
9465 : initializing a new object; expansion with an explicit target
9466 : strips the TARGET_EXPR before we get here. */
9467 20734716 : new_ctx = *ctx;
9468 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9469 : any PLACEHOLDER_EXPR within the initializer that refers to the
9470 : former object under construction. */
9471 20734716 : new_ctx.parent = ctx;
9472 20734716 : new_ctx.ctor = build_constructor (type, NULL);
9473 20734716 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9474 20734716 : new_ctx.object = slot;
9475 20734716 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9476 20734716 : ctx = &new_ctx;
9477 : }
9478 :
9479 : /* If the initializer is complex, evaluate it to initialize slot. */
9480 26144904 : bool is_complex = target_expr_needs_replace (t);
9481 26144904 : if (is_complex)
9482 : /* In case no initialization actually happens, clear out any
9483 : void_node from a previous evaluation. */
9484 244 : ctx->global->put_value (slot, NULL_TREE);
9485 :
9486 : /* Pass vc_prvalue because this indicates
9487 : initialization of a temporary. */
9488 26144904 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
9489 : non_constant_p, overflow_p,
9490 : jump_target);
9491 26144904 : if (*non_constant_p)
9492 : break;
9493 13099934 : if (*jump_target)
9494 : return NULL_TREE;
9495 13099698 : if (!is_complex)
9496 : {
9497 13099501 : r = unshare_constructor (r);
9498 : /* Adjust the type of the result to the type of the temporary. */
9499 13099501 : r = adjust_temp_type (type, r);
9500 13099501 : ctx->global->put_value (slot, r);
9501 : }
9502 13099698 : if (TARGET_EXPR_CLEANUP (t)
9503 13099698 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9504 : {
9505 1095082 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9506 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9507 : them. */
9508 1095082 : if (CLEANUP_EH_ONLY (t))
9509 913 : ctx->global->cleanups->safe_push (NULL_TREE);
9510 : }
9511 13099698 : if (ctx->save_exprs)
9512 6767159 : ctx->save_exprs->safe_push (slot);
9513 13099698 : if (lval)
9514 : return slot;
9515 2577339 : if (is_complex)
9516 0 : r = ctx->global->get_value (slot);
9517 : }
9518 2577339 : break;
9519 :
9520 83304906 : case INIT_EXPR:
9521 83304906 : case MODIFY_EXPR:
9522 83304906 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9523 83304906 : r = cxx_eval_store_expression (ctx, t, lval,
9524 : non_constant_p, overflow_p, jump_target);
9525 83304906 : break;
9526 :
9527 0 : case SCOPE_REF:
9528 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9529 : lval,
9530 : non_constant_p, overflow_p,
9531 : jump_target);
9532 0 : break;
9533 :
9534 52609046 : case RETURN_EXPR:
9535 52609046 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9536 52428161 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9537 : lval,
9538 : non_constant_p, overflow_p,
9539 : jump_target);
9540 52609044 : if (!throws (jump_target))
9541 52608928 : *jump_target = t;
9542 : break;
9543 20948 : case BREAK_STMT:
9544 20948 : case CONTINUE_STMT:
9545 20948 : *jump_target = t;
9546 20948 : break;
9547 :
9548 578850 : case SAVE_EXPR:
9549 : /* Avoid evaluating a SAVE_EXPR more than once. */
9550 578850 : if (tree v = ctx->global->get_value (t))
9551 : r = v;
9552 : else
9553 : {
9554 573827 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9555 : vc_prvalue, non_constant_p,
9556 : overflow_p, jump_target);
9557 573827 : if (*non_constant_p || *jump_target)
9558 : break;
9559 6790 : ctx->global->put_value (t, r);
9560 6790 : if (ctx->save_exprs)
9561 4652 : ctx->save_exprs->safe_push (t);
9562 : }
9563 : break;
9564 :
9565 44802207 : case NON_LVALUE_EXPR:
9566 44802207 : case EXPR_STMT:
9567 44802207 : case EH_SPEC_BLOCK:
9568 44802207 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9569 : lval,
9570 : non_constant_p, overflow_p,
9571 : jump_target);
9572 44802207 : break;
9573 :
9574 2218 : case TRY_BLOCK:
9575 2218 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9576 : non_constant_p, overflow_p,
9577 : jump_target);
9578 2218 : if (!*non_constant_p && throws (jump_target))
9579 1690 : if (tree h = TRY_HANDLERS (t))
9580 : {
9581 1690 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9582 1690 : if (TREE_CODE (h) == STATEMENT_LIST)
9583 : {
9584 470 : for (tree stmt : tsi_range (h))
9585 453 : if (TREE_CODE (stmt) == HANDLER
9586 453 : && handler_match_for_exception_type (stmt, type))
9587 : {
9588 : h = stmt;
9589 : break;
9590 : }
9591 231 : if (TREE_CODE (h) == STATEMENT_LIST)
9592 : h = NULL_TREE;
9593 : }
9594 1459 : else if (TREE_CODE (h) != HANDLER
9595 1459 : || !handler_match_for_exception_type (h, type))
9596 : h = NULL_TREE;
9597 : if (h)
9598 : {
9599 1673 : gcc_assert (VAR_P (*jump_target));
9600 1673 : ctx->global->caught_exceptions.safe_push (*jump_target);
9601 1673 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9602 1673 : *jump_target = NULL_TREE;
9603 1673 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9604 : vc_discard, non_constant_p,
9605 : overflow_p, jump_target);
9606 : }
9607 : }
9608 : break;
9609 :
9610 75406548 : case CLEANUP_POINT_EXPR:
9611 75406548 : {
9612 75406548 : auto_vec<tree, 2> cleanups;
9613 75406548 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9614 75406548 : ctx->global->cleanups = &cleanups;
9615 :
9616 75406548 : auto_vec<tree, 10> save_exprs;
9617 75406548 : constexpr_ctx new_ctx = *ctx;
9618 75406548 : new_ctx.save_exprs = &save_exprs;
9619 :
9620 75406548 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9621 : lval,
9622 : non_constant_p, overflow_p,
9623 : jump_target);
9624 :
9625 75406547 : ctx->global->cleanups = prev_cleanups;
9626 75406547 : unsigned int i;
9627 75406547 : tree cleanup, jmp_target = NULL_TREE;
9628 75406547 : bool eh = throws (jump_target);
9629 : /* Evaluate the cleanups. */
9630 151782767 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9631 969673 : if (cleanup == NULL_TREE)
9632 : {
9633 : /* NULL_TREE cleanup is a marker that before it is
9634 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9635 : if the body didn't throw. */
9636 890 : if (!eh)
9637 889 : --i;
9638 : }
9639 : else
9640 968783 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9641 : non_constant_p, overflow_p,
9642 : &jmp_target);
9643 :
9644 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9645 : full-expression. */
9646 232991452 : for (tree save_expr : save_exprs)
9647 6771811 : destroy_value_checked (ctx, save_expr, non_constant_p);
9648 75406547 : if (throws (&jmp_target))
9649 0 : *jump_target = jmp_target;
9650 75406547 : }
9651 75406547 : break;
9652 :
9653 50184093 : case MUST_NOT_THROW_EXPR:
9654 50184093 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9655 : lval,
9656 : non_constant_p, overflow_p,
9657 : jump_target);
9658 50184092 : if (throws (jump_target))
9659 : {
9660 : /* [except.handle]/7 - If the search for a handler exits the
9661 : function body of a function with a non-throwing exception
9662 : specification, the function std::terminate is invoked. */
9663 27 : if (!ctx->quiet)
9664 : {
9665 9 : auto_diagnostic_group d;
9666 9 : diagnose_std_terminate (loc, ctx, *jump_target);
9667 9 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9668 7 : && ctx->call
9669 16 : && ctx->call->fundef)
9670 7 : inform (loc, "uncaught exception exited from %<noexcept%> "
9671 : "function %qD",
9672 : ctx->call->fundef->decl);
9673 2 : else if (MUST_NOT_THROW_THROW_P (t))
9674 1 : inform (loc, "destructor exited with an exception after "
9675 : "initializing the exception object");
9676 1 : else if (MUST_NOT_THROW_CATCH_P (t))
9677 1 : inform (loc, "constructor exited with another exception while "
9678 : "entering handler");
9679 9 : }
9680 27 : *non_constant_p = true;
9681 27 : *jump_target = NULL_TREE;
9682 27 : r = NULL_TREE;
9683 : }
9684 : break;
9685 :
9686 0 : case TRY_CATCH_EXPR:
9687 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9688 : {
9689 0 : r = void_node;
9690 0 : break;
9691 : }
9692 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9693 : non_constant_p, overflow_p,
9694 : jump_target);
9695 0 : if (!*non_constant_p && throws (jump_target))
9696 : {
9697 0 : tree jmp_target = NULL_TREE;
9698 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9699 : non_constant_p, overflow_p,
9700 : &jmp_target);
9701 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9702 : jmp_target);
9703 : }
9704 : break;
9705 :
9706 618 : case TRY_FINALLY_EXPR:
9707 618 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9708 : non_constant_p, overflow_p,
9709 : jump_target);
9710 618 : if (!*non_constant_p)
9711 : {
9712 603 : tree jmp_target = NULL_TREE;
9713 : /* Also evaluate the cleanup. */
9714 603 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9715 603 : && throws (jump_target))
9716 0 : cxx_eval_constant_expression (ctx,
9717 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9718 : 1), vc_discard,
9719 : non_constant_p, overflow_p,
9720 : &jmp_target);
9721 : else
9722 603 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9723 : non_constant_p, overflow_p,
9724 : &jmp_target);
9725 603 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9726 : jmp_target);
9727 : }
9728 : break;
9729 :
9730 0 : case EH_ELSE_EXPR:
9731 : /* Evaluate any cleanup that applies to non-EH exits. */
9732 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9733 : non_constant_p, overflow_p,
9734 : jump_target);
9735 :
9736 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9737 0 : break;
9738 :
9739 3461938 : case CLEANUP_STMT:
9740 3461938 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9741 : non_constant_p, overflow_p,
9742 : jump_target);
9743 3461937 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9744 : {
9745 810393 : iloc_sentinel ils (loc);
9746 810393 : tree jmp_target = NULL_TREE;
9747 : /* Also evaluate the cleanup. */
9748 810393 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9749 : non_constant_p, overflow_p,
9750 : &jmp_target);
9751 810393 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9752 : jmp_target);
9753 810393 : }
9754 : break;
9755 :
9756 : /* These differ from cxx_eval_unary_expression in that this doesn't
9757 : check for a constant operand or result; an address can be
9758 : constant without its operand being, and vice versa. */
9759 101442620 : case MEM_REF:
9760 101442620 : case INDIRECT_REF:
9761 101442620 : r = cxx_eval_indirect_ref (ctx, t, lval,
9762 : non_constant_p, overflow_p,
9763 : jump_target);
9764 101442620 : break;
9765 :
9766 110223340 : case ADDR_EXPR:
9767 110223340 : {
9768 110223340 : tree oldop = TREE_OPERAND (t, 0);
9769 110223340 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9770 : non_constant_p, overflow_p,
9771 : jump_target);
9772 110223340 : if (*jump_target)
9773 : return NULL_TREE;
9774 : /* Don't VERIFY_CONSTANT here. */
9775 110223336 : if (*non_constant_p)
9776 : return t;
9777 87588669 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9778 : /* This function does more aggressive folding than fold itself. */
9779 87588669 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9780 87588669 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9781 : {
9782 64542189 : ggc_free (r);
9783 64542189 : return t;
9784 : }
9785 : break;
9786 : }
9787 :
9788 462590 : case REALPART_EXPR:
9789 462590 : case IMAGPART_EXPR:
9790 462590 : if (lval)
9791 : {
9792 614 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9793 : non_constant_p, overflow_p,
9794 : jump_target);
9795 614 : if (*jump_target)
9796 : return NULL_TREE;
9797 614 : if (r == error_mark_node)
9798 : ;
9799 614 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9800 : r = t;
9801 : else
9802 568 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9803 : break;
9804 : }
9805 : /* FALLTHRU */
9806 23999293 : case CONJ_EXPR:
9807 23999293 : case FIX_TRUNC_EXPR:
9808 23999293 : case FLOAT_EXPR:
9809 23999293 : case NEGATE_EXPR:
9810 23999293 : case ABS_EXPR:
9811 23999293 : case ABSU_EXPR:
9812 23999293 : case BIT_NOT_EXPR:
9813 23999293 : case TRUTH_NOT_EXPR:
9814 23999293 : case FIXED_CONVERT_EXPR:
9815 23999293 : case VEC_DUPLICATE_EXPR:
9816 23999293 : r = cxx_eval_unary_expression (ctx, t, lval,
9817 : non_constant_p, overflow_p,
9818 : jump_target);
9819 23999293 : break;
9820 :
9821 10499970 : case SIZEOF_EXPR:
9822 10499970 : r = fold_sizeof_expr (t);
9823 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9824 : which could lead to an infinite recursion. */
9825 10499970 : if (TREE_CODE (r) != SIZEOF_EXPR)
9826 10499970 : r = cxx_eval_constant_expression (ctx, r, lval,
9827 : non_constant_p, overflow_p,
9828 : jump_target);
9829 : else
9830 : {
9831 0 : *non_constant_p = true;
9832 0 : gcc_assert (ctx->quiet);
9833 : }
9834 :
9835 : break;
9836 :
9837 8644234 : case COMPOUND_EXPR:
9838 8644234 : {
9839 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9840 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9841 : introduced by build_call_a. */
9842 8644234 : tree op0 = TREE_OPERAND (t, 0);
9843 8644234 : tree op1 = TREE_OPERAND (t, 1);
9844 8644234 : STRIP_NOPS (op1);
9845 3588351 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9846 9854165 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9847 5444194 : r = cxx_eval_constant_expression (ctx, op0,
9848 : lval, non_constant_p, overflow_p,
9849 : jump_target);
9850 : else
9851 : {
9852 : /* Check that the LHS is constant and then discard it. */
9853 3200040 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9854 : non_constant_p, overflow_p,
9855 : jump_target);
9856 3200040 : if (*jump_target)
9857 : return NULL_TREE;
9858 3196660 : if (*non_constant_p)
9859 : return t;
9860 2948536 : op1 = TREE_OPERAND (t, 1);
9861 2948536 : r = cxx_eval_constant_expression (ctx, op1,
9862 : lval, non_constant_p, overflow_p,
9863 : jump_target);
9864 : }
9865 : }
9866 : break;
9867 :
9868 92687443 : case POINTER_PLUS_EXPR:
9869 92687443 : case POINTER_DIFF_EXPR:
9870 92687443 : case PLUS_EXPR:
9871 92687443 : case MINUS_EXPR:
9872 92687443 : case MULT_EXPR:
9873 92687443 : case TRUNC_DIV_EXPR:
9874 92687443 : case CEIL_DIV_EXPR:
9875 92687443 : case FLOOR_DIV_EXPR:
9876 92687443 : case ROUND_DIV_EXPR:
9877 92687443 : case TRUNC_MOD_EXPR:
9878 92687443 : case CEIL_MOD_EXPR:
9879 92687443 : case ROUND_MOD_EXPR:
9880 92687443 : case RDIV_EXPR:
9881 92687443 : case EXACT_DIV_EXPR:
9882 92687443 : case MIN_EXPR:
9883 92687443 : case MAX_EXPR:
9884 92687443 : case LSHIFT_EXPR:
9885 92687443 : case RSHIFT_EXPR:
9886 92687443 : case LROTATE_EXPR:
9887 92687443 : case RROTATE_EXPR:
9888 92687443 : case BIT_IOR_EXPR:
9889 92687443 : case BIT_XOR_EXPR:
9890 92687443 : case BIT_AND_EXPR:
9891 92687443 : case TRUTH_XOR_EXPR:
9892 92687443 : case LT_EXPR:
9893 92687443 : case LE_EXPR:
9894 92687443 : case GT_EXPR:
9895 92687443 : case GE_EXPR:
9896 92687443 : case EQ_EXPR:
9897 92687443 : case NE_EXPR:
9898 92687443 : case SPACESHIP_EXPR:
9899 92687443 : case UNORDERED_EXPR:
9900 92687443 : case ORDERED_EXPR:
9901 92687443 : case UNLT_EXPR:
9902 92687443 : case UNLE_EXPR:
9903 92687443 : case UNGT_EXPR:
9904 92687443 : case UNGE_EXPR:
9905 92687443 : case UNEQ_EXPR:
9906 92687443 : case LTGT_EXPR:
9907 92687443 : case RANGE_EXPR:
9908 92687443 : case COMPLEX_EXPR:
9909 92687443 : r = cxx_eval_binary_expression (ctx, t, lval,
9910 : non_constant_p, overflow_p,
9911 : jump_target);
9912 92687443 : break;
9913 :
9914 : /* fold can introduce non-IF versions of these; still treat them as
9915 : short-circuiting. */
9916 12531203 : case TRUTH_AND_EXPR:
9917 12531203 : case TRUTH_ANDIF_EXPR:
9918 12531203 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
9919 : boolean_true_node,
9920 : non_constant_p, overflow_p,
9921 : jump_target);
9922 12531203 : break;
9923 :
9924 3197281 : case TRUTH_OR_EXPR:
9925 3197281 : case TRUTH_ORIF_EXPR:
9926 3197281 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
9927 : boolean_false_node,
9928 : non_constant_p, overflow_p,
9929 : jump_target);
9930 3197281 : break;
9931 :
9932 10623027 : case ARRAY_REF:
9933 10623027 : r = cxx_eval_array_reference (ctx, t, lval,
9934 : non_constant_p, overflow_p,
9935 : jump_target);
9936 10623027 : break;
9937 :
9938 105743546 : case COMPONENT_REF:
9939 105743546 : if (is_overloaded_fn (t))
9940 : {
9941 : /* We can only get here in checking mode via
9942 : build_non_dependent_expr, because any expression that
9943 : calls or takes the address of the function will have
9944 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9945 6 : gcc_checking_assert (ctx->quiet || errorcount);
9946 6 : *non_constant_p = true;
9947 6 : return t;
9948 : }
9949 105743540 : r = cxx_eval_component_reference (ctx, t, lval,
9950 : non_constant_p, overflow_p,
9951 : jump_target);
9952 105743540 : break;
9953 :
9954 1914 : case BIT_FIELD_REF:
9955 1914 : r = cxx_eval_bit_field_ref (ctx, t, lval,
9956 : non_constant_p, overflow_p,
9957 : jump_target);
9958 1914 : break;
9959 :
9960 26383102 : case COND_EXPR:
9961 26383102 : case IF_STMT:
9962 26383102 : if (*jump_target)
9963 : {
9964 151144 : tree orig_jump = *jump_target;
9965 151144 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
9966 302288 : ? TREE_OPERAND (t, 1) : void_node);
9967 : /* When jumping to a label, the label might be either in the
9968 : then or else blocks, so process then block first in skipping
9969 : mode first, and if we are still in the skipping mode at its end,
9970 : process the else block too. */
9971 151144 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9972 : overflow_p, jump_target);
9973 : /* It's possible that we found the label in the then block. But
9974 : it could have been followed by another jumping statement, e.g.
9975 : say we're looking for case 1:
9976 : if (cond)
9977 : {
9978 : // skipped statements
9979 : case 1:; // clears up *jump_target
9980 : return 1; // and sets it to a RETURN_EXPR
9981 : }
9982 : else { ... }
9983 : in which case we need not go looking to the else block.
9984 : (goto is not allowed in a constexpr function.) */
9985 151144 : if (*jump_target == orig_jump)
9986 : {
9987 151192 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
9988 151192 : ? TREE_OPERAND (t, 2) : void_node);
9989 151110 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9990 : overflow_p, jump_target);
9991 : }
9992 : break;
9993 : }
9994 26231958 : r = cxx_eval_conditional_expression (ctx, t, lval,
9995 : non_constant_p, overflow_p,
9996 : jump_target);
9997 26231958 : break;
9998 1073 : case VEC_COND_EXPR:
9999 1073 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10000 : overflow_p, jump_target);
10001 1073 : break;
10002 :
10003 25196875 : case CONSTRUCTOR:
10004 25196875 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10005 : {
10006 : /* Don't re-process a constant CONSTRUCTOR. */
10007 22978788 : verify_constructor_flags (t);
10008 22978788 : if (TREE_CONSTANT (t))
10009 : return t;
10010 : }
10011 2218087 : if (TREE_CLOBBER_P (t))
10012 : {
10013 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10014 : a clobber by itself isn't a constant-expression. */
10015 0 : gcc_assert (ctx->quiet);
10016 0 : *non_constant_p = true;
10017 0 : break;
10018 : }
10019 2218087 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10020 : non_constant_p, overflow_p, jump_target);
10021 2218087 : break;
10022 :
10023 524 : case VEC_INIT_EXPR:
10024 : /* We can get this in a defaulted constructor for a class with a
10025 : non-static data member of array type. Either the initializer will
10026 : be NULL, meaning default-initialization, or it will be an lvalue
10027 : or xvalue of the same type, meaning direct-initialization from the
10028 : corresponding member. */
10029 524 : r = cxx_eval_vec_init (ctx, t, lval,
10030 : non_constant_p, overflow_p, jump_target);
10031 524 : break;
10032 :
10033 16 : case VEC_PERM_EXPR:
10034 16 : r = cxx_eval_trinary_expression (ctx, t, lval,
10035 : non_constant_p, overflow_p,
10036 : jump_target);
10037 16 : break;
10038 :
10039 4 : case PAREN_EXPR:
10040 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
10041 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10042 : constant expressions since it's unaffected by -fassociative-math. */
10043 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10044 : non_constant_p, overflow_p,
10045 : jump_target);
10046 4 : break;
10047 :
10048 421943455 : case NOP_EXPR:
10049 421943455 : if (REINTERPRET_CAST_P (t))
10050 : {
10051 73 : if (!ctx->quiet)
10052 6 : error_at (loc,
10053 : "%<reinterpret_cast%> is not a constant expression");
10054 73 : *non_constant_p = true;
10055 73 : return t;
10056 : }
10057 : /* FALLTHROUGH. */
10058 496388598 : case CONVERT_EXPR:
10059 496388598 : case VIEW_CONVERT_EXPR:
10060 496388598 : case UNARY_PLUS_EXPR:
10061 496388598 : {
10062 496388598 : tree oldop = TREE_OPERAND (t, 0);
10063 :
10064 496388598 : tree op = cxx_eval_constant_expression (ctx, oldop,
10065 496388598 : VOID_TYPE_P (TREE_TYPE (t))
10066 : ? vc_discard
10067 : : tcode == VIEW_CONVERT_EXPR
10068 468670093 : ? lval : vc_prvalue,
10069 : non_constant_p, overflow_p,
10070 : jump_target);
10071 496385900 : if (*jump_target)
10072 : return NULL_TREE;
10073 496383942 : if (*non_constant_p)
10074 : return t;
10075 361067641 : tree type = TREE_TYPE (t);
10076 :
10077 361067641 : if (VOID_TYPE_P (type))
10078 25407907 : return void_node;
10079 :
10080 335659734 : if (TREE_CODE (t) == CONVERT_EXPR
10081 28251505 : && ARITHMETIC_TYPE_P (type)
10082 4579062 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10083 335683192 : && ctx->strict)
10084 : {
10085 23366 : if (!ctx->quiet)
10086 54 : error_at (loc,
10087 : "conversion from pointer type %qT to arithmetic type "
10088 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10089 23366 : *non_constant_p = true;
10090 23366 : return t;
10091 : }
10092 :
10093 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10094 : type cannot be part of a core constant expression as a resolution to
10095 : DR 1312. */
10096 117859904 : if (TYPE_PTROB_P (type)
10097 115569139 : && TYPE_PTR_P (TREE_TYPE (op))
10098 90233805 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10099 : /* Inside a call to std::construct_at,
10100 : std::allocator<T>::{,de}allocate, or
10101 : std::source_location::current, we permit casting from void*
10102 : because that is compiler-generated code. */
10103 762160 : && !is_std_construct_at (ctx->call)
10104 89203 : && !is_std_allocator_allocate (ctx->call)
10105 335678014 : && !is_std_source_location_current (ctx->call))
10106 : {
10107 : /* Likewise, don't error when casting from void* when OP is
10108 : &heap uninit and similar. */
10109 41487 : tree sop = tree_strip_nop_conversions (op);
10110 41487 : tree decl = NULL_TREE;
10111 41487 : if (TREE_CODE (sop) == ADDR_EXPR)
10112 31780 : decl = TREE_OPERAND (sop, 0);
10113 31780 : if (decl
10114 31780 : && VAR_P (decl)
10115 31412 : && DECL_ARTIFICIAL (decl)
10116 63153 : && (DECL_NAME (decl) == heap_identifier
10117 24466 : || DECL_NAME (decl) == heap_uninit_identifier
10118 2819 : || DECL_NAME (decl) == heap_vec_identifier
10119 1517 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
10120 : /* OK */;
10121 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10122 : cv void" to a pointer-to-object type T unless P is a null
10123 : pointer value or points to an object whose type is similar to
10124 : T. */
10125 10130 : else if (cxx_dialect > cxx23)
10126 : {
10127 9978 : if (integer_zerop (sop))
10128 28 : return build_int_cst (type, 0);
10129 9950 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10130 : NULL, jump_target);
10131 9950 : if (*jump_target)
10132 : return NULL_TREE;
10133 9950 : if (r)
10134 : {
10135 9915 : r = build1 (ADDR_EXPR, type, r);
10136 9915 : break;
10137 : }
10138 35 : if (!ctx->quiet)
10139 : {
10140 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10141 3 : auto_diagnostic_group d;
10142 3 : error_at (loc, "cast from %qT is not allowed in a "
10143 : "constant expression because "
10144 : "pointed-to type %qT is not similar to %qT",
10145 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10146 3 : TREE_TYPE (type));
10147 3 : tree obj = build_fold_indirect_ref (sop);
10148 3 : if (TREE_CODE (obj) == COMPONENT_REF)
10149 2 : obj = TREE_OPERAND (obj, 1);
10150 3 : if (DECL_P (obj))
10151 3 : inform (DECL_SOURCE_LOCATION (obj),
10152 : "pointed-to object declared here");
10153 3 : }
10154 35 : *non_constant_p = true;
10155 35 : return t;
10156 : }
10157 : else
10158 : {
10159 152 : if (!ctx->quiet)
10160 26 : error_at (loc, "cast from %qT is not allowed in a "
10161 : "constant expression before C++26",
10162 26 : TREE_TYPE (op));
10163 152 : *non_constant_p = true;
10164 152 : return t;
10165 : }
10166 : }
10167 :
10168 335626238 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
10169 : {
10170 1009 : op = cplus_expand_constant (op);
10171 1009 : if (TREE_CODE (op) == PTRMEM_CST)
10172 : {
10173 21 : if (!ctx->quiet)
10174 3 : error_at (loc, "%qE is not a constant expression when the "
10175 : "class %qT is still incomplete", op,
10176 3 : PTRMEM_CST_CLASS (op));
10177 21 : *non_constant_p = true;
10178 21 : return t;
10179 : }
10180 : }
10181 :
10182 335626217 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
10183 : {
10184 606 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
10185 606 : && !can_convert_qual (type, op))
10186 6 : op = cplus_expand_constant (op);
10187 606 : return cp_fold_convert (type, op);
10188 : }
10189 :
10190 335625611 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
10191 : {
10192 577672 : if (integer_zerop (op))
10193 : {
10194 523240 : if (TYPE_REF_P (type))
10195 : {
10196 38 : if (!ctx->quiet)
10197 4 : error_at (loc, "dereferencing a null pointer");
10198 38 : *non_constant_p = true;
10199 38 : return t;
10200 : }
10201 : }
10202 54432 : else if (TYPE_PTR_P (type)
10203 54432 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10204 : /* INTEGER_CST with pointer-to-method type is only used
10205 : for a virtual method in a pointer to member function.
10206 : Don't reject those. */
10207 : ;
10208 : else
10209 : {
10210 : /* This detects for example:
10211 : reinterpret_cast<void*>(sizeof 0)
10212 : */
10213 54429 : if (!ctx->quiet)
10214 15 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
10215 : "a constant expression",
10216 : type, op);
10217 54429 : *non_constant_p = true;
10218 54429 : return t;
10219 : }
10220 : }
10221 :
10222 335571144 : if (INDIRECT_TYPE_P (type)
10223 179056328 : && TREE_CODE (op) == NOP_EXPR
10224 96128566 : && TREE_TYPE (op) == ptr_type_node
10225 287304 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
10226 284485 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
10227 335804481 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10228 233337 : 0)) == heap_uninit_identifier
10229 164133 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10230 164133 : 0)) == heap_vec_uninit_identifier))
10231 : {
10232 70705 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
10233 70705 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
10234 70705 : tree elt_type = TREE_TYPE (type);
10235 70705 : tree cookie_size = NULL_TREE;
10236 70705 : tree arg_size = NULL_TREE;
10237 70705 : if (TREE_CODE (elt_type) == RECORD_TYPE
10238 70705 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
10239 : {
10240 9 : tree fld1 = TYPE_FIELDS (elt_type);
10241 9 : tree fld2 = DECL_CHAIN (fld1);
10242 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
10243 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
10244 : }
10245 70705 : DECL_NAME (var)
10246 70705 : = (DECL_NAME (var) == heap_uninit_identifier
10247 70705 : ? heap_identifier : heap_vec_identifier);
10248 : /* For zero sized elt_type, try to recover how many outer_nelts
10249 : it should have. */
10250 70705 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
10251 70696 : : integer_zerop (var_size))
10252 89 : && !int_size_in_bytes (elt_type)
10253 9 : && TREE_CODE (oldop) == CALL_EXPR
10254 70723 : && call_expr_nargs (oldop) >= 1)
10255 9 : if (tree fun = get_function_named_in_call (oldop))
10256 9 : if (cxx_replaceable_global_alloc_fn (fun)
10257 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
10258 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
10259 70705 : tree new_type
10260 70705 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
10261 : var_size, arg_size,
10262 : non_constant_p, overflow_p,
10263 : jump_target);
10264 70705 : if (*jump_target)
10265 : return NULL_TREE;
10266 70705 : TREE_TYPE (var) = new_type;
10267 70705 : TREE_TYPE (TREE_OPERAND (op, 0))
10268 141410 : = build_pointer_type (TREE_TYPE (var));
10269 : }
10270 :
10271 : /* This can happen for std::meta::info(^^int) where the cast has no
10272 : meaning. */
10273 335571144 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
10274 : {
10275 : r = op;
10276 : break;
10277 : }
10278 :
10279 335525006 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
10280 : /* We didn't fold at the top so we could check for ptr-int
10281 : conversion. */
10282 26752082 : return fold (t);
10283 :
10284 308772924 : tree sop;
10285 :
10286 : /* Handle an array's bounds having been deduced after we built
10287 : the wrapping expression. */
10288 308772924 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
10289 : r = op;
10290 98928334 : else if (sop = tree_strip_nop_conversions (op),
10291 141781695 : sop != op && (same_type_ignoring_tlq_and_bounds_p
10292 42853361 : (type, TREE_TYPE (sop))))
10293 : r = sop;
10294 85415002 : else if (tcode == UNARY_PLUS_EXPR)
10295 0 : r = fold_convert (TREE_TYPE (t), op);
10296 : else
10297 85415002 : r = fold_build1 (tcode, type, op);
10298 :
10299 : /* Conversion of an out-of-range value has implementation-defined
10300 : behavior; the language considers it different from arithmetic
10301 : overflow, which is undefined. */
10302 308772924 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10303 11526 : TREE_OVERFLOW (r) = false;
10304 : }
10305 : break;
10306 :
10307 6094 : case EXCESS_PRECISION_EXPR:
10308 6094 : {
10309 6094 : tree oldop = TREE_OPERAND (t, 0);
10310 :
10311 6094 : tree op = cxx_eval_constant_expression (ctx, oldop,
10312 : lval,
10313 : non_constant_p, overflow_p,
10314 : jump_target);
10315 6094 : if (*jump_target)
10316 : return NULL_TREE;
10317 6094 : if (*non_constant_p)
10318 : return t;
10319 6086 : r = fold_convert (TREE_TYPE (t), op);
10320 6086 : break;
10321 : }
10322 :
10323 36487 : case EMPTY_CLASS_EXPR:
10324 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10325 : it to an appropriate CONSTRUCTOR. */
10326 36487 : return build_constructor (TREE_TYPE (t), NULL);
10327 :
10328 46033774 : case STATEMENT_LIST:
10329 46033774 : new_ctx = *ctx;
10330 46033774 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10331 46033774 : return cxx_eval_statement_list (&new_ctx, t,
10332 46033772 : non_constant_p, overflow_p, jump_target);
10333 :
10334 28691136 : case BIND_EXPR:
10335 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10336 : map, so that when checking whether they're already destroyed later we
10337 : don't get confused by remnants of previous calls. */
10338 42660111 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10339 13968975 : ctx->global->clear_value (decl);
10340 28691136 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10341 : lval,
10342 : non_constant_p, overflow_p,
10343 : jump_target);
10344 42660109 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10345 13968974 : destroy_value_checked (ctx, decl, non_constant_p);
10346 : break;
10347 :
10348 5832508 : case PREINCREMENT_EXPR:
10349 5832508 : case POSTINCREMENT_EXPR:
10350 5832508 : case PREDECREMENT_EXPR:
10351 5832508 : case POSTDECREMENT_EXPR:
10352 5832508 : return cxx_eval_increment_expression (ctx, t,
10353 : lval, non_constant_p, overflow_p,
10354 5832508 : jump_target);
10355 :
10356 1099 : case THROW_EXPR:
10357 1099 : if (cxx_dialect >= cxx26)
10358 772 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10359 : non_constant_p, overflow_p,
10360 772 : jump_target);
10361 : /* FALLTHROUGH */
10362 335 : case LAMBDA_EXPR:
10363 335 : case NEW_EXPR:
10364 335 : case VEC_NEW_EXPR:
10365 335 : case DELETE_EXPR:
10366 335 : case VEC_DELETE_EXPR:
10367 335 : case MODOP_EXPR:
10368 : /* GCC internal stuff. */
10369 335 : case VA_ARG_EXPR:
10370 335 : case BASELINK:
10371 335 : case OFFSET_REF:
10372 335 : if (!ctx->quiet)
10373 86 : error_at (loc, "expression %qE is not a constant expression", t);
10374 335 : *non_constant_p = true;
10375 335 : break;
10376 :
10377 468792 : case OBJ_TYPE_REF:
10378 : /* Virtual function lookup. We don't need to do anything fancy. */
10379 468792 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10380 : lval, non_constant_p, overflow_p,
10381 468792 : jump_target);
10382 :
10383 16116 : case PLACEHOLDER_EXPR:
10384 : /* Use of the value or address of the current object. */
10385 16116 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10386 : {
10387 15885 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10388 : return ctor;
10389 : else
10390 15585 : return cxx_eval_constant_expression (ctx, ctor, lval,
10391 : non_constant_p, overflow_p,
10392 15585 : jump_target);
10393 : }
10394 : /* A placeholder without a referent. We can get here when
10395 : checking whether NSDMIs are noexcept, or in massage_init_elt;
10396 : just say it's non-constant for now. */
10397 231 : gcc_assert (ctx->quiet);
10398 231 : *non_constant_p = true;
10399 231 : break;
10400 :
10401 3215 : case EXIT_EXPR:
10402 3215 : {
10403 3215 : tree cond = TREE_OPERAND (t, 0);
10404 3215 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10405 : non_constant_p, overflow_p,
10406 : jump_target);
10407 3215 : if (*jump_target)
10408 : return NULL_TREE;
10409 3215 : VERIFY_CONSTANT (cond);
10410 3215 : if (integer_nonzerop (cond))
10411 1554 : *jump_target = t;
10412 : }
10413 : break;
10414 :
10415 39 : case GOTO_EXPR:
10416 39 : if (breaks (&TREE_OPERAND (t, 0))
10417 39 : || continues (&TREE_OPERAND (t, 0)))
10418 18 : *jump_target = TREE_OPERAND (t, 0);
10419 : else
10420 : {
10421 21 : if (!ctx->quiet)
10422 1 : error_at (loc, "%<goto%> is not a constant expression");
10423 21 : *non_constant_p = true;
10424 : }
10425 : break;
10426 :
10427 5467081 : case LOOP_EXPR:
10428 5467081 : case DO_STMT:
10429 5467081 : case WHILE_STMT:
10430 5467081 : case FOR_STMT:
10431 5467081 : cxx_eval_loop_expr (ctx, t,
10432 : non_constant_p, overflow_p, jump_target);
10433 5467081 : break;
10434 :
10435 34548 : case SWITCH_EXPR:
10436 34548 : case SWITCH_STMT:
10437 34548 : cxx_eval_switch_expr (ctx, t,
10438 : non_constant_p, overflow_p, jump_target);
10439 34548 : break;
10440 :
10441 120 : case REQUIRES_EXPR:
10442 : /* It's possible to get a requires-expression in a constant
10443 : expression. For example:
10444 :
10445 : template<typename T> concept bool C() {
10446 : return requires (T t) { t; };
10447 : }
10448 :
10449 : template<typename T> requires !C<T>() void f(T);
10450 :
10451 : Normalization leaves f with the associated constraint
10452 : '!requires (T t) { ... }' which is not transformed into
10453 : a constraint. */
10454 120 : if (!processing_template_decl)
10455 120 : return evaluate_requires_expr (t);
10456 : else
10457 0 : *non_constant_p = true;
10458 0 : return t;
10459 :
10460 16216 : case ANNOTATE_EXPR:
10461 16216 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10462 : lval,
10463 : non_constant_p, overflow_p,
10464 : jump_target);
10465 16216 : break;
10466 :
10467 13505 : case USING_STMT:
10468 13505 : r = void_node;
10469 13505 : break;
10470 :
10471 12 : case ASSERTION_STMT:
10472 12 : case PRECONDITION_STMT:
10473 12 : case POSTCONDITION_STMT:
10474 12 : {
10475 12 : r = void_node;
10476 : /* Only record the first fail, and do not go further is the semantic
10477 : is 'ignore'. */
10478 12 : if (*non_constant_p || ctx->global->contract_statement
10479 24 : || contract_ignored_p (t))
10480 : break;
10481 :
10482 12 : tree cond = CONTRACT_CONDITION (t);
10483 12 : if (!potential_rvalue_constant_expression (cond))
10484 : {
10485 4 : ctx->global->contract_statement = t;
10486 4 : ctx->global->contract_condition_non_const = true;
10487 4 : break;
10488 : }
10489 :
10490 : /* We need to evaluate and stash the result of this here, since whether
10491 : it needs to be reported (and how) depends on whether the containing
10492 : expression is otherwise const. */
10493 8 : bool ctrct_non_const_p = false;
10494 8 : bool ctrct_overflow_p = false;
10495 8 : tree jmp_target = NULL_TREE;
10496 8 : constexpr_ctx new_ctx = *ctx;
10497 8 : new_ctx.quiet = true;
10498 : /* Avoid modification of existing values. */
10499 8 : modifiable_tracker ms (new_ctx.global);
10500 8 : tree eval =
10501 8 : cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
10502 : &ctrct_non_const_p,
10503 : &ctrct_overflow_p, &jmp_target);
10504 : /* Not a constant. */
10505 8 : if (ctrct_non_const_p)
10506 : {
10507 0 : ctx->global->contract_statement = t;
10508 0 : ctx->global->contract_condition_non_const = true;
10509 0 : break;
10510 : }
10511 : /* Constant, but check failed. */
10512 8 : if (integer_zerop (eval))
10513 8 : ctx->global->contract_statement = t;
10514 8 : }
10515 8 : break;
10516 :
10517 3746143 : case TEMPLATE_ID_EXPR:
10518 3746143 : {
10519 : /* We can evaluate template-id that refers to a concept only if
10520 : the template arguments are non-dependent. */
10521 3746143 : gcc_assert (concept_check_p (t));
10522 :
10523 3746143 : if (!value_dependent_expression_p (t)
10524 3746143 : && !uid_sensitive_constexpr_evaluation_p ())
10525 3745950 : r = evaluate_concept_check (t);
10526 : else
10527 193 : *non_constant_p = true;
10528 :
10529 : break;
10530 : }
10531 :
10532 54 : case ASM_EXPR:
10533 54 : if (!ctx->quiet)
10534 30 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10535 54 : *non_constant_p = true;
10536 54 : return t;
10537 :
10538 923 : case BIT_CAST_EXPR:
10539 923 : if (lval)
10540 : {
10541 0 : if (!ctx->quiet)
10542 0 : error_at (EXPR_LOCATION (t),
10543 : "address of a call to %qs is not a constant expression",
10544 : "__builtin_bit_cast");
10545 0 : *non_constant_p = true;
10546 0 : return t;
10547 : }
10548 923 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10549 923 : break;
10550 :
10551 0 : case OMP_PARALLEL:
10552 0 : case OMP_TASK:
10553 0 : case OMP_FOR:
10554 0 : case OMP_SIMD:
10555 0 : case OMP_DISTRIBUTE:
10556 0 : case OMP_TASKLOOP:
10557 0 : case OMP_LOOP:
10558 0 : case OMP_TEAMS:
10559 0 : case OMP_TARGET_DATA:
10560 0 : case OMP_TARGET:
10561 0 : case OMP_SECTIONS:
10562 0 : case OMP_ORDERED:
10563 0 : case OMP_CRITICAL:
10564 0 : case OMP_SINGLE:
10565 0 : case OMP_SCAN:
10566 0 : case OMP_SCOPE:
10567 0 : case OMP_SECTION:
10568 0 : case OMP_STRUCTURED_BLOCK:
10569 0 : case OMP_MASTER:
10570 0 : case OMP_MASKED:
10571 0 : case OMP_TASKGROUP:
10572 0 : case OMP_TARGET_UPDATE:
10573 0 : case OMP_TARGET_ENTER_DATA:
10574 0 : case OMP_TARGET_EXIT_DATA:
10575 0 : case OMP_ATOMIC:
10576 0 : case OMP_ATOMIC_READ:
10577 0 : case OMP_ATOMIC_CAPTURE_OLD:
10578 0 : case OMP_ATOMIC_CAPTURE_NEW:
10579 0 : case OMP_DEPOBJ:
10580 0 : case OACC_PARALLEL:
10581 0 : case OACC_KERNELS:
10582 0 : case OACC_SERIAL:
10583 0 : case OACC_DATA:
10584 0 : case OACC_HOST_DATA:
10585 0 : case OACC_LOOP:
10586 0 : case OACC_CACHE:
10587 0 : case OACC_DECLARE:
10588 0 : case OACC_ENTER_DATA:
10589 0 : case OACC_EXIT_DATA:
10590 0 : case OACC_UPDATE:
10591 0 : if (!ctx->quiet)
10592 0 : error_at (EXPR_LOCATION (t),
10593 : "statement is not a constant expression");
10594 0 : *non_constant_p = true;
10595 0 : break;
10596 :
10597 0 : default:
10598 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10599 : {
10600 : /* This function doesn't know how to deal with pre-genericize
10601 : statements; this can only happen with statement-expressions,
10602 : so for now just fail. */
10603 0 : if (!ctx->quiet)
10604 0 : error_at (EXPR_LOCATION (t),
10605 : "statement is not a constant expression");
10606 : }
10607 0 : else if (flag_checking)
10608 0 : internal_error ("unexpected expression %qE of kind %s", t,
10609 : get_tree_code_name (TREE_CODE (t)));
10610 0 : *non_constant_p = true;
10611 0 : break;
10612 : }
10613 :
10614 1779769638 : if (r == error_mark_node)
10615 30248487 : *non_constant_p = true;
10616 :
10617 101907996 : if (r == void_node && lval != vc_discard && !*jump_target
10618 1779773152 : && !VOID_TYPE_P (TREE_TYPE (t)))
10619 : {
10620 : /* For diagnostic quality we should have handled this sooner, where we
10621 : can be more specific about the out-of-lifetime object. But here we
10622 : can still be correct. */
10623 1 : gcc_checking_assert (false);
10624 : if (!ctx->quiet)
10625 : error_at (EXPR_LOCATION (t),
10626 : "%qE accesses an object outside its lifetime", t);
10627 : *non_constant_p = true;
10628 : }
10629 :
10630 1779769637 : if (*non_constant_p)
10631 550002899 : return t;
10632 : else
10633 : return r;
10634 : }
10635 :
10636 : /* P0859: A function is needed for constant evaluation if it is a constexpr
10637 : function that is named by an expression ([basic.def.odr]) that is
10638 : potentially constant evaluated.
10639 :
10640 : So we need to instantiate any constexpr functions mentioned by the
10641 : expression even if the definition isn't needed for evaluating the
10642 : expression. */
10643 :
10644 : static tree
10645 635830281 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10646 : {
10647 635830281 : if (TREE_CODE (*tp) == FUNCTION_DECL
10648 12977963 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10649 12855982 : && !DECL_INITIAL (*tp)
10650 3285739 : && !trivial_fn_p (*tp)
10651 3285719 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10652 635830281 : && !uid_sensitive_constexpr_evaluation_p ())
10653 : {
10654 3272098 : ++function_depth;
10655 3272098 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10656 3272090 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10657 : else
10658 8 : synthesize_method (*tp);
10659 3272098 : --function_depth;
10660 : }
10661 632558183 : else if (TREE_CODE (*tp) == CALL_EXPR
10662 619711192 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10663 : {
10664 13047039 : if (EXPR_HAS_LOCATION (*tp))
10665 13043898 : input_location = EXPR_LOCATION (*tp);
10666 : }
10667 :
10668 635830281 : if (!EXPR_P (*tp))
10669 348703295 : *walk_subtrees = 0;
10670 :
10671 635830281 : return NULL_TREE;
10672 : }
10673 :
10674 : static void
10675 296982758 : instantiate_constexpr_fns (tree t)
10676 : {
10677 296982758 : location_t loc = input_location;
10678 296982758 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10679 296982758 : input_location = loc;
10680 296982758 : }
10681 :
10682 : /* Look for heap variables in the expression *TP. */
10683 :
10684 : static tree
10685 606371 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10686 : {
10687 606371 : if (VAR_P (*tp)
10688 606371 : && (DECL_NAME (*tp) == heap_uninit_identifier
10689 6906 : || DECL_NAME (*tp) == heap_identifier
10690 3704 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10691 1717 : || DECL_NAME (*tp) == heap_vec_identifier
10692 543 : || DECL_NAME (*tp) == heap_deleted_identifier))
10693 : return *tp;
10694 :
10695 411652 : if (TYPE_P (*tp))
10696 51 : *walk_subtrees = 0;
10697 : return NULL_TREE;
10698 : }
10699 :
10700 : /* Find immediate function decls in *TP if any. */
10701 :
10702 : static tree
10703 647183717 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
10704 : {
10705 652152353 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10706 : return *tp;
10707 647179505 : if (TREE_CODE (*tp) == PTRMEM_CST
10708 55320 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10709 647234060 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10710 : return PTRMEM_CST_MEMBER (*tp);
10711 647179400 : if (REFLECT_EXPR_P (*tp))
10712 47052 : *walk_subtrees = 0;
10713 : return NULL_TREE;
10714 : }
10715 :
10716 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10717 : expression. Return a version of T that has TREE_CONSTANT cleared. */
10718 :
10719 : static tree
10720 138972 : mark_non_constant (tree t)
10721 : {
10722 138972 : gcc_checking_assert (TREE_CONSTANT (t));
10723 :
10724 : /* This isn't actually constant, so unset TREE_CONSTANT.
10725 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10726 : it to be set if it is invariant address, even when it is not
10727 : a valid C++ constant expression. Wrap it with a NOP_EXPR
10728 : instead. */
10729 138972 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10730 137395 : t = copy_node (t);
10731 1577 : else if (TREE_CODE (t) == CONSTRUCTOR)
10732 235 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10733 : else
10734 1342 : t = build_nop (TREE_TYPE (t), t);
10735 138972 : TREE_CONSTANT (t) = false;
10736 138972 : return t;
10737 : }
10738 :
10739 : /* If we have a successful constant evaluation, now check whether there is
10740 : a failed or non-constant contract that would invalidate this. */
10741 :
10742 : static bool
10743 429496667 : check_for_failed_contracts (constexpr_global_ctx *global_ctx)
10744 : {
10745 429496667 : if (!flag_contracts || !global_ctx->contract_statement)
10746 : return false;
10747 :
10748 12 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
10749 12 : enum diagnostics::kind kind;
10750 12 : bool error = false;
10751 : /* [intro.compliance.general]/2.3.4. */
10752 : /* [basic.contract.eval]/8. */
10753 12 : if (contract_terminating_p (global_ctx->contract_statement))
10754 : {
10755 : kind = diagnostics::kind::error;
10756 : error = true;
10757 : }
10758 : else
10759 4 : kind = diagnostics::kind::warning;
10760 :
10761 : /* [basic.contract.eval]/7.3 */
10762 12 : if (global_ctx->contract_condition_non_const)
10763 : {
10764 4 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
10765 4 : return error;
10766 : }
10767 :
10768 : /* Otherwise, the evaluation was const, but determined to be false. */
10769 8 : emit_diagnostic (kind, loc, 0,
10770 : "contract predicate is false in constant expression");
10771 8 : return error;
10772 : }
10773 :
10774 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10775 : STRICT has the same sense as for constant_value_1: true if we only allow
10776 : conforming C++ constant expressions, or false if we want a constant value
10777 : even if it doesn't conform.
10778 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10779 : per P0595 even when ALLOW_NON_CONSTANT is true.
10780 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10781 : OBJECT must be non-NULL in that case. */
10782 :
10783 : static tree
10784 615990857 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10785 : bool strict = true,
10786 : mce_value manifestly_const_eval = mce_unknown,
10787 : bool constexpr_dtor = false,
10788 : tree object = NULL_TREE)
10789 : {
10790 615990857 : auto_timevar time (TV_CONSTEXPR);
10791 :
10792 615990857 : bool non_constant_p = false;
10793 615990857 : bool overflow_p = false;
10794 :
10795 615990857 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10796 : {
10797 0 : gcc_checking_assert (allow_non_constant);
10798 : return t;
10799 : }
10800 :
10801 615990857 : constexpr_global_ctx global_ctx;
10802 615990857 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10803 : allow_non_constant, strict,
10804 615990857 : !allow_non_constant ? mce_true : manifestly_const_eval };
10805 :
10806 : /* Turn off -frounding-math for manifestly constant evaluation. */
10807 615990857 : warning_sentinel rm (flag_rounding_math,
10808 615990857 : ctx.manifestly_const_eval == mce_true);
10809 615990857 : tree type = (object
10810 615990857 : ? cv_unqualified (TREE_TYPE (object))
10811 545442910 : : initialized_type (t));
10812 615990857 : tree r = t;
10813 615990857 : bool is_consteval = false;
10814 615990857 : if (VOID_TYPE_P (type))
10815 : {
10816 11592289 : if (!constexpr_dtor)
10817 : {
10818 11592289 : if (cxx_dialect < cxx20)
10819 : return t;
10820 : /* We could have a COMPOUND_EXPR here coming from
10821 : keep_unused_object_arg. */
10822 11581318 : tree x = extract_call_expr (t);
10823 11581318 : if (x == NULL_TREE || x == error_mark_node)
10824 : return t;
10825 : /* Calls to immediate functions returning void need to be
10826 : evaluated. */
10827 11581278 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10828 23162556 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10829 : return t;
10830 : else
10831 590 : is_consteval = true;
10832 590 : tree lam;
10833 590 : if (manifestly_const_eval == mce_true
10834 816 : && LAMBDA_FUNCTION_P (fndecl)
10835 220 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
10836 810 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
10837 210 : global_ctx.consteval_block = fndecl;
10838 : }
10839 : }
10840 604398568 : else if (cxx_dialect >= cxx20
10841 596215079 : && (TREE_CODE (t) == CALL_EXPR
10842 503853128 : || TREE_CODE (t) == AGGR_INIT_EXPR
10843 495089168 : || TREE_CODE (t) == TARGET_EXPR))
10844 : {
10845 106965163 : tree x = t;
10846 106965163 : if (TREE_CODE (x) == TARGET_EXPR)
10847 5839252 : x = TARGET_EXPR_INITIAL (x);
10848 106965163 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10849 210924310 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10850 : is_consteval = true;
10851 : /* Don't try to evaluate a std::vector constructor taking an integer, it
10852 : will fail in the 'if (heap_var)' block below after doing all the work
10853 : (c++/113835). This will need adjustment if P3554 is accepted. Note
10854 : that evaluation of e.g. the vector default constructor can succeed, so
10855 : we don't shortcut all vector constructors. */
10856 207918294 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10857 17339576 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10858 106986914 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10859 : return t;
10860 : }
10861 604397747 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10862 : {
10863 : /* In C++14 an NSDMI can participate in aggregate initialization,
10864 : and can refer to the address of the object being initialized, so
10865 : we need to pass in the relevant VAR_DECL if we want to do the
10866 : evaluation in a single pass. The evaluation will dynamically
10867 : update ctx.values for the VAR_DECL. We use the same strategy
10868 : for C++11 constexpr constructors that refer to the object being
10869 : initialized. */
10870 67272260 : if (constexpr_dtor)
10871 : {
10872 143 : gcc_assert (object && VAR_P (object));
10873 143 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10874 143 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10875 143 : if (error_operand_p (DECL_INITIAL (object)))
10876 : return t;
10877 131 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10878 131 : TREE_READONLY (ctx.ctor) = false;
10879 : /* Temporarily force decl_really_constant_value to return false
10880 : for it, we want to use ctx.ctor for the current value instead. */
10881 131 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10882 : }
10883 : else
10884 : {
10885 67272117 : ctx.ctor = build_constructor (type, NULL);
10886 67272117 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10887 : }
10888 67272248 : if (!object)
10889 : {
10890 23760289 : if (TREE_CODE (t) == CALL_EXPR)
10891 : {
10892 : /* If T is calling a constructor to initialize an object, reframe
10893 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10894 : from outside the constant evaluation, which will fail even if
10895 : the value is actually constant (is_constant_evaluated3.C). */
10896 11329532 : tree fn = cp_get_callee_fndecl_nofold (t);
10897 22659054 : if (fn && DECL_CONSTRUCTOR_P (fn))
10898 : {
10899 5115964 : object = CALL_EXPR_ARG (t, 0);
10900 5115964 : object = build_fold_indirect_ref (object);
10901 5115964 : r = build_aggr_init_expr (type, r);
10902 : }
10903 : }
10904 12430757 : else if (TREE_CODE (t) == TARGET_EXPR)
10905 5738612 : object = TARGET_EXPR_SLOT (t);
10906 6692145 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10907 340610 : object = AGGR_INIT_EXPR_SLOT (t);
10908 : }
10909 67272248 : ctx.object = object;
10910 67272248 : if (object)
10911 54707145 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10912 : (type, TREE_TYPE (object)));
10913 54707145 : if (object && DECL_P (object))
10914 49223509 : global_ctx.put_value (object, ctx.ctor);
10915 67272248 : if (TREE_CODE (r) == TARGET_EXPR)
10916 : /* Avoid creating another CONSTRUCTOR when we expand the
10917 : TARGET_EXPR. */
10918 5875101 : r = TARGET_EXPR_INITIAL (r);
10919 : }
10920 :
10921 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
10922 : constexpr evaluation to avoid unnecessary template instantiation, and is
10923 : always done with mce_unknown. But due to gaps in the restriction logic
10924 : we may still end up taking an evaluation path that in turn requires
10925 : manifestly constant evaluation, and such evaluation must not be
10926 : restricted since it likely has semantic consequences.
10927 : TODO: Remove/replace the mechanism in GCC 17. */
10928 604397735 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
10929 604397735 : if (ctx.manifestly_const_eval == mce_true)
10930 296982758 : uid_sensitive_constexpr_evaluation_value = false;
10931 :
10932 604397735 : auto_vec<tree, 16> cleanups;
10933 604397735 : global_ctx.cleanups = &cleanups;
10934 :
10935 604397735 : if (manifestly_const_eval == mce_true)
10936 296982758 : instantiate_constexpr_fns (r);
10937 604397735 : tree jmp_target = NULL_TREE;
10938 604397735 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
10939 : &non_constant_p, &overflow_p,
10940 : &jmp_target);
10941 604395697 : if (throws (&jmp_target) && !non_constant_p)
10942 : {
10943 660 : if (!ctx.quiet)
10944 187 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10945 660 : non_constant_p = true;
10946 660 : jmp_target = NULL_TREE;
10947 660 : r = t;
10948 : }
10949 604394377 : else if (!non_constant_p && jmp_target)
10950 : {
10951 24 : non_constant_p = true;
10952 24 : if (!ctx.quiet)
10953 : {
10954 3 : if (breaks (&jmp_target))
10955 3 : error ("%<break%> outside of a loop or %<switch%>");
10956 0 : else if (continues (&jmp_target))
10957 0 : error ("%<continue%> outside of a loop");
10958 0 : else if (returns (&jmp_target))
10959 0 : error ("%<return%> in a statement expression");
10960 : else
10961 0 : gcc_unreachable ();
10962 : }
10963 24 : r = t;
10964 : }
10965 :
10966 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
10967 : of statements, and the result ought to be stored in ctx.ctor. */
10968 604395037 : if (r == void_node && !constexpr_dtor && ctx.ctor)
10969 0 : r = ctx.ctor;
10970 :
10971 604395037 : unsigned int i;
10972 604395037 : tree cleanup;
10973 604395037 : jmp_target = NULL_TREE;
10974 : /* Evaluate the cleanups. */
10975 1208915484 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
10976 125410 : if (cleanup == NULL_TREE)
10977 : /* NULL_TREE cleanup is a marker that before it is
10978 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
10979 23 : --i;
10980 : else
10981 125387 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
10982 : &non_constant_p, &overflow_p,
10983 : &jmp_target);
10984 604395037 : if (throws (&jmp_target) && !non_constant_p)
10985 : {
10986 0 : if (!ctx.quiet)
10987 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10988 0 : non_constant_p = true;
10989 0 : r = t;
10990 : }
10991 :
10992 : /* Mutable logic is a bit tricky: we want to allow initialization of
10993 : constexpr variables with mutable members, but we can't copy those
10994 : members to another constexpr variable. */
10995 604395037 : if (!non_constant_p
10996 435203510 : && TREE_CODE (r) == CONSTRUCTOR
10997 625995853 : && CONSTRUCTOR_MUTABLE_POISON (r))
10998 : {
10999 91 : if (!allow_non_constant)
11000 6 : error ("%qE is not a constant expression because it refers to "
11001 : "mutable subobjects of %qT", t, type);
11002 91 : non_constant_p = true;
11003 : }
11004 :
11005 435203419 : if (!non_constant_p && cxx_dialect >= cxx20
11006 1039598456 : && !global_ctx.heap_vars.is_empty ())
11007 : {
11008 199828 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11009 : NULL);
11010 199828 : unsigned int i;
11011 199828 : if (heap_var)
11012 : {
11013 194719 : if (!allow_non_constant && !non_constant_p)
11014 : {
11015 12 : if (DECL_LANG_SPECIFIC (heap_var))
11016 2 : error ("%qE is not a constant expression because it refers to "
11017 : "exception object allocated with "
11018 : "%<__cxa_allocate_exception%>", t);
11019 : else
11020 10 : error ("%qE is not a constant expression because it refers to "
11021 : "a result of %<operator new%>", t);
11022 12 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11023 : }
11024 194719 : r = t;
11025 194719 : non_constant_p = true;
11026 : }
11027 443367 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11028 : {
11029 243539 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11030 : {
11031 194806 : if (!allow_non_constant && !non_constant_p)
11032 : {
11033 16 : error ("%qE is not a constant expression because allocated "
11034 : "storage has not been deallocated", t);
11035 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11036 : }
11037 194806 : r = t;
11038 194806 : non_constant_p = true;
11039 : }
11040 : }
11041 : }
11042 :
11043 : /* Check that immediate invocation does not return an expression referencing
11044 : any immediate function decls. */
11045 604395037 : if (!non_constant_p && cxx_dialect >= cxx20)
11046 858737210 : if (tree immediate_fndecl
11047 429368605 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11048 : NULL))
11049 : {
11050 4317 : if (!allow_non_constant && !non_constant_p)
11051 : {
11052 73 : if (is_consteval)
11053 36 : error_at (cp_expr_loc_or_input_loc (t),
11054 : "immediate evaluation returns address of immediate "
11055 : "function %qD", immediate_fndecl);
11056 : else
11057 56 : error_at (cp_expr_loc_or_input_loc (t),
11058 : "constant evaluation returns address of immediate "
11059 : "function %qD", immediate_fndecl);
11060 : }
11061 4317 : r = t;
11062 4317 : non_constant_p = true;
11063 : }
11064 :
11065 : /* Detect consteval-only smuggling: turning a consteval-only object
11066 : into one that is not. For instance, in
11067 : struct B { };
11068 : struct D : B { info r; };
11069 : constexpr D d{^^::};
11070 : constexpr const B &b = d; // #1
11071 : #1 is wrong because D is a consteval-only type but B is not. */
11072 604395037 : if (flag_reflection
11073 4283200 : && !non_constant_p
11074 3447253 : && object
11075 394096 : && POINTER_TYPE_P (TREE_TYPE (object))
11076 783 : && !consteval_only_p (object)
11077 604395767 : && check_out_of_consteval_use (r, /*complain=*/false))
11078 : {
11079 15 : if (!allow_non_constant)
11080 : {
11081 5 : if (TYPE_REF_P (TREE_TYPE (object)))
11082 6 : error_at (cp_expr_loc_or_input_loc (t),
11083 : "reference into an object of consteval-only type is "
11084 : "not a constant expression unless it also has "
11085 : "consteval-only type");
11086 : else
11087 2 : error_at (cp_expr_loc_or_input_loc (t),
11088 : "pointer into an object of consteval-only type is "
11089 : "not a constant expression unless it also has "
11090 : "consteval-only type");
11091 : }
11092 15 : r = t;
11093 15 : non_constant_p = true;
11094 : }
11095 :
11096 604395037 : if (!non_constant_p && !constexpr_dtor)
11097 435004160 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11098 :
11099 : /* After verify_constant because reduced_constant_expression_p can unset
11100 : CONSTRUCTOR_NO_CLEARING. */
11101 604395037 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11102 : {
11103 302874 : if (!allow_non_constant)
11104 41 : error ("%qE is not a constant expression because it refers to "
11105 : "an incompletely initialized variable", t);
11106 302874 : TREE_CONSTANT (r) = false;
11107 302874 : non_constant_p = true;
11108 : }
11109 :
11110 604395037 : if (non_constant_p)
11111 : /* If we saw something bad, go back to our argument. The wrapping below is
11112 : only for the cases of TREE_CONSTANT argument or overflow. */
11113 174063080 : r = t;
11114 :
11115 604395037 : if (!non_constant_p && overflow_p)
11116 215 : non_constant_p = true;
11117 :
11118 : /* Unshare the result. */
11119 604395037 : bool should_unshare = true;
11120 604395037 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
11121 1525927 : && TARGET_EXPR_INITIAL (t) == r))
11122 : should_unshare = false;
11123 :
11124 604395037 : if (non_constant_p && !allow_non_constant)
11125 2831 : return error_mark_node;
11126 604392206 : else if (non_constant_p && TREE_CONSTANT (r))
11127 134486 : r = mark_non_constant (r);
11128 604257720 : else if (non_constant_p)
11129 : return t;
11130 :
11131 430466228 : if (constexpr_dtor)
11132 : {
11133 113 : if (check_for_failed_contracts (&global_ctx))
11134 0 : r = mark_non_constant (r);
11135 : else
11136 113 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
11137 113 : return r;
11138 : }
11139 :
11140 : /* Check we are not trying to return the wrong type. */
11141 430466115 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
11142 : {
11143 : /* If so, this is not a constant expression. */
11144 151 : if (!allow_non_constant)
11145 2 : error ("%qE is not a constant expression because it initializes "
11146 2 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
11147 151 : return t;
11148 : }
11149 :
11150 430465964 : if (should_unshare)
11151 258409512 : r = unshare_expr (r);
11152 :
11153 430465964 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
11154 : {
11155 20015884 : r = adjust_temp_type (type, r);
11156 20015884 : if (TREE_CODE (t) == TARGET_EXPR
11157 20015884 : && TARGET_EXPR_INITIAL (t) == r)
11158 : return t;
11159 19046474 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
11160 2704791 : || TREE_CODE (t) == AGGR_INIT_EXPR)
11161 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
11162 624521 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
11163 456 : r = get_target_expr (r);
11164 : else
11165 : {
11166 624065 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
11167 624065 : TREE_CONSTANT (r) = true;
11168 : }
11169 : }
11170 :
11171 429496554 : if (TREE_CODE (t) == TARGET_EXPR
11172 556517 : && TREE_CODE (r) == TARGET_EXPR)
11173 : {
11174 : /* Preserve this flag for potential_constant_expression, and the others
11175 : for good measure. */
11176 556419 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
11177 556419 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
11178 556419 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
11179 556419 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
11180 : }
11181 :
11182 : /* Remember the original location if that wouldn't need a wrapper. */
11183 429496554 : if (location_t loc = EXPR_LOCATION (t))
11184 203387141 : protected_set_expr_location (r, loc);
11185 :
11186 429496554 : if (check_for_failed_contracts (&global_ctx))
11187 8 : r = mark_non_constant (r);
11188 429496554 : return r;
11189 615988159 : }
11190 :
11191 : /* If T represents a constant expression returns its reduced value.
11192 : Otherwise return error_mark_node. */
11193 :
11194 : tree
11195 150243730 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
11196 : tsubst_flags_t complain /* = tf_error */)
11197 : {
11198 150243730 : bool sfinae = !(complain & tf_error);
11199 150243730 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
11200 150243730 : if (sfinae && !TREE_CONSTANT (r))
11201 1633 : r = error_mark_node;
11202 150243730 : return r;
11203 : }
11204 :
11205 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
11206 : of constexpr variables. The actual initializer of DECL is not modified. */
11207 :
11208 : void
11209 143 : cxx_constant_dtor (tree t, tree decl)
11210 : {
11211 143 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
11212 143 : }
11213 :
11214 : /* Helper routine for fold_simple function. Either return simplified
11215 : expression T, otherwise NULL_TREE.
11216 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
11217 : even if we are within template-declaration. So be careful on call, as in
11218 : such case types can be undefined. */
11219 :
11220 : static tree
11221 185451449 : fold_simple_1 (tree t)
11222 : {
11223 185451449 : tree op1;
11224 185451449 : enum tree_code code = TREE_CODE (t);
11225 :
11226 185451449 : switch (code)
11227 : {
11228 : case INTEGER_CST:
11229 : case REAL_CST:
11230 : case VECTOR_CST:
11231 : case FIXED_CST:
11232 : case COMPLEX_CST:
11233 : return t;
11234 :
11235 2062803 : case SIZEOF_EXPR:
11236 2062803 : return fold_sizeof_expr (t);
11237 :
11238 46274765 : case ABS_EXPR:
11239 46274765 : case ABSU_EXPR:
11240 46274765 : case CONJ_EXPR:
11241 46274765 : case REALPART_EXPR:
11242 46274765 : case IMAGPART_EXPR:
11243 46274765 : case NEGATE_EXPR:
11244 46274765 : case BIT_NOT_EXPR:
11245 46274765 : case TRUTH_NOT_EXPR:
11246 46274765 : case VIEW_CONVERT_EXPR:
11247 46274765 : CASE_CONVERT:
11248 46274765 : case FLOAT_EXPR:
11249 46274765 : case FIX_TRUNC_EXPR:
11250 46274765 : case FIXED_CONVERT_EXPR:
11251 46274765 : case ADDR_SPACE_CONVERT_EXPR:
11252 :
11253 46274765 : op1 = TREE_OPERAND (t, 0);
11254 :
11255 46274765 : t = const_unop (code, TREE_TYPE (t), op1);
11256 46274765 : if (!t)
11257 : return NULL_TREE;
11258 :
11259 4032593 : if (CONVERT_EXPR_CODE_P (code)
11260 4032593 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
11261 0 : TREE_OVERFLOW (t) = false;
11262 : return t;
11263 :
11264 : default:
11265 : return NULL_TREE;
11266 : }
11267 : }
11268 :
11269 : /* If T is a simple constant expression, returns its simplified value.
11270 : Otherwise returns T. In contrast to maybe_constant_value we
11271 : simplify only few operations on constant-expressions, and we don't
11272 : try to simplify constexpressions. */
11273 :
11274 : tree
11275 186115727 : fold_simple (tree t)
11276 : {
11277 186115727 : if (processing_template_decl)
11278 : return t;
11279 :
11280 185451449 : tree r = fold_simple_1 (t);
11281 185451449 : if (r)
11282 : return r;
11283 :
11284 : return t;
11285 : }
11286 :
11287 : /* Try folding the expression T to a simple constant.
11288 : Returns that constant, otherwise returns T. */
11289 :
11290 : tree
11291 1627915 : fold_to_constant (tree t)
11292 : {
11293 1627915 : if (processing_template_decl)
11294 : return t;
11295 :
11296 1535582 : tree r = fold (t);
11297 1535582 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
11298 : return r;
11299 : else
11300 : return t;
11301 : }
11302 :
11303 : /* If T is a constant expression, returns its reduced value.
11304 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
11305 : Otherwise, returns a version of T without TREE_CONSTANT.
11306 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
11307 : as per P0595. */
11308 :
11309 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
11310 :
11311 : tree
11312 937849763 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
11313 : mce_value manifestly_const_eval /* = mce_unknown */)
11314 : {
11315 937849763 : tree orig_t = t;
11316 937849763 : tree r;
11317 :
11318 937849763 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
11319 : {
11320 : /* Look up each operand in the cv_cache first to see if we've already
11321 : reduced it, and reuse that result to avoid quadratic behavior if
11322 : we're called when building up a large expression. */
11323 276362183 : int n = cp_tree_operand_length (t);
11324 276362183 : tree *ops = XALLOCAVEC (tree, n);
11325 276362183 : bool rebuild = false;
11326 780948413 : for (int i = 0; i < n; ++i)
11327 : {
11328 504586230 : ops[i] = TREE_OPERAND (t, i);
11329 1513160432 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
11330 33451123 : if (*cached != ops[i])
11331 : {
11332 9543144 : ops[i] = *cached;
11333 9543144 : rebuild = true;
11334 : }
11335 : }
11336 276362183 : if (rebuild)
11337 : {
11338 8090064 : t = copy_node (t);
11339 26415199 : for (int i = 0; i < n; ++i)
11340 18325135 : TREE_OPERAND (t, i) = ops[i];
11341 : }
11342 : }
11343 :
11344 937849763 : if (!is_nondependent_constant_expression (t))
11345 : {
11346 0 : if (TREE_OVERFLOW_P (t)
11347 144380894 : || (!processing_template_decl && TREE_CONSTANT (t)))
11348 4478 : t = mark_non_constant (t);
11349 144380894 : return t;
11350 : }
11351 793468869 : else if (CONSTANT_CLASS_P (t))
11352 : /* No caching or evaluation needed. */
11353 : return t;
11354 :
11355 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11356 : but at least try folding it to a simple constant. */
11357 378078190 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11358 537307 : return fold_to_constant (t);
11359 :
11360 357829618 : if (manifestly_const_eval != mce_unknown)
11361 : /* TODO: Extend the cache to be mce_value aware. And if we have a
11362 : previously cached mce_unknown result that's TREE_CONSTANT, it means
11363 : the reduced value is independent of mce_value and so we should
11364 : be able to reuse it in the mce_true/false case. */
11365 202882393 : return cxx_eval_outermost_constant_expr (t, true, true,
11366 202879695 : manifestly_const_eval, false, decl);
11367 :
11368 174658490 : if (cv_cache == NULL)
11369 138930 : cv_cache = hash_map<tree, tree>::create_ggc (101);
11370 174658490 : if (tree *cached = cv_cache->get (t))
11371 : {
11372 15442211 : r = *cached;
11373 15442211 : if (r != t)
11374 : {
11375 : /* Clear processing_template_decl for sake of break_out_target_exprs;
11376 : entries in the cv_cache are non-templated. */
11377 4501577 : processing_template_decl_sentinel ptds;
11378 :
11379 4501577 : r = break_out_target_exprs (r, /*clear_loc*/true);
11380 4501577 : protected_set_expr_location (r, EXPR_LOCATION (t));
11381 4501577 : }
11382 15442211 : return r;
11383 : }
11384 :
11385 159216279 : uid_sensitive_constexpr_evaluation_checker c;
11386 159216279 : r = cxx_eval_outermost_constant_expr (t, true, true,
11387 : manifestly_const_eval, false, decl);
11388 159216279 : gcc_checking_assert (r == t
11389 : || CONVERT_EXPR_P (t)
11390 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11391 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11392 : || !cp_tree_equal (r, t));
11393 159216279 : if (!c.evaluation_restricted_p ())
11394 158869044 : cv_cache->put (orig_t, r);
11395 : return r;
11396 : }
11397 :
11398 : /* Dispose of the whole CV_CACHE. */
11399 :
11400 : static void
11401 44696462 : clear_cv_cache (void)
11402 : {
11403 44696462 : if (cv_cache != NULL)
11404 44404899 : cv_cache->empty ();
11405 44696462 : }
11406 :
11407 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
11408 :
11409 : void
11410 44696462 : clear_cv_and_fold_caches ()
11411 : {
11412 44696462 : clear_cv_cache ();
11413 44696462 : clear_fold_cache ();
11414 44696462 : }
11415 :
11416 : /* Internal function handling expressions in templates for
11417 : fold_non_dependent_expr and fold_non_dependent_init.
11418 :
11419 : If we're in a template, but T isn't value dependent, simplify
11420 : it. We're supposed to treat:
11421 :
11422 : template <typename T> void f(T[1 + 1]);
11423 : template <typename T> void f(T[2]);
11424 :
11425 : as two declarations of the same function, for example. */
11426 :
11427 : static tree
11428 29201669 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11429 : bool manifestly_const_eval,
11430 : tree object)
11431 : {
11432 29201669 : gcc_assert (processing_template_decl);
11433 :
11434 29201669 : if (is_nondependent_constant_expression (t))
11435 : {
11436 16004640 : processing_template_decl_sentinel s;
11437 16004640 : t = instantiate_non_dependent_expr_internal (t, complain);
11438 :
11439 16004640 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11440 : {
11441 0 : if (TREE_OVERFLOW_P (t))
11442 : {
11443 0 : t = build_nop (TREE_TYPE (t), t);
11444 0 : TREE_CONSTANT (t) = false;
11445 : }
11446 0 : return t;
11447 : }
11448 16004640 : else if (CONSTANT_CLASS_P (t))
11449 : /* No evaluation needed. */
11450 : return t;
11451 :
11452 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11453 : but at least try folding it to a simple constant. */
11454 4201315 : if (cp_unevaluated_operand && !manifestly_const_eval)
11455 89 : return fold_to_constant (t);
11456 :
11457 4201226 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11458 : mce_value (manifestly_const_eval),
11459 : false, object);
11460 : /* cp_tree_equal looks through NOPs, so allow them. */
11461 4201226 : gcc_checking_assert (r == t
11462 : || CONVERT_EXPR_P (t)
11463 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11464 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11465 : || !cp_tree_equal (r, t));
11466 4201226 : return r;
11467 16004640 : }
11468 13197029 : else if (TREE_OVERFLOW_P (t))
11469 : {
11470 0 : t = build_nop (TREE_TYPE (t), t);
11471 0 : TREE_CONSTANT (t) = false;
11472 : }
11473 :
11474 : return t;
11475 : }
11476 :
11477 : /* Like maybe_constant_value but first fully instantiate the argument.
11478 :
11479 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11480 : followed by maybe_constant_value but is more efficient,
11481 : because it calls instantiation_dependent_expression_p and
11482 : potential_constant_expression at most once.
11483 : The manifestly_const_eval argument is passed to maybe_constant_value.
11484 :
11485 : Callers should generally pass their active complain, or if they are in a
11486 : non-template, diagnosing context, they can use the default of
11487 : tf_warning_or_error. Callers that might be within a template context, don't
11488 : have a complain parameter, and aren't going to remember the result for long
11489 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11490 : appropriately. */
11491 :
11492 : tree
11493 265527307 : fold_non_dependent_expr (tree t,
11494 : tsubst_flags_t complain /* = tf_warning_or_error */,
11495 : bool manifestly_const_eval /* = false */,
11496 : tree object /* = NULL_TREE */)
11497 : {
11498 265527307 : if (t == NULL_TREE)
11499 : return NULL_TREE;
11500 :
11501 264425547 : if (processing_template_decl)
11502 28179789 : return fold_non_dependent_expr_template (t, complain,
11503 28179789 : manifestly_const_eval, object);
11504 :
11505 236245758 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11506 : }
11507 :
11508 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11509 : return the original expression. */
11510 :
11511 : tree
11512 2339377 : maybe_fold_non_dependent_expr (tree expr,
11513 : tsubst_flags_t complain/*=tf_warning_or_error*/)
11514 : {
11515 2339377 : tree t = fold_non_dependent_expr (expr, complain);
11516 2339377 : if (t && TREE_CONSTANT (t))
11517 1114915 : return t;
11518 :
11519 : return expr;
11520 : }
11521 :
11522 : /* Like maybe_constant_init but first fully instantiate the argument. */
11523 :
11524 : tree
11525 60931024 : fold_non_dependent_init (tree t,
11526 : tsubst_flags_t complain /*=tf_warning_or_error*/,
11527 : bool manifestly_const_eval /*=false*/,
11528 : tree object /* = NULL_TREE */)
11529 : {
11530 60931024 : if (t == NULL_TREE)
11531 : return NULL_TREE;
11532 :
11533 60931024 : if (processing_template_decl)
11534 : {
11535 1021880 : t = fold_non_dependent_expr_template (t, complain,
11536 : manifestly_const_eval, object);
11537 : /* maybe_constant_init does this stripping, so do it here too. */
11538 1021880 : if (TREE_CODE (t) == TARGET_EXPR)
11539 : {
11540 76 : tree init = TARGET_EXPR_INITIAL (t);
11541 76 : if (TREE_CODE (init) == CONSTRUCTOR)
11542 1021880 : t = init;
11543 : }
11544 1021880 : return t;
11545 : }
11546 :
11547 59909144 : return maybe_constant_init (t, object, manifestly_const_eval);
11548 : }
11549 :
11550 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11551 : than wrapped in a TARGET_EXPR.
11552 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11553 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11554 : per P0595 even when ALLOW_NON_CONSTANT is true. */
11555 :
11556 : static tree
11557 161679711 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11558 : mce_value manifestly_const_eval)
11559 : {
11560 161679711 : if (!t)
11561 : return t;
11562 161679711 : if (TREE_CODE (t) == EXPR_STMT)
11563 24243 : t = TREE_OPERAND (t, 0);
11564 161679711 : if (TREE_CODE (t) == CONVERT_EXPR
11565 161679711 : && VOID_TYPE_P (TREE_TYPE (t)))
11566 79709 : t = TREE_OPERAND (t, 0);
11567 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11568 : DECL and losing that information would cause mischief later. */
11569 161679711 : if (TREE_CODE (t) == INIT_EXPR
11570 161679711 : && (!decl
11571 55482 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11572 55482 : TREE_TYPE (t))))
11573 25419 : t = TREE_OPERAND (t, 1);
11574 161679711 : if (TREE_CODE (t) == TARGET_EXPR)
11575 601489 : t = TARGET_EXPR_INITIAL (t);
11576 161679711 : if (!is_nondependent_static_init_expression (t))
11577 : /* Don't try to evaluate it. */;
11578 138860927 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11579 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11580 : else
11581 : {
11582 : /* [basic.start.static] allows constant-initialization of variables with
11583 : static or thread storage duration even if it isn't required, but we
11584 : shouldn't bend the rules the same way for automatic variables.
11585 :
11586 : But still enforce the requirements of constexpr/constinit.
11587 : [dcl.constinit] "If a variable declared with the constinit specifier
11588 : has dynamic initialization, the program is ill-formed, even if the
11589 : implementation would perform that initialization as a static
11590 : initialization." */
11591 45148512 : bool is_static = (decl && DECL_P (decl)
11592 115936739 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11593 3398574 : bool strict = (!is_static
11594 : || (decl && DECL_P (decl)
11595 3398574 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11596 1068608 : || DECL_DECLARED_CONSTINIT_P (decl))));
11597 : if (is_static)
11598 : manifestly_const_eval = mce_true;
11599 :
11600 71898374 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11601 33784 : return fold_to_constant (t);
11602 :
11603 71864590 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11604 : manifestly_const_eval,
11605 : false, decl);
11606 : }
11607 161645927 : if (TREE_CODE (t) == TARGET_EXPR)
11608 : {
11609 67634 : tree init = TARGET_EXPR_INITIAL (t);
11610 67634 : if (TREE_CODE (init) == CONSTRUCTOR)
11611 161679711 : t = init;
11612 : }
11613 : return t;
11614 : }
11615 :
11616 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11617 :
11618 : tree
11619 102462410 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11620 : {
11621 102462410 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11622 : }
11623 :
11624 : tree
11625 59215791 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11626 : {
11627 59215791 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11628 : }
11629 :
11630 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11631 :
11632 : tree
11633 1510 : cxx_constant_init (tree t, tree decl)
11634 : {
11635 1510 : return maybe_constant_init_1 (t, decl, false, mce_true);
11636 : }
11637 :
11638 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11639 :
11640 : static bool
11641 257265136 : callee_might_throw (tree t)
11642 : {
11643 257265136 : if (cxx_dialect < cxx26 || !flag_exceptions)
11644 : return false;
11645 17884462 : tree callee = cp_get_callee (t);
11646 17884462 : if (callee == NULL_TREE)
11647 : return false;
11648 17852150 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11649 17852150 : return (!flag_enforce_eh_specs
11650 17852150 : || type_dependent_expression_p (callee)
11651 16081046 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11652 32752683 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11653 6004979 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11654 : }
11655 :
11656 : #if 0
11657 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11658 : /* Return true if the object referred to by REF has automatic or thread
11659 : local storage. */
11660 :
11661 : enum { ck_ok, ck_bad, ck_unknown };
11662 : static int
11663 : check_automatic_or_tls (tree ref)
11664 : {
11665 : machine_mode mode;
11666 : poly_int64 bitsize, bitpos;
11667 : tree offset;
11668 : int volatilep = 0, unsignedp = 0;
11669 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11670 : &mode, &unsignedp, &volatilep, false);
11671 : duration_kind dk;
11672 :
11673 : /* If there isn't a decl in the middle, we don't know the linkage here,
11674 : and this isn't a constant expression anyway. */
11675 : if (!DECL_P (decl))
11676 : return ck_unknown;
11677 : dk = decl_storage_duration (decl);
11678 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11679 : }
11680 : #endif
11681 :
11682 : /* Data structure for passing data from potential_constant_expression_1
11683 : to check_for_return_continue via cp_walk_tree. */
11684 : struct check_for_return_continue_data {
11685 : hash_set<tree> *pset;
11686 : tree continue_stmt;
11687 : tree break_stmt;
11688 : bool could_throw;
11689 : };
11690 :
11691 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11692 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11693 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11694 : For C++26 also note presence of possibly throwing calls. */
11695 : static tree
11696 69946771 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11697 : {
11698 69946771 : tree t = *tp, s, b;
11699 69946771 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11700 69946771 : switch (TREE_CODE (t))
11701 : {
11702 : case RETURN_EXPR:
11703 : return t;
11704 :
11705 28 : case CONTINUE_STMT:
11706 28 : if (d->continue_stmt == NULL_TREE)
11707 28 : d->continue_stmt = t;
11708 : break;
11709 :
11710 19576 : case BREAK_STMT:
11711 19576 : if (d->break_stmt == NULL_TREE)
11712 17861 : d->break_stmt = t;
11713 : break;
11714 :
11715 : #define RECUR(x) \
11716 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11717 : d->pset)) \
11718 : return r
11719 :
11720 : /* For loops, walk subtrees manually, so that continue stmts found
11721 : inside of the bodies of the loops are ignored. */
11722 38837 : case DO_STMT:
11723 38837 : *walk_subtrees = 0;
11724 38837 : RECUR (DO_COND (t));
11725 38837 : s = d->continue_stmt;
11726 38837 : b = d->break_stmt;
11727 38837 : RECUR (DO_BODY (t));
11728 38837 : d->continue_stmt = s;
11729 38837 : d->break_stmt = b;
11730 38837 : break;
11731 :
11732 116 : case WHILE_STMT:
11733 116 : *walk_subtrees = 0;
11734 116 : RECUR (WHILE_COND_PREP (t));
11735 116 : RECUR (WHILE_COND (t));
11736 116 : s = d->continue_stmt;
11737 116 : b = d->break_stmt;
11738 116 : RECUR (WHILE_BODY (t));
11739 104 : d->continue_stmt = s;
11740 104 : d->break_stmt = b;
11741 104 : break;
11742 :
11743 214 : case FOR_STMT:
11744 214 : *walk_subtrees = 0;
11745 214 : RECUR (FOR_INIT_STMT (t));
11746 214 : RECUR (FOR_COND_PREP (t));
11747 214 : RECUR (FOR_COND (t));
11748 214 : RECUR (FOR_EXPR (t));
11749 214 : s = d->continue_stmt;
11750 214 : b = d->break_stmt;
11751 214 : RECUR (FOR_BODY (t));
11752 184 : d->continue_stmt = s;
11753 184 : d->break_stmt = b;
11754 184 : break;
11755 :
11756 0 : case RANGE_FOR_STMT:
11757 0 : *walk_subtrees = 0;
11758 0 : RECUR (RANGE_FOR_EXPR (t));
11759 0 : s = d->continue_stmt;
11760 0 : b = d->break_stmt;
11761 0 : RECUR (RANGE_FOR_BODY (t));
11762 0 : d->continue_stmt = s;
11763 0 : d->break_stmt = b;
11764 0 : break;
11765 :
11766 207 : case SWITCH_STMT:
11767 207 : *walk_subtrees = 0;
11768 207 : RECUR (SWITCH_STMT_COND (t));
11769 207 : b = d->break_stmt;
11770 207 : RECUR (SWITCH_STMT_BODY (t));
11771 187 : d->break_stmt = b;
11772 187 : break;
11773 : #undef RECUR
11774 :
11775 : case STATEMENT_LIST:
11776 : case CONSTRUCTOR:
11777 : break;
11778 :
11779 3965423 : case AGGR_INIT_EXPR:
11780 3965423 : case CALL_EXPR:
11781 : /* In C++26 a function could throw. */
11782 3965423 : if (callee_might_throw (t))
11783 49263 : d->could_throw = true;
11784 : break;
11785 :
11786 63855872 : default:
11787 63855872 : if (!EXPR_P (t))
11788 18038134 : *walk_subtrees = 0;
11789 : break;
11790 : }
11791 :
11792 : return NULL_TREE;
11793 : }
11794 :
11795 : /* Return true if T denotes a potentially constant expression. Issue
11796 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11797 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11798 : consider the expression in the current context, independent of constexpr
11799 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11800 : and hard errors should not be reported by constexpr_error.
11801 :
11802 : C++0x [expr.const] used to say
11803 :
11804 : 6 An expression is a potential constant expression if it is
11805 : a constant expression where all occurrences of function
11806 : parameters are replaced by arbitrary constant expressions
11807 : of the appropriate type.
11808 :
11809 : 2 A conditional expression is a constant expression unless it
11810 : involves one of the following as a potentially evaluated
11811 : subexpression (3.2), but subexpressions of logical AND (5.14),
11812 : logical OR (5.15), and conditional (5.16) operations that are
11813 : not evaluated are not considered. */
11814 :
11815 : static bool
11816 5317106646 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11817 : bool fundef_p, tsubst_flags_t flags,
11818 : tree *jump_target)
11819 : {
11820 : #define RECUR(T,RV) \
11821 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11822 : jump_target)
11823 :
11824 5317106646 : enum { any = false, rval = true };
11825 5317106646 : int i;
11826 5317106646 : tree tmp;
11827 :
11828 5317106646 : if (t == error_mark_node)
11829 : return false;
11830 5317095418 : if (t == NULL_TREE)
11831 : return true;
11832 5305865316 : location_t loc = cp_expr_loc_or_input_loc (t);
11833 5305865316 : iloc_sentinel ils = loc;
11834 :
11835 5305865316 : if (*jump_target)
11836 : /* If we are jumping, ignore everything. This is simpler than the
11837 : cxx_eval_constant_expression handling because we only need to be
11838 : conservatively correct, and we don't necessarily have a constant value
11839 : available, so we don't bother with switch tracking. */
11840 : return true;
11841 :
11842 1815354 : if (TREE_THIS_VOLATILE (t) && want_rval
11843 1254693 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11844 5296687316 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11845 : {
11846 77836 : if (TREE_CLOBBER_P (t))
11847 : {
11848 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
11849 0 : gcc_checking_assert (false);
11850 : return true;
11851 : }
11852 :
11853 77836 : if (flags & tf_error)
11854 22 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11855 : "a volatile lvalue %qE with type %qT", t,
11856 22 : TREE_TYPE (t));
11857 77836 : return false;
11858 : }
11859 5296531618 : if (CONSTANT_CLASS_P (t))
11860 : return true;
11861 3965260143 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11862 3965260143 : && TREE_TYPE (t) == error_mark_node)
11863 : return false;
11864 :
11865 3965260112 : switch (TREE_CODE (t))
11866 : {
11867 : case FUNCTION_DECL:
11868 : case BASELINK:
11869 : case TEMPLATE_DECL:
11870 : case OVERLOAD:
11871 : case TEMPLATE_ID_EXPR:
11872 : case LABEL_DECL:
11873 : case CASE_LABEL_EXPR:
11874 : case PREDICT_EXPR:
11875 : case CONST_DECL:
11876 : case SIZEOF_EXPR:
11877 : case ALIGNOF_EXPR:
11878 : case OFFSETOF_EXPR:
11879 : case NOEXCEPT_EXPR:
11880 : case TEMPLATE_PARM_INDEX:
11881 : case TRAIT_EXPR:
11882 : case IDENTIFIER_NODE:
11883 : case USERDEF_LITERAL:
11884 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11885 : case FIELD_DECL:
11886 : case RESULT_DECL:
11887 : case USING_DECL:
11888 : case USING_STMT:
11889 : case PLACEHOLDER_EXPR:
11890 : case REQUIRES_EXPR:
11891 : case STATIC_ASSERT:
11892 : case DEBUG_BEGIN_STMT:
11893 : case REFLECT_EXPR:
11894 : return true;
11895 :
11896 26498213 : case RETURN_EXPR:
11897 26498213 : if (!RECUR (TREE_OPERAND (t, 0), any))
11898 : return false;
11899 : /* FALLTHROUGH */
11900 :
11901 26158921 : case BREAK_STMT:
11902 26158921 : case CONTINUE_STMT:
11903 26158921 : *jump_target = t;
11904 26158921 : return true;
11905 :
11906 405497568 : case PARM_DECL:
11907 405497568 : if (now && want_rval)
11908 : {
11909 96035585 : tree type = TREE_TYPE (t);
11910 96035585 : if (dependent_type_p (type)
11911 68903918 : || !COMPLETE_TYPE_P (processing_template_decl
11912 : ? type : complete_type (type))
11913 164939503 : || is_really_empty_class (type, /*ignore_vptr*/false))
11914 : /* An empty class has no data to read. */
11915 27132183 : return true;
11916 68903402 : if (flags & tf_error)
11917 17 : constexpr_error (input_location, fundef_p,
11918 : "%qE is not a constant expression", t);
11919 68903402 : return false;
11920 : }
11921 : return true;
11922 :
11923 316140616 : case AGGR_INIT_EXPR:
11924 316140616 : case CALL_EXPR:
11925 : /* -- an invocation of a function other than a constexpr function
11926 : or a constexpr constructor. */
11927 316140616 : {
11928 316140616 : tree fun = get_function_named_in_call (t);
11929 316140616 : const int nargs = call_expr_nargs (t);
11930 316140616 : i = 0;
11931 :
11932 316140616 : if (fun == NULL_TREE)
11933 : {
11934 : /* Reset to allow the function to continue past the end
11935 : of the block below. Otherwise return early. */
11936 392435 : bool bail = true;
11937 :
11938 392435 : if (TREE_CODE (t) == CALL_EXPR
11939 392435 : && CALL_EXPR_FN (t) == NULL_TREE)
11940 392435 : switch (CALL_EXPR_IFN (t))
11941 : {
11942 : /* These should be ignored, they are optimized away from
11943 : constexpr functions. */
11944 : case IFN_UBSAN_NULL:
11945 : case IFN_UBSAN_BOUNDS:
11946 : case IFN_UBSAN_VPTR:
11947 : case IFN_FALLTHROUGH:
11948 : case IFN_ASSUME:
11949 : return true;
11950 :
11951 : case IFN_ADD_OVERFLOW:
11952 : case IFN_SUB_OVERFLOW:
11953 : case IFN_MUL_OVERFLOW:
11954 : case IFN_LAUNDER:
11955 : case IFN_VEC_CONVERT:
11956 : bail = false;
11957 : break;
11958 :
11959 : default:
11960 : break;
11961 : }
11962 :
11963 : if (bail)
11964 : {
11965 : /* fold_call_expr can't do anything with IFN calls. */
11966 53 : if (flags & tf_error)
11967 0 : constexpr_error (loc, fundef_p,
11968 : "call to internal function %qE", t);
11969 53 : return false;
11970 : }
11971 : }
11972 :
11973 315748181 : if (fun && is_overloaded_fn (fun))
11974 : {
11975 296271916 : if (!RECUR (fun, true))
11976 : return false;
11977 295851661 : fun = get_fns (fun);
11978 :
11979 295851661 : if (TREE_CODE (fun) == FUNCTION_DECL)
11980 : {
11981 278532175 : if (builtin_valid_in_constant_expr_p (fun))
11982 : return true;
11983 276448323 : if (!maybe_constexpr_fn (fun)
11984 : /* Allow any built-in function; if the expansion
11985 : isn't constant, we'll deal with that then. */
11986 55519169 : && !fndecl_built_in_p (fun)
11987 : /* In C++20, replaceable global allocation functions
11988 : are constant expressions. */
11989 41743714 : && (!cxx_replaceable_global_alloc_fn (fun)
11990 872678 : || TREE_CODE (t) != CALL_EXPR
11991 872678 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
11992 260224 : && (current_function_decl == NULL_TREE
11993 260224 : || !is_std_allocator_allocate
11994 260224 : (current_function_decl))))
11995 : /* Allow placement new in std::construct_at. */
11996 40875014 : && (!cxx_placement_new_fn (fun)
11997 636587 : || TREE_CODE (t) != CALL_EXPR
11998 636587 : || current_function_decl == NULL_TREE
11999 636575 : || !is_std_construct_at (current_function_decl))
12000 40481152 : && !cxx_dynamic_cast_fn_p (fun)
12001 316926671 : && !cxx_cxa_builtin_fn_p (fun))
12002 : {
12003 : /* In C++26 evaluation of the function arguments might
12004 : throw and in that case it is irrelevant whether
12005 : fun is constexpr or not. */
12006 40435741 : if (cxx_dialect >= cxx26)
12007 5409161 : for (; i < nargs; ++i)
12008 : {
12009 3307688 : tree x = get_nth_callarg (t, i);
12010 3307688 : bool rv = processing_template_decl ? any : rval;
12011 3307688 : bool sub_now = false;
12012 3307688 : if (!potential_constant_expression_1 (x, rv, strict,
12013 : sub_now,
12014 : fundef_p,
12015 : flags,
12016 : jump_target))
12017 : return false;
12018 3012879 : if (throws (jump_target))
12019 : return true;
12020 : }
12021 40093488 : if ((flags & tf_error)
12022 40093488 : && constexpr_error (loc, fundef_p,
12023 : "call to non-%<constexpr%> "
12024 : "function %qD", fun))
12025 251 : explain_invalid_constexpr_fn (fun);
12026 40093488 : return false;
12027 : }
12028 : }
12029 :
12030 253332068 : fun = OVL_FIRST (fun);
12031 : /* Skip initial arguments to base constructors. */
12032 253332068 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12033 4072552 : i = num_artificial_parms_for (fun);
12034 : }
12035 19476265 : else if (fun)
12036 : {
12037 19476265 : if (TREE_TYPE (fun)
12038 19476265 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12039 : want_rval = rval;
12040 : else
12041 : want_rval = any;
12042 19476265 : if (RECUR (fun, want_rval))
12043 : /* Might end up being a constant function pointer. But it
12044 : could also be a function object with constexpr op(), so
12045 : we pass 'any' so that the underlying VAR_DECL is deemed
12046 : as potentially-constant even though it wasn't declared
12047 : constexpr. */;
12048 : else
12049 : return false;
12050 : }
12051 607947996 : for (; i < nargs; ++i)
12052 : {
12053 354615160 : tree x = get_nth_callarg (t, i);
12054 : /* In a template, reference arguments haven't been converted to
12055 : REFERENCE_TYPE and we might not even know if the parameter
12056 : is a reference, so accept lvalue constants too. */
12057 354615160 : bool rv = processing_template_decl ? any : rval;
12058 : /* Don't require an immediately constant value, as constexpr
12059 : substitution might not use the value of the argument. */
12060 354615160 : bool sub_now = false;
12061 354615160 : if (!potential_constant_expression_1 (x, rv, strict,
12062 : sub_now, fundef_p, flags,
12063 : jump_target))
12064 : return false;
12065 3385551813 : if (throws (jump_target))
12066 : return true;
12067 : }
12068 : /* In C++26 a function could throw. */
12069 253332836 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12070 5749052 : *jump_target = void_node;
12071 : return true;
12072 : }
12073 :
12074 208777692 : case NON_LVALUE_EXPR:
12075 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
12076 : -- an lvalue of integral type that refers to a non-volatile
12077 : const variable or static data member initialized with
12078 : constant expressions, or
12079 :
12080 : -- an lvalue of literal type that refers to non-volatile
12081 : object defined with constexpr, or that refers to a
12082 : sub-object of such an object; */
12083 208777692 : return RECUR (TREE_OPERAND (t, 0), rval);
12084 :
12085 25503 : case EXCESS_PRECISION_EXPR:
12086 25503 : return RECUR (TREE_OPERAND (t, 0), rval);
12087 :
12088 380224655 : case VAR_DECL:
12089 380224655 : if (DECL_HAS_VALUE_EXPR_P (t))
12090 : {
12091 10141717 : if (now && is_normal_capture_proxy (t))
12092 : {
12093 : /* -- in a lambda-expression, a reference to this or to a
12094 : variable with automatic storage duration defined outside that
12095 : lambda-expression, where the reference would be an
12096 : odr-use. */
12097 :
12098 1448861 : if (want_rval)
12099 : /* Since we're doing an lvalue-rvalue conversion, this might
12100 : not be an odr-use, so evaluate the variable directly. */
12101 1447739 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
12102 :
12103 1122 : if (flags & tf_error)
12104 : {
12105 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
12106 3 : auto_diagnostic_group d;
12107 3 : if (constexpr_error (input_location, fundef_p,
12108 : "lambda capture of %qE is not a "
12109 : "constant expression", cap)
12110 3 : && decl_constant_var_p (cap))
12111 3 : inform (input_location, "because it is used as a glvalue");
12112 3 : }
12113 1122 : return false;
12114 : }
12115 8692856 : tree ve = DECL_VALUE_EXPR (t);
12116 : /* Treat __PRETTY_FUNCTION__ inside a template function as
12117 : potentially-constant. */
12118 8692856 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
12119 : return true;
12120 8692851 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
12121 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
12122 8690115 : return RECUR (ve, rval);
12123 : }
12124 370082938 : if (want_rval
12125 247652276 : && (now || !var_in_maybe_constexpr_fn (t))
12126 224933418 : && !type_dependent_expression_p (t)
12127 194447142 : && !decl_maybe_constant_var_p (t)
12128 63208081 : && !is_local_temp (t)
12129 62496288 : && (strict
12130 3135080 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
12131 645548 : || (DECL_INITIAL (t)
12132 640325 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
12133 62489874 : && COMPLETE_TYPE_P (TREE_TYPE (t))
12134 432572668 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
12135 : {
12136 62484969 : if (flags & tf_error)
12137 163 : non_const_var_error (loc, t, fundef_p);
12138 62484969 : return false;
12139 : }
12140 : return true;
12141 :
12142 503847719 : case NOP_EXPR:
12143 503847719 : if (REINTERPRET_CAST_P (t))
12144 : {
12145 172949 : if (flags & tf_error)
12146 46 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
12147 : "constant expression");
12148 172949 : return false;
12149 : }
12150 : /* FALLTHRU */
12151 994796149 : case CONVERT_EXPR:
12152 994796149 : case VIEW_CONVERT_EXPR:
12153 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
12154 : may change to something more specific to type-punning (DR 1312). */
12155 994796149 : {
12156 994796149 : tree from = TREE_OPERAND (t, 0);
12157 994796149 : if (location_wrapper_p (t))
12158 395776881 : return (RECUR (from, want_rval));
12159 599019268 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
12160 : {
12161 352691292 : STRIP_ANY_LOCATION_WRAPPER (from);
12162 352691292 : if (TREE_CODE (from) == INTEGER_CST
12163 352691292 : && !integer_zerop (from))
12164 : {
12165 1518 : if (flags & tf_error)
12166 25 : constexpr_error (loc, fundef_p,
12167 : "%<reinterpret_cast%> from integer to "
12168 : "pointer");
12169 1518 : return false;
12170 : }
12171 : }
12172 599017750 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
12173 : }
12174 :
12175 11751 : case ADDRESSOF_EXPR:
12176 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
12177 11751 : t = TREE_OPERAND (t, 0);
12178 11751 : goto handle_addr_expr;
12179 :
12180 130568560 : case ADDR_EXPR:
12181 : /* -- a unary operator & that is applied to an lvalue that
12182 : designates an object with thread or automatic storage
12183 : duration; */
12184 130568560 : t = TREE_OPERAND (t, 0);
12185 :
12186 130568560 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
12187 : /* A pointer-to-member constant. */
12188 : return true;
12189 :
12190 130580073 : handle_addr_expr:
12191 : #if 0
12192 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
12193 : any checking here, as we might dereference the pointer later. If
12194 : we remove this code, also remove check_automatic_or_tls. */
12195 : i = check_automatic_or_tls (t);
12196 : if (i == ck_ok)
12197 : return true;
12198 : if (i == ck_bad)
12199 : {
12200 : if (flags & tf_error)
12201 : error ("address-of an object %qE with thread local or "
12202 : "automatic storage is not a constant expression", t);
12203 : return false;
12204 : }
12205 : #endif
12206 130580073 : return RECUR (t, any);
12207 :
12208 139680961 : case COMPONENT_REF:
12209 139680961 : case ARROW_EXPR:
12210 139680961 : case OFFSET_REF:
12211 : /* -- a class member access unless its postfix-expression is
12212 : of literal type or of pointer to literal type. */
12213 : /* This test would be redundant, as it follows from the
12214 : postfix-expression being a potential constant expression. */
12215 139680961 : if (type_unknown_p (t))
12216 : return true;
12217 130924328 : if (is_overloaded_fn (t))
12218 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
12219 : which uses ob as an lvalue. */
12220 132445621 : want_rval = false;
12221 132445621 : gcc_fallthrough ();
12222 :
12223 132445621 : case REALPART_EXPR:
12224 132445621 : case IMAGPART_EXPR:
12225 132445621 : case BIT_FIELD_REF:
12226 132445621 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12227 :
12228 249892 : case EXPR_PACK_EXPANSION:
12229 249892 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
12230 :
12231 : case PACK_INDEX_EXPR:
12232 : return true;
12233 :
12234 140569435 : case INDIRECT_REF:
12235 140569435 : return RECUR (TREE_OPERAND (t, 0), rval);
12236 :
12237 27353255 : case STATEMENT_LIST:
12238 5414446025 : for (tree stmt : tsi_range (t))
12239 82733071 : if (!RECUR (stmt, any))
12240 317963746 : return false;
12241 : return true;
12242 :
12243 5376509 : case MODIFY_EXPR:
12244 5376509 : if (cxx_dialect < cxx14)
12245 1983 : goto fail;
12246 5374526 : if (!RECUR (TREE_OPERAND (t, 0), any))
12247 : return false;
12248 : /* Just ignore clobbers. */
12249 4904503 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12250 : return true;
12251 4325110 : if (!RECUR (TREE_OPERAND (t, 1), rval))
12252 : return false;
12253 : return true;
12254 :
12255 893144 : case MODOP_EXPR:
12256 893144 : if (cxx_dialect < cxx14)
12257 96 : goto fail;
12258 893048 : if (!RECUR (TREE_OPERAND (t, 0), rval))
12259 : return false;
12260 883215 : if (!RECUR (TREE_OPERAND (t, 2), rval))
12261 : return false;
12262 : return true;
12263 :
12264 631257 : case DO_STMT:
12265 631257 : if (!RECUR (DO_COND (t), rval))
12266 : return false;
12267 631257 : if (!RECUR (DO_BODY (t), any))
12268 : return false;
12269 630965 : if (breaks (jump_target) || continues (jump_target))
12270 2 : *jump_target = NULL_TREE;
12271 : return true;
12272 :
12273 443761 : case FOR_STMT:
12274 443761 : if (!RECUR (FOR_INIT_STMT (t), any))
12275 : return false;
12276 443761 : if (!RECUR (FOR_COND_PREP (t), any))
12277 : return false;
12278 443761 : tmp = FOR_COND (t);
12279 443761 : if (!RECUR (tmp, rval))
12280 : return false;
12281 443693 : if (tmp)
12282 : {
12283 399629 : if (!processing_template_decl)
12284 396638 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12285 : /* If we couldn't evaluate the condition, it might not ever be
12286 : true. */
12287 399629 : if (!integer_onep (tmp))
12288 : {
12289 : /* Before returning true, check if the for body can contain
12290 : a return. */
12291 399629 : hash_set<tree> pset;
12292 399629 : check_for_return_continue_data data = { &pset, NULL_TREE,
12293 399629 : NULL_TREE, false };
12294 399629 : if (tree ret_expr
12295 399629 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
12296 : &data, &pset))
12297 144462 : *jump_target = ret_expr;
12298 399629 : if (data.could_throw)
12299 9760 : *jump_target = void_node;
12300 399629 : return true;
12301 399629 : }
12302 : }
12303 44064 : if (!RECUR (FOR_EXPR (t), any))
12304 : return false;
12305 44064 : if (!RECUR (FOR_BODY (t), any))
12306 : return false;
12307 44064 : if (!RECUR (FOR_COND_CLEANUP (t), any))
12308 : return false;
12309 44064 : if (breaks (jump_target) || continues (jump_target))
12310 12 : *jump_target = NULL_TREE;
12311 : return true;
12312 :
12313 449 : case RANGE_FOR_STMT:
12314 449 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
12315 : return false;
12316 449 : if (!RECUR (RANGE_FOR_EXPR (t), any))
12317 : return false;
12318 449 : if (!RECUR (RANGE_FOR_BODY (t), any))
12319 : return false;
12320 447 : if (breaks (jump_target) || continues (jump_target))
12321 0 : *jump_target = NULL_TREE;
12322 : return true;
12323 :
12324 228397 : case WHILE_STMT:
12325 228397 : if (!RECUR (WHILE_COND_PREP (t), any))
12326 : return false;
12327 228397 : tmp = WHILE_COND (t);
12328 228397 : if (!RECUR (tmp, rval))
12329 : return false;
12330 228333 : if (!processing_template_decl)
12331 228320 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12332 : /* If we couldn't evaluate the condition, it might not ever be true. */
12333 228333 : if (!integer_onep (tmp))
12334 : {
12335 : /* Before returning true, check if the while body can contain
12336 : a return. */
12337 218685 : hash_set<tree> pset;
12338 218685 : check_for_return_continue_data data = { &pset, NULL_TREE,
12339 218685 : NULL_TREE, false };
12340 218685 : if (tree ret_expr
12341 218685 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
12342 : &data, &pset))
12343 417 : *jump_target = ret_expr;
12344 218685 : if (data.could_throw)
12345 3001 : *jump_target = void_node;
12346 218685 : return true;
12347 218685 : }
12348 9648 : if (!RECUR (WHILE_BODY (t), any))
12349 : return false;
12350 9637 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
12351 : return false;
12352 9637 : if (breaks (jump_target) || continues (jump_target))
12353 24 : *jump_target = NULL_TREE;
12354 : return true;
12355 :
12356 73941 : case SWITCH_STMT:
12357 73941 : if (!RECUR (SWITCH_STMT_COND (t), rval))
12358 : return false;
12359 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
12360 : unreachable labels would be checked and it is enough if there is
12361 : a single switch cond value for which it is a valid constant
12362 : expression. We need to check if there are any RETURN_EXPRs
12363 : or CONTINUE_STMTs inside of the body though, as in that case
12364 : we need to set *jump_target. */
12365 : else
12366 : {
12367 73935 : hash_set<tree> pset;
12368 73935 : check_for_return_continue_data data = { &pset, NULL_TREE,
12369 73935 : NULL_TREE, false };
12370 73935 : if (tree ret_expr
12371 73935 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
12372 : &data, &pset))
12373 : /* The switch might return. */
12374 73555 : *jump_target = ret_expr;
12375 380 : else if (data.continue_stmt)
12376 : /* The switch can't return, but might continue. */
12377 3 : *jump_target = data.continue_stmt;
12378 73935 : if (data.could_throw)
12379 25 : *jump_target = void_node;
12380 73935 : }
12381 73935 : return true;
12382 :
12383 50 : case STMT_EXPR:
12384 50 : return RECUR (STMT_EXPR_STMT (t), rval);
12385 :
12386 531341 : case LAMBDA_EXPR:
12387 531341 : if (cxx_dialect >= cxx17)
12388 : /* In C++17 lambdas can be constexpr, don't give up yet. */
12389 : return true;
12390 423 : else if (flags & tf_error)
12391 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
12392 : "constant expression before C++17");
12393 : return false;
12394 :
12395 108391 : case NEW_EXPR:
12396 108391 : case VEC_NEW_EXPR:
12397 108391 : case DELETE_EXPR:
12398 108391 : case VEC_DELETE_EXPR:
12399 108391 : if (cxx_dialect >= cxx20)
12400 : /* In C++20, new-expressions are potentially constant. */
12401 : return true;
12402 1044 : else if (flags & tf_error)
12403 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
12404 : "constant expression before C++20");
12405 : return false;
12406 :
12407 79329 : case DYNAMIC_CAST_EXPR:
12408 79329 : case PSEUDO_DTOR_EXPR:
12409 79329 : case OMP_PARALLEL:
12410 79329 : case OMP_TASK:
12411 79329 : case OMP_FOR:
12412 79329 : case OMP_SIMD:
12413 79329 : case OMP_DISTRIBUTE:
12414 79329 : case OMP_TASKLOOP:
12415 79329 : case OMP_LOOP:
12416 79329 : case OMP_TEAMS:
12417 79329 : case OMP_TARGET_DATA:
12418 79329 : case OMP_TARGET:
12419 79329 : case OMP_SECTIONS:
12420 79329 : case OMP_ORDERED:
12421 79329 : case OMP_CRITICAL:
12422 79329 : case OMP_SINGLE:
12423 79329 : case OMP_SCAN:
12424 79329 : case OMP_SCOPE:
12425 79329 : case OMP_SECTION:
12426 79329 : case OMP_MASTER:
12427 79329 : case OMP_MASKED:
12428 79329 : case OMP_TASKGROUP:
12429 79329 : case OMP_TARGET_UPDATE:
12430 79329 : case OMP_TARGET_ENTER_DATA:
12431 79329 : case OMP_TARGET_EXIT_DATA:
12432 79329 : case OMP_ATOMIC:
12433 79329 : case OMP_ATOMIC_READ:
12434 79329 : case OMP_ATOMIC_CAPTURE_OLD:
12435 79329 : case OMP_ATOMIC_CAPTURE_NEW:
12436 79329 : case OMP_DEPOBJ:
12437 79329 : case OACC_PARALLEL:
12438 79329 : case OACC_KERNELS:
12439 79329 : case OACC_SERIAL:
12440 79329 : case OACC_DATA:
12441 79329 : case OACC_HOST_DATA:
12442 79329 : case OACC_LOOP:
12443 79329 : case OACC_CACHE:
12444 79329 : case OACC_DECLARE:
12445 79329 : case OACC_ENTER_DATA:
12446 79329 : case OACC_EXIT_DATA:
12447 79329 : case OACC_UPDATE:
12448 79329 : case OMP_ARRAY_SECTION:
12449 : /* GCC internal stuff. */
12450 79329 : case VA_ARG_EXPR:
12451 79329 : case TRANSACTION_EXPR:
12452 79329 : case AT_ENCODE_EXPR:
12453 79329 : fail:
12454 79329 : if (flags & tf_error)
12455 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12456 : "expression", t);
12457 : return false;
12458 :
12459 : case OMP_DECLARE_MAPPER:
12460 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12461 : constant. */
12462 : return true;
12463 :
12464 14328 : case THROW_EXPR:
12465 14328 : if (cxx_dialect < cxx26)
12466 279 : goto fail;
12467 14049 : return RECUR (TREE_OPERAND (t, 0), rval);
12468 :
12469 843 : case ASM_EXPR:
12470 843 : if (flags & tf_error)
12471 5 : inline_asm_in_constexpr_error (loc, fundef_p);
12472 : return false;
12473 :
12474 630549 : case OBJ_TYPE_REF:
12475 630549 : if (cxx_dialect >= cxx20)
12476 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12477 : return true;
12478 6605 : else if (flags & tf_error)
12479 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12480 : "%<constexpr%> before C++20");
12481 : return false;
12482 :
12483 4232 : case TYPEID_EXPR:
12484 : /* In C++20, a typeid expression whose operand is of polymorphic
12485 : class type can be constexpr. */
12486 4232 : {
12487 4232 : tree e = TREE_OPERAND (t, 0);
12488 4232 : if (cxx_dialect < cxx20
12489 26 : && strict
12490 26 : && !TYPE_P (e)
12491 19 : && !type_dependent_expression_p (e)
12492 7 : && CLASS_TYPE_P (TREE_TYPE (e))
12493 4237 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12494 : {
12495 4 : if (flags & tf_error)
12496 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12497 : "constant expression because %qE is "
12498 : "of polymorphic type", e);
12499 4 : return false;
12500 : }
12501 : return true;
12502 : }
12503 :
12504 30650717 : case POINTER_DIFF_EXPR:
12505 30650717 : case MINUS_EXPR:
12506 30650717 : want_rval = true;
12507 30650717 : goto binary;
12508 :
12509 94091383 : case LT_EXPR:
12510 94091383 : case LE_EXPR:
12511 94091383 : case GT_EXPR:
12512 94091383 : case GE_EXPR:
12513 94091383 : case EQ_EXPR:
12514 94091383 : case NE_EXPR:
12515 94091383 : case SPACESHIP_EXPR:
12516 94091383 : want_rval = true;
12517 94091383 : goto binary;
12518 :
12519 2543712 : case PREINCREMENT_EXPR:
12520 2543712 : case POSTINCREMENT_EXPR:
12521 2543712 : case PREDECREMENT_EXPR:
12522 2543712 : case POSTDECREMENT_EXPR:
12523 2543712 : if (cxx_dialect < cxx14)
12524 8586 : goto fail;
12525 2535126 : goto unary;
12526 :
12527 1120473 : case BIT_NOT_EXPR:
12528 : /* A destructor. */
12529 1120473 : if (TYPE_P (TREE_OPERAND (t, 0)))
12530 : return true;
12531 : /* fall through. */
12532 :
12533 46787087 : case CONJ_EXPR:
12534 46787087 : case SAVE_EXPR:
12535 46787087 : case FIX_TRUNC_EXPR:
12536 46787087 : case FLOAT_EXPR:
12537 46787087 : case NEGATE_EXPR:
12538 46787087 : case ABS_EXPR:
12539 46787087 : case ABSU_EXPR:
12540 46787087 : case TRUTH_NOT_EXPR:
12541 46787087 : case FIXED_CONVERT_EXPR:
12542 46787087 : case UNARY_PLUS_EXPR:
12543 46787087 : case UNARY_LEFT_FOLD_EXPR:
12544 46787087 : case UNARY_RIGHT_FOLD_EXPR:
12545 46787087 : case VEC_DUPLICATE_EXPR:
12546 1120473 : unary:
12547 46787087 : return RECUR (TREE_OPERAND (t, 0), rval);
12548 :
12549 30633991 : case CAST_EXPR:
12550 30633991 : case CONST_CAST_EXPR:
12551 30633991 : case STATIC_CAST_EXPR:
12552 30633991 : case REINTERPRET_CAST_EXPR:
12553 30633991 : case IMPLICIT_CONV_EXPR:
12554 30633991 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12555 : /* In C++98, a conversion to non-integral type can't be part of a
12556 : constant expression. */
12557 : {
12558 323 : if (flags & tf_error)
12559 0 : constexpr_error (loc, fundef_p,
12560 : "cast to non-integral type %qT in a constant "
12561 0 : "expression", TREE_TYPE (t));
12562 323 : return false;
12563 : }
12564 : /* This might be a conversion from a class to a (potentially) literal
12565 : type. Let's consider it potentially constant since the conversion
12566 : might be a constexpr user-defined conversion. */
12567 30633668 : else if (cxx_dialect >= cxx11
12568 30613052 : && (dependent_type_p (TREE_TYPE (t))
12569 10982407 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12570 10969492 : || literal_type_p (TREE_TYPE (t)))
12571 30576606 : && TREE_OPERAND (t, 0)
12572 60973612 : && (TREE_CODE (t) != CAST_EXPR
12573 23102313 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12574 : {
12575 30299868 : tree from = TREE_OPERAND (t, 0);
12576 30299868 : if (TREE_CODE (t) == CAST_EXPR)
12577 23062237 : from = TREE_VALUE (from);
12578 30299868 : tree type = TREE_TYPE (from);
12579 : /* If this is a dependent type, it could end up being a class
12580 : with conversions. */
12581 30299868 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12582 : return true;
12583 : /* Or a non-dependent class which has conversions. */
12584 603932 : else if (CLASS_TYPE_P (type)
12585 603932 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12586 266495 : return true;
12587 : }
12588 :
12589 24251029 : return (RECUR (TREE_OPERAND (t, 0),
12590 24251029 : !TYPE_REF_P (TREE_TYPE (t))));
12591 :
12592 18834413 : case BIND_EXPR:
12593 18834413 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12594 :
12595 82179263 : case CLEANUP_POINT_EXPR:
12596 82179263 : case MUST_NOT_THROW_EXPR:
12597 82179263 : case TRY_CATCH_EXPR:
12598 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12599 : body could throw, even with catch (...) among handlers we'd need
12600 : to analyze them in detail if they couldn't rethrow it. More
12601 : importantly though, throws (jump_target) is just conservative,
12602 : and there could be e.g.
12603 : try
12604 : {
12605 : possibly_throwing_fn (args);
12606 : break;
12607 : }
12608 : catch (...)
12609 : {
12610 : }
12611 : or continue or return instead of break. So, clearing *jump_target
12612 : because we see catch (...) handler might mean we missed break
12613 : etc. */
12614 82179263 : case TRY_BLOCK:
12615 82179263 : case EH_SPEC_BLOCK:
12616 82179263 : case EXPR_STMT:
12617 82179263 : case PAREN_EXPR:
12618 : /* For convenience. */
12619 82179263 : case LOOP_EXPR:
12620 82179263 : case EXIT_EXPR:
12621 82179263 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12622 :
12623 16113069 : case DECL_EXPR:
12624 16113069 : tmp = DECL_EXPR_DECL (t);
12625 11901773 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12626 23938629 : && (processing_template_decl
12627 7085143 : ? !decl_maybe_constant_var_p (tmp)
12628 6344726 : : !decl_constant_var_p (tmp)))
12629 : {
12630 6489534 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12631 : {
12632 8 : if (flags & tf_error)
12633 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12634 : "%qD defined %<thread_local%> in "
12635 : "%<constexpr%> context", tmp);
12636 8 : return false;
12637 : }
12638 6489526 : else if (TREE_STATIC (tmp))
12639 : {
12640 112 : if (flags & tf_error)
12641 25 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12642 : "%qD defined %<static%> in %<constexpr%> "
12643 : "context", tmp);
12644 112 : return false;
12645 : }
12646 6489414 : else if (!check_for_uninitialized_const_var
12647 6489414 : (tmp, /*constexpr_context_p=*/true, flags))
12648 : return false;
12649 : }
12650 16109214 : if (VAR_P (tmp))
12651 11897918 : return RECUR (DECL_INITIAL (tmp), want_rval);
12652 : return true;
12653 :
12654 26904 : case TRY_FINALLY_EXPR:
12655 26904 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12656 26904 : && RECUR (TREE_OPERAND (t, 1), any));
12657 :
12658 24452241 : case SCOPE_REF:
12659 24452241 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12660 :
12661 55811855 : case TARGET_EXPR:
12662 55811855 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12663 55608921 : && !TARGET_EXPR_ELIDING_P (t)
12664 97695018 : && !literal_type_p (TREE_TYPE (t)))
12665 : {
12666 866974 : if (flags & tf_error)
12667 : {
12668 23 : auto_diagnostic_group d;
12669 23 : if (constexpr_error (loc, fundef_p,
12670 : "temporary of non-literal type %qT in a "
12671 23 : "constant expression", TREE_TYPE (t)))
12672 23 : explain_non_literal_class (TREE_TYPE (t));
12673 23 : }
12674 866974 : return false;
12675 : }
12676 : /* FALLTHRU */
12677 86718129 : case INIT_EXPR:
12678 86718129 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12679 : return true;
12680 86654283 : return RECUR (TREE_OPERAND (t, 1), rval);
12681 :
12682 44985788 : case CONSTRUCTOR:
12683 44985788 : {
12684 44985788 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12685 44985788 : constructor_elt *ce;
12686 5578912917 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12687 229546498 : if (!RECUR (ce->value, want_rval))
12688 : return false;
12689 : return true;
12690 : }
12691 :
12692 20691698 : case TREE_LIST:
12693 20691698 : {
12694 20691698 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12695 : || DECL_P (TREE_PURPOSE (t)));
12696 20691698 : if (!RECUR (TREE_VALUE (t), want_rval))
12697 : return false;
12698 18375170 : if (TREE_CHAIN (t) == NULL_TREE)
12699 : return true;
12700 67704 : return RECUR (TREE_CHAIN (t), want_rval);
12701 : }
12702 :
12703 14822417 : case TRUNC_DIV_EXPR:
12704 14822417 : case CEIL_DIV_EXPR:
12705 14822417 : case FLOOR_DIV_EXPR:
12706 14822417 : case ROUND_DIV_EXPR:
12707 14822417 : case TRUNC_MOD_EXPR:
12708 14822417 : case CEIL_MOD_EXPR:
12709 14822417 : case ROUND_MOD_EXPR:
12710 14822417 : {
12711 14822417 : tree denom = TREE_OPERAND (t, 1);
12712 14822417 : if (!RECUR (denom, rval))
12713 : return false;
12714 : /* We can't call cxx_eval_outermost_constant_expr on an expression
12715 : that hasn't been through instantiate_non_dependent_expr yet. */
12716 14385214 : if (!processing_template_decl)
12717 7638374 : denom = cxx_eval_outermost_constant_expr (denom, true);
12718 14385214 : if (integer_zerop (denom))
12719 : {
12720 337 : if (flags & tf_error)
12721 35 : constexpr_error (input_location, fundef_p,
12722 : "division by zero is not a constant expression");
12723 337 : return false;
12724 : }
12725 : else
12726 : {
12727 14384877 : want_rval = true;
12728 14384877 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12729 : }
12730 : }
12731 :
12732 7442786 : case COMPOUND_EXPR:
12733 7442786 : {
12734 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12735 : COMPOUND_EXPR; don't get confused. */
12736 7442786 : tree op0 = TREE_OPERAND (t, 0);
12737 7442786 : tree op1 = TREE_OPERAND (t, 1);
12738 7442786 : STRIP_NOPS (op1);
12739 7442786 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12740 1385648 : return RECUR (op0, want_rval);
12741 : else
12742 6057138 : goto binary;
12743 : }
12744 :
12745 : /* If the first operand is the non-short-circuit constant, look at
12746 : the second operand; otherwise we only care about the first one for
12747 : potentiality. */
12748 20485079 : case TRUTH_AND_EXPR:
12749 20485079 : case TRUTH_ANDIF_EXPR:
12750 20485079 : tmp = boolean_true_node;
12751 20485079 : goto truth;
12752 8603067 : case TRUTH_OR_EXPR:
12753 8603067 : case TRUTH_ORIF_EXPR:
12754 8603067 : tmp = boolean_false_node;
12755 29088146 : truth:
12756 29088146 : {
12757 29088146 : tree op0 = TREE_OPERAND (t, 0);
12758 29088146 : tree op1 = TREE_OPERAND (t, 1);
12759 29088146 : if (!RECUR (op0, rval))
12760 : return false;
12761 20260434 : if (!(flags & tf_error) && RECUR (op1, rval))
12762 : /* When quiet, try to avoid expensive trial evaluation by first
12763 : checking potentiality of the second operand. */
12764 : return true;
12765 2284613 : if (!processing_template_decl)
12766 1811330 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12767 2284613 : if (tree_int_cst_equal (op0, tmp))
12768 5133 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12769 : else
12770 : return true;
12771 : }
12772 :
12773 : case PLUS_EXPR:
12774 : case MULT_EXPR:
12775 : case POINTER_PLUS_EXPR:
12776 : case RDIV_EXPR:
12777 : case EXACT_DIV_EXPR:
12778 : case MIN_EXPR:
12779 : case MAX_EXPR:
12780 : case LSHIFT_EXPR:
12781 : case RSHIFT_EXPR:
12782 : case LROTATE_EXPR:
12783 : case RROTATE_EXPR:
12784 : case BIT_IOR_EXPR:
12785 : case BIT_XOR_EXPR:
12786 : case BIT_AND_EXPR:
12787 : case TRUTH_XOR_EXPR:
12788 : case UNORDERED_EXPR:
12789 : case ORDERED_EXPR:
12790 : case UNLT_EXPR:
12791 : case UNLE_EXPR:
12792 : case UNGT_EXPR:
12793 : case UNGE_EXPR:
12794 : case UNEQ_EXPR:
12795 : case LTGT_EXPR:
12796 : case RANGE_EXPR:
12797 : case COMPLEX_EXPR:
12798 250699774 : want_rval = true;
12799 : /* Fall through. */
12800 250699774 : case ARRAY_REF:
12801 250699774 : case ARRAY_RANGE_REF:
12802 250699774 : case MEMBER_REF:
12803 250699774 : case DOTSTAR_EXPR:
12804 250699774 : case MEM_REF:
12805 250699774 : case BINARY_LEFT_FOLD_EXPR:
12806 250699774 : case BINARY_RIGHT_FOLD_EXPR:
12807 250699774 : binary:
12808 550279491 : for (i = 0; i < 2; ++i)
12809 407520603 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12810 : return false;
12811 : return true;
12812 :
12813 : case VEC_PERM_EXPR:
12814 103947 : for (i = 0; i < 3; ++i)
12815 103913 : if (!RECUR (TREE_OPERAND (t, i), true))
12816 : return false;
12817 : return true;
12818 :
12819 7164278 : case COND_EXPR:
12820 7164278 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12821 : {
12822 8 : if (flags & tf_error)
12823 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12824 : "constant expression");
12825 8 : return false;
12826 : }
12827 : /* Fall through. */
12828 21237166 : case IF_STMT:
12829 21237166 : case VEC_COND_EXPR:
12830 : /* If the condition is a known constant, we know which of the legs we
12831 : care about; otherwise we only require that the condition and
12832 : either of the legs be potentially constant. */
12833 21237166 : tmp = TREE_OPERAND (t, 0);
12834 21237166 : if (!RECUR (tmp, rval))
12835 : return false;
12836 19463915 : if (!processing_template_decl)
12837 17507834 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12838 : /* potential_constant_expression* isn't told if it is called for
12839 : manifestly_const_eval or not, so for consteval if always
12840 : process both branches as if the condition is not a known
12841 : constant. */
12842 19463915 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12843 : {
12844 19433042 : if (integer_zerop (tmp))
12845 5069256 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12846 14363786 : else if (TREE_CODE (tmp) == INTEGER_CST)
12847 4583412 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12848 : }
12849 9811247 : tmp = *jump_target;
12850 11477086 : for (i = 1; i < 3; ++i)
12851 : {
12852 11255938 : tree this_jump_target = tmp;
12853 11255938 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12854 : want_rval, strict, now, fundef_p,
12855 : tf_none, &this_jump_target))
12856 : {
12857 9743017 : if (returns (&this_jump_target) || throws (&this_jump_target))
12858 2463513 : *jump_target = this_jump_target;
12859 7126586 : else if (!returns (jump_target) && !throws (jump_target))
12860 : {
12861 7126586 : if (breaks (&this_jump_target)
12862 7126586 : || continues (&this_jump_target))
12863 42 : *jump_target = this_jump_target;
12864 7126586 : if (i == 1)
12865 : {
12866 : /* If the then branch is potentially constant, but
12867 : does not return, check if the else branch
12868 : couldn't return, break or continue. */
12869 5905281 : hash_set<tree> pset;
12870 5905281 : check_for_return_continue_data data = { &pset, NULL_TREE,
12871 : NULL_TREE,
12872 5905281 : false };
12873 11810562 : if (tree ret_expr
12874 5905281 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12875 : check_for_return_continue, &data,
12876 : &pset))
12877 59168 : *jump_target = ret_expr;
12878 5846113 : else if (*jump_target == NULL_TREE)
12879 : {
12880 5846074 : if (data.continue_stmt)
12881 0 : *jump_target = data.continue_stmt;
12882 5846074 : else if (data.break_stmt)
12883 13 : *jump_target = data.break_stmt;
12884 : }
12885 5905281 : if (data.could_throw)
12886 21237 : *jump_target = void_node;
12887 5905281 : }
12888 : }
12889 9590099 : return true;
12890 : }
12891 : }
12892 221148 : if (flags & tf_error)
12893 : {
12894 3 : if (TREE_CODE (t) == IF_STMT)
12895 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
12896 : "constant expression");
12897 : else
12898 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
12899 : "constant expression", t);
12900 : }
12901 : return false;
12902 :
12903 1043 : case VEC_INIT_EXPR:
12904 1043 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
12905 : return true;
12906 411 : if (flags & tf_error)
12907 : {
12908 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
12909 : "initialization"))
12910 2 : diagnose_non_constexpr_vec_init (t);
12911 : }
12912 : return false;
12913 :
12914 : case TYPE_DECL:
12915 : case TAG_DEFN:
12916 : /* We can see these in statement-expressions. */
12917 : return true;
12918 :
12919 1626416 : case CLEANUP_STMT:
12920 1626416 : if (!RECUR (CLEANUP_BODY (t), any))
12921 : return false;
12922 1625893 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
12923 : return false;
12924 : return true;
12925 :
12926 : case EMPTY_CLASS_EXPR:
12927 : return true;
12928 :
12929 36 : case GOTO_EXPR:
12930 36 : {
12931 36 : tree *target = &TREE_OPERAND (t, 0);
12932 : /* Gotos representing break, continue and cdtor return are OK. */
12933 36 : if (breaks (target) || continues (target) || returns (target))
12934 : {
12935 18 : *jump_target = *target;
12936 18 : return true;
12937 : }
12938 18 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
12939 : /* The user didn't write this goto, this isn't the problem. */
12940 : return true;
12941 16 : if (flags & tf_error)
12942 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
12943 : "expression");
12944 : return false;
12945 : }
12946 :
12947 : case ASSERTION_STMT:
12948 : case PRECONDITION_STMT:
12949 : case POSTCONDITION_STMT:
12950 : /* Contracts are not supposed to alter this; we have to check that this
12951 : is not violated at a later time. */
12952 : return true;
12953 :
12954 223 : case LABEL_EXPR:
12955 223 : t = LABEL_EXPR_LABEL (t);
12956 223 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
12957 : return true;
12958 25 : else if (flags & tf_error)
12959 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
12960 : "function only available with %<-std=c++23%> or "
12961 : "%<-std=gnu++23%>");
12962 : return false;
12963 :
12964 9749 : case ANNOTATE_EXPR:
12965 9749 : return RECUR (TREE_OPERAND (t, 0), rval);
12966 :
12967 127888 : case BIT_CAST_EXPR:
12968 127888 : return RECUR (TREE_OPERAND (t, 0), rval);
12969 :
12970 : /* Coroutine await, yield and return expressions are not. */
12971 682 : case CO_AWAIT_EXPR:
12972 682 : case CO_YIELD_EXPR:
12973 682 : case CO_RETURN_EXPR:
12974 682 : case TEMPLATE_FOR_STMT:
12975 682 : if (flags & tf_error)
12976 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
12977 : "%qE is not a constant expression", t);
12978 : return false;
12979 :
12980 : /* Assume a TU-local entity is not constant, we'll error later when
12981 : instantiating. */
12982 : case TU_LOCAL_ENTITY:
12983 : return false;
12984 :
12985 : /* A splice expression is dependent, but will be constant after
12986 : substitution. */
12987 : case SPLICE_EXPR:
12988 : return true;
12989 :
12990 30 : case NONTYPE_ARGUMENT_PACK:
12991 30 : {
12992 30 : tree args = ARGUMENT_PACK_ARGS (t);
12993 30 : int len = TREE_VEC_LENGTH (args);
12994 90 : for (int i = 0; i < len; ++i)
12995 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
12996 : return false;
12997 : return true;
12998 : }
12999 :
13000 0 : default:
13001 0 : if (objc_non_constant_expr_p (t))
13002 : return false;
13003 :
13004 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13005 0 : gcc_unreachable ();
13006 : return false;
13007 : }
13008 : #undef RECUR
13009 5305865316 : }
13010 :
13011 : bool
13012 1837976434 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13013 : bool fundef_p, tsubst_flags_t flags)
13014 : {
13015 1837976434 : if (flags & tf_error)
13016 : {
13017 : /* Check potentiality quietly first, as that could be performed more
13018 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13019 : that fails, replay the check noisily to give errors. */
13020 14164128 : flags &= ~tf_error;
13021 14164128 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13022 : flags))
13023 : return true;
13024 965 : flags |= tf_error;
13025 : }
13026 :
13027 1823813271 : tree target = NULL_TREE;
13028 1823813271 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13029 1823813271 : flags, &target);
13030 : }
13031 :
13032 : /* The main entry point to the above. */
13033 :
13034 : bool
13035 461629528 : potential_constant_expression (tree t)
13036 : {
13037 461629528 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13038 : /*now*/false, /*fundef_p*/false,
13039 461629528 : tf_none);
13040 : }
13041 :
13042 : /* As above, but require a constant rvalue. */
13043 :
13044 : bool
13045 37641211 : potential_rvalue_constant_expression (tree t)
13046 : {
13047 37641211 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13048 : /*now*/false, /*fundef_p*/false,
13049 37641211 : tf_none);
13050 : }
13051 :
13052 : /* Like above, but complain about non-constant expressions. */
13053 :
13054 : bool
13055 72 : require_potential_constant_expression (tree t)
13056 : {
13057 72 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13058 : /*now*/false, /*fundef_p*/false,
13059 72 : tf_warning_or_error);
13060 : }
13061 :
13062 : /* Cross product of the above. */
13063 :
13064 : bool
13065 126003 : require_potential_rvalue_constant_expression (tree t)
13066 : {
13067 126003 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13068 : /*now*/false, /*fundef_p*/false,
13069 126003 : tf_warning_or_error);
13070 : }
13071 :
13072 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
13073 :
13074 : bool
13075 252 : require_potential_rvalue_constant_expression_fncheck (tree t)
13076 : {
13077 252 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13078 : /*now*/false, /*fundef_p*/true,
13079 252 : tf_warning_or_error);
13080 : }
13081 :
13082 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
13083 :
13084 : bool
13085 733 : require_rvalue_constant_expression (tree t)
13086 : {
13087 733 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13088 : /*now*/true, /*fundef_p*/false,
13089 733 : tf_warning_or_error);
13090 : }
13091 :
13092 : /* Like potential_constant_expression, but don't consider possible constexpr
13093 : substitution of the current function. That is, PARM_DECL qualifies under
13094 : potential_constant_expression, but not here.
13095 :
13096 : This is basically what you can check when any actual constant values might
13097 : be value-dependent. */
13098 :
13099 : bool
13100 1001628957 : is_constant_expression (tree t)
13101 : {
13102 1001628957 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13103 : /*now*/true, /*fundef_p*/false,
13104 1001628957 : tf_none);
13105 : }
13106 :
13107 : /* As above, but expect an rvalue. */
13108 :
13109 : bool
13110 147068771 : is_rvalue_constant_expression (tree t)
13111 : {
13112 147068771 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13113 : /*now*/true, /*fundef_p*/false,
13114 147068771 : tf_none);
13115 : }
13116 :
13117 : /* Like above, but complain about non-constant expressions. */
13118 :
13119 : bool
13120 14037068 : require_constant_expression (tree t)
13121 : {
13122 14037068 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13123 : /*now*/true, /*fundef_p*/false,
13124 14037068 : tf_warning_or_error);
13125 : }
13126 :
13127 : /* Like is_constant_expression, but allow const variables that are not allowed
13128 : under constexpr rules. */
13129 :
13130 : bool
13131 161679711 : is_static_init_expression (tree t)
13132 : {
13133 161679711 : return potential_constant_expression_1 (t, /*want_rval*/false,
13134 : /*strict*/false, /*now*/true,
13135 161679711 : /*fundef_p*/false, tf_none);
13136 : }
13137 :
13138 : /* Returns true if T is a potential constant expression that is not
13139 : instantiation-dependent, and therefore a candidate for constant folding even
13140 : in a template. */
13141 :
13142 : bool
13143 967541644 : is_nondependent_constant_expression (tree t)
13144 : {
13145 967541644 : return (!type_unknown_p (t)
13146 967541595 : && is_constant_expression (t)
13147 1806294931 : && !instantiation_dependent_expression_p (t));
13148 : }
13149 :
13150 : /* Returns true if T is a potential static initializer expression that is not
13151 : instantiation-dependent. */
13152 :
13153 : bool
13154 161679711 : is_nondependent_static_init_expression (tree t)
13155 : {
13156 161679711 : return (!type_unknown_p (t)
13157 161679711 : && is_static_init_expression (t)
13158 300577581 : && !instantiation_dependent_expression_p (t));
13159 : }
13160 :
13161 : /* True iff FN is an implicitly constexpr function. */
13162 :
13163 : bool
13164 188509 : decl_implicit_constexpr_p (tree fn)
13165 : {
13166 188509 : if (!(flag_implicit_constexpr
13167 2 : && TREE_CODE (fn) == FUNCTION_DECL
13168 2 : && DECL_DECLARED_CONSTEXPR_P (fn)))
13169 : return false;
13170 :
13171 2 : if (DECL_CLONED_FUNCTION_P (fn))
13172 0 : fn = DECL_CLONED_FUNCTION (fn);
13173 :
13174 2 : return (DECL_LANG_SPECIFIC (fn)
13175 2 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
13176 : }
13177 :
13178 : /* Finalize constexpr processing after parsing. */
13179 :
13180 : void
13181 95785 : fini_constexpr (void)
13182 : {
13183 : /* The contexpr call and fundef copies tables are no longer needed. */
13184 95785 : constexpr_call_table = NULL;
13185 95785 : fundef_copies_table = NULL;
13186 95785 : }
13187 :
13188 : #include "gt-cp-constexpr.h"
|