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 181462134 : is_instantiation_of_constexpr (tree fun)
62 : {
63 252280336 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 111065102 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 271141736 : || (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 171378606 : literal_type_p (tree t)
73 : {
74 168267759 : if (SCALAR_TYPE_P (t)
75 77545487 : || VECTOR_TYPE_P (t)
76 77524962 : || TYPE_REF_P (t)
77 235696108 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : return true;
79 59797398 : if (CLASS_TYPE_P (t))
80 : {
81 58320244 : t = complete_type (t);
82 58320244 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 58320244 : return CLASSTYPE_LITERAL_P (t);
84 : }
85 1477154 : if (TREE_CODE (t) == ARRAY_TYPE)
86 1476948 : 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 320975146 : ensure_literal_type_for_constexpr_object (tree decl)
96 : {
97 320975146 : tree type = TREE_TYPE (decl);
98 320975146 : if (VAR_P (decl)
99 120696886 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 84555705 : || var_in_constexpr_fn (decl))
101 375221452 : && !processing_template_decl)
102 : {
103 35247754 : tree stype = strip_array_types (type);
104 35247754 : 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 35247748 : else if (!literal_type_p (type))
108 : {
109 24087 : 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 24034 : else if (cxx_dialect < cxx23)
119 : {
120 18139 : 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 18139 : cp_function_chain->invalid_constexpr = true;
131 : }
132 : }
133 35223661 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 35223661 : && 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 320975146 : 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 1034 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : const char *gmsgid, ...)
156 : {
157 1034 : diagnostics::diagnostic_info diagnostic;
158 1034 : va_list ap;
159 1034 : rich_location richloc (line_table, location);
160 1034 : va_start (ap, gmsgid);
161 1034 : bool ret;
162 1034 : if (!constexpr_fundef_p)
163 : {
164 : /* Report an error that cannot be suppressed. */
165 738 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
166 : diagnostics::kind::error);
167 738 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
168 : }
169 296 : else if (warn_invalid_constexpr)
170 : {
171 163 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
172 163 : (cxx_dialect < cxx23
173 : ? diagnostics::kind::pedwarn
174 : : diagnostics::kind::warning));
175 163 : diagnostic.m_option_id = OPT_Winvalid_constexpr;
176 163 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
177 : }
178 : else
179 : ret = false;
180 1034 : va_end (ap);
181 2068 : return ret;
182 1034 : }
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 739419372 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : const constexpr_fundef *rhs)
202 : {
203 718556374 : 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 713030046 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : {
212 713030046 : return DECL_UID (fundef->decl);
213 : }
214 :
215 : /* Return a previously saved definition of function FUN. */
216 :
217 : constexpr_fundef *
218 81560333 : retrieve_constexpr_fundef (tree fun)
219 : {
220 81560333 : if (constexpr_fundef_table == NULL)
221 : return NULL;
222 :
223 81556332 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 81556332 : 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 29765009 : is_valid_constexpr_fn (tree fun, bool complain)
232 : {
233 29765009 : bool ret = true;
234 :
235 59530018 : if (DECL_INHERITED_CTOR (fun)
236 3471708 : && 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 29765009 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 60124264 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 30359255 : if (!literal_type_p (TREE_TYPE (parm)))
248 : {
249 13903 : ret = false;
250 13903 : if (complain)
251 : {
252 23 : auto_diagnostic_group d;
253 23 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
254 : "invalid type for parameter %d of "
255 : "%<constexpr%> function %q+#D",
256 23 : DECL_PARM_INDEX (parm), fun))
257 15 : explain_non_literal_class (TREE_TYPE (parm));
258 23 : }
259 : }
260 : }
261 :
262 49299667 : 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 59530012 : 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 29765006 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : {
279 25674113 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 25674113 : if (!literal_type_p (rettype))
281 : {
282 50561 : ret = false;
283 50561 : if (complain)
284 : {
285 21 : auto_diagnostic_group d;
286 21 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
287 : "invalid return type %qT of %<constexpr%> "
288 : "function %q+D", rettype, fun))
289 10 : explain_non_literal_class (rettype);
290 21 : }
291 : }
292 :
293 : /* C++14 DR 1684 removed this restriction. */
294 25674113 : if (cxx_dialect < cxx14
295 32931 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 25675233 : && !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 4090893 : 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 29765009 : 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 3491 : 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 3491 : auto_vec<tree, 2> fields;
344 7078 : do
345 : {
346 7078 : fields.safe_push (TREE_OPERAND (member, 1));
347 7078 : member = TREE_OPERAND (member, 0);
348 : }
349 7078 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 14162 : && 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 7078 : while (field = fields.pop(),
357 7078 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : {
359 3587 : tree ctor;
360 : /* If there is already an outer constructor entry for the anonymous
361 : aggregate FIELD, use it; otherwise, insert one. */
362 3587 : if (vec_safe_is_empty (*vec)
363 198 : || (*vec)->last().index != field)
364 : {
365 3485 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 3485 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : }
368 : else
369 102 : ctor = (*vec)->last().value;
370 3587 : 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 3491 : gcc_assert (fields.is_empty());
376 3491 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 :
378 3491 : return true;
379 3491 : }
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 5712952 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : {
390 6373796 : tree member, init;
391 6373796 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 2895511 : t = TREE_OPERAND (t, 0);
393 6373796 : if (TREE_CODE (t) == EXPR_STMT)
394 2894665 : t = TREE_OPERAND (t, 0);
395 6373796 : if (t == error_mark_node)
396 : return false;
397 6373787 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : {
399 7211130 : for (tree stmt : tsi_range (t))
400 841655 : if (! build_data_member_initialization (stmt, vec))
401 9 : return false;
402 : return true;
403 : }
404 5717255 : 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 630056 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : }
413 5087199 : if (TREE_CODE (t) == CONVERT_EXPR)
414 2848015 : t = TREE_OPERAND (t, 0);
415 5087199 : if (TREE_CODE (t) == INIT_EXPR)
416 : {
417 2722041 : member = TREE_OPERAND (t, 0);
418 2722041 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : }
420 2365158 : else if (TREE_CODE (t) == CALL_EXPR)
421 : {
422 1934806 : tree fn = get_callee_fndecl (t);
423 3869612 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : /* We're only interested in calls to subobject constructors. */
425 : return true;
426 1545080 : 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 1545080 : init = break_out_target_exprs (t);
431 : }
432 430352 : else if (TREE_CODE (t) == BIND_EXPR)
433 30788 : 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 4267121 : if (INDIRECT_REF_P (member))
438 58908 : member = TREE_OPERAND (member, 0);
439 4267121 : if (TREE_CODE (member) == NOP_EXPR)
440 : {
441 534656 : tree op = member;
442 534656 : STRIP_NOPS (op);
443 534656 : if (TREE_CODE (op) == ADDR_EXPR)
444 : {
445 563 : 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 534093 : else if (op == current_class_ptr
453 1068055 : && (same_type_ignoring_top_level_qualifiers_p
454 533962 : (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 460599 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : }
464 : }
465 4267121 : if (TREE_CODE (member) == ADDR_EXPR)
466 1069895 : member = TREE_OPERAND (member, 0);
467 4267121 : if (TREE_CODE (member) == COMPONENT_REF)
468 : {
469 3708850 : tree aggr = TREE_OPERAND (member, 0);
470 3708850 : if (TREE_CODE (aggr) == VAR_DECL)
471 : /* Initializing a local variable, don't add anything. */
472 : return true;
473 3708848 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : /* Normal member initialization. */
475 3705354 : member = TREE_OPERAND (member, 1);
476 3494 : else if (VAR_P (get_base_address (aggr)))
477 : /* Initializing a local variable, don't add anything. */
478 : return true;
479 3491 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
480 : /* Initializing a member of an anonymous union. */
481 3491 : 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 5316400 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
491 56 : (*vec)->last().value = init;
492 : else
493 4263569 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
494 : return true;
495 : }
496 :
497 : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
498 : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
499 : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
500 :
501 : static bool
502 2401 : check_constexpr_bind_expr_vars (tree t)
503 : {
504 2401 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
505 :
506 3239 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
507 850 : if (TREE_CODE (var) == TYPE_DECL
508 831 : && DECL_IMPLICIT_TYPEDEF_P (var)
509 872 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
510 : return false;
511 : return true;
512 : }
513 :
514 : /* Subroutine of check_constexpr_ctor_body. */
515 :
516 : static bool
517 4135 : check_constexpr_ctor_body_1 (tree last, tree list)
518 : {
519 4135 : switch (TREE_CODE (list))
520 : {
521 6 : case DECL_EXPR:
522 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
523 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
524 : return true;
525 : return false;
526 :
527 4 : case CLEANUP_POINT_EXPR:
528 4 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
529 4 : /*complain=*/false);
530 :
531 2058 : case BIND_EXPR:
532 2058 : if (!check_constexpr_bind_expr_vars (list)
533 2058 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
534 : /*complain=*/false))
535 43 : return false;
536 : return true;
537 :
538 : case USING_STMT:
539 : case STATIC_ASSERT:
540 : case DEBUG_BEGIN_STMT:
541 : return true;
542 :
543 : default:
544 : return false;
545 : }
546 : }
547 :
548 : /* Make sure that there are no statements after LAST in the constructor
549 : body represented by LIST. */
550 :
551 : bool
552 5647028 : 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 5647028 : if (cxx_dialect >= cxx14)
556 : return true;
557 :
558 13770 : bool ok = true;
559 13770 : if (TREE_CODE (list) == STATEMENT_LIST)
560 : {
561 13743 : tree_stmt_iterator i = tsi_last (list);
562 17781 : for (; !tsi_end_p (i); tsi_prev (&i))
563 : {
564 15654 : tree t = tsi_stmt (i);
565 15654 : if (t == last)
566 : break;
567 4108 : if (!check_constexpr_ctor_body_1 (last, t))
568 : {
569 : ok = false;
570 : break;
571 : }
572 : }
573 : }
574 27 : else if (list != last
575 27 : && !check_constexpr_ctor_body_1 (last, list))
576 : ok = false;
577 13770 : if (!ok && complain)
578 : {
579 49 : pedwarn (input_location, OPT_Wc__14_extensions,
580 : "%<constexpr%> constructor does not have empty body");
581 49 : ok = true;
582 : }
583 : return ok;
584 : }
585 :
586 : /* V is a vector of constructor elements built up for the base and member
587 : initializers of a constructor for TYPE. They need to be in increasing
588 : offset order, which they might not be yet if TYPE has a primary base
589 : which is not first in the base-clause or a vptr and at least one base
590 : all of which are non-primary. */
591 :
592 : static vec<constructor_elt, va_gc> *
593 3397354 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
594 : {
595 3397354 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
596 3397354 : tree field_type;
597 3397354 : unsigned i;
598 3397354 : constructor_elt *ce;
599 :
600 3397354 : if (pri)
601 75477 : field_type = BINFO_TYPE (pri);
602 3321877 : else if (TYPE_CONTAINS_VPTR_P (type))
603 35366 : 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 150544 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
610 115058 : if (TREE_TYPE (ce->index) == field_type)
611 : break;
612 :
613 131831 : if (i > 0 && i < vec_safe_length (v))
614 : {
615 28 : vec<constructor_elt, va_gc> &vref = *v;
616 28 : constructor_elt elt = vref[i];
617 56 : for (; i > 0; --i)
618 28 : vref[i] = vref[i-1];
619 28 : vref[0] = elt;
620 : }
621 :
622 : return v;
623 : }
624 :
625 : /* Build compile-time evalable representations of member-initializer list
626 : for a constexpr constructor. */
627 :
628 : static tree
629 3470829 : build_constexpr_constructor_member_initializers (tree type, tree body)
630 : {
631 3470829 : vec<constructor_elt, va_gc> *vec = NULL;
632 3470829 : bool ok = true;
633 5430408 : while (true)
634 5430408 : switch (TREE_CODE (body))
635 : {
636 1959502 : case MUST_NOT_THROW_EXPR:
637 1959502 : case EH_SPEC_BLOCK:
638 1959502 : case TRY_FINALLY_EXPR: // For C++26 postconditions.
639 1959502 : body = TREE_OPERAND (body, 0);
640 1959502 : break;
641 :
642 77 : case STATEMENT_LIST:
643 5430510 : for (tree stmt : tsi_range (body))
644 : {
645 157 : body = stmt;
646 157 : if (TREE_CODE (body) == BIND_EXPR)
647 : break;
648 : }
649 : break;
650 :
651 3470829 : case BIND_EXPR:
652 3470829 : body = BIND_EXPR_BODY (body);
653 3470829 : goto found;
654 :
655 0 : default:
656 0 : gcc_unreachable ();
657 : }
658 3470829 : found:
659 3470829 : if (TREE_CODE (body) == TRY_BLOCK)
660 : {
661 31 : body = TREE_OPERAND (body, 0);
662 31 : if (TREE_CODE (body) == BIND_EXPR)
663 31 : body = BIND_EXPR_BODY (body);
664 : }
665 3470829 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
666 : {
667 1888160 : body = TREE_OPERAND (body, 0);
668 1888160 : if (TREE_CODE (body) == EXPR_STMT)
669 1888148 : body = TREE_OPERAND (body, 0);
670 1888160 : if (TREE_CODE (body) == INIT_EXPR
671 1888160 : && (same_type_ignoring_top_level_qualifiers_p
672 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
673 : current_class_type)))
674 : {
675 : /* Trivial copy. */
676 0 : return TREE_OPERAND (body, 1);
677 : }
678 1888160 : ok = build_data_member_initialization (body, &vec);
679 : }
680 1582669 : else if (TREE_CODE (body) == STATEMENT_LIST)
681 : {
682 4555615 : for (tree stmt : tsi_range (body))
683 : {
684 2978042 : ok = build_data_member_initialization (stmt, &vec);
685 2978042 : if (!ok)
686 : break;
687 : }
688 : }
689 5095 : else if (EXPR_P (body))
690 5095 : ok = build_data_member_initialization (body, &vec);
691 : else
692 0 : gcc_assert (errorcount > 0);
693 3470829 : if (ok)
694 : {
695 3470820 : if (vec_safe_length (vec) > 0)
696 : {
697 : /* In a delegating constructor, return the target. */
698 3214215 : constructor_elt *ce = &(*vec)[0];
699 3214215 : if (ce->index == current_class_ptr)
700 : {
701 73466 : body = ce->value;
702 73466 : vec_free (vec);
703 73466 : return body;
704 : }
705 : }
706 3397354 : vec = sort_constexpr_mem_initializers (type, vec);
707 3397354 : return build_constructor (type, vec);
708 : }
709 : else
710 9 : return error_mark_node;
711 : }
712 :
713 : /* We have an expression tree T that represents a call, either CALL_EXPR
714 : or AGGR_INIT_EXPR. If the call is lexically to a named function,
715 : return the _DECL for that function. */
716 :
717 : static tree
718 460440365 : get_function_named_in_call (tree t)
719 : {
720 460440365 : tree callee = cp_get_callee (t);
721 460440365 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
722 460440365 : return fun ? fun : callee;
723 : }
724 :
725 : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
726 : declared to be constexpr, or a sub-statement thereof. Returns the
727 : return value if suitable, error_mark_node for a statement not allowed in
728 : a constexpr function, or NULL_TREE if no return value was found. */
729 :
730 : tree
731 70540 : constexpr_fn_retval (tree body)
732 : {
733 77340 : switch (TREE_CODE (body))
734 : {
735 18773 : case STATEMENT_LIST:
736 18773 : {
737 18773 : tree expr = NULL_TREE;
738 56249 : for (tree stmt : tsi_range (body))
739 : {
740 37505 : tree s = constexpr_fn_retval (stmt);
741 37505 : if (s == error_mark_node)
742 : return error_mark_node;
743 37478 : else if (s == NULL_TREE)
744 : /* Keep iterating. */;
745 18738 : else if (expr)
746 : /* Multiple return statements. */
747 : return error_mark_node;
748 : else
749 : expr = s;
750 : }
751 : return expr;
752 : }
753 :
754 32985 : case RETURN_EXPR:
755 32985 : return break_out_target_exprs (TREE_OPERAND (body, 0));
756 :
757 20 : case DECL_EXPR:
758 20 : {
759 20 : tree decl = DECL_EXPR_DECL (body);
760 20 : if (TREE_CODE (decl) == USING_DECL
761 : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
762 20 : || DECL_ARTIFICIAL (decl))
763 : return NULL_TREE;
764 6 : return error_mark_node;
765 : }
766 :
767 6463 : case CLEANUP_POINT_EXPR:
768 6463 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
769 :
770 343 : case BIND_EXPR:
771 343 : if (!check_constexpr_bind_expr_vars (body))
772 6 : return error_mark_node;
773 337 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
774 :
775 : case USING_STMT:
776 : case DEBUG_BEGIN_STMT:
777 : return NULL_TREE;
778 :
779 0 : case CALL_EXPR:
780 0 : {
781 0 : tree fun = get_function_named_in_call (body);
782 0 : if (fun != NULL_TREE
783 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
784 : return NULL_TREE;
785 : }
786 : /* Fallthru. */
787 :
788 30 : default:
789 30 : return error_mark_node;
790 : }
791 : }
792 :
793 : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
794 : FUN; do the necessary transformations to turn it into a single expression
795 : that we can store in the hash table. */
796 :
797 : static tree
798 29156149 : massage_constexpr_body (tree fun, tree body)
799 : {
800 58312298 : if (DECL_CONSTRUCTOR_P (fun))
801 3470829 : body = build_constexpr_constructor_member_initializers
802 3470829 : (DECL_CONTEXT (fun), body);
803 25685320 : else if (cxx_dialect < cxx14)
804 : {
805 32800 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
806 1 : body = EH_SPEC_STMTS (body);
807 32800 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
808 21419 : body = TREE_OPERAND (body, 0);
809 32800 : body = constexpr_fn_retval (body);
810 : }
811 29156149 : return body;
812 : }
813 :
814 : /* CTYPE is a type constructed from BODY. Return true if some
815 : bases/fields are uninitialized, and complain if COMPLAIN. */
816 :
817 : static bool
818 3155413 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
819 : {
820 : /* We allow uninitialized bases/fields in C++20. */
821 3155413 : if (cxx_dialect >= cxx20)
822 : return false;
823 :
824 13623 : unsigned nelts = 0;
825 :
826 13623 : if (body)
827 : {
828 13619 : if (TREE_CODE (body) != CONSTRUCTOR)
829 : return false;
830 13568 : nelts = CONSTRUCTOR_NELTS (body);
831 : }
832 13572 : tree field = TYPE_FIELDS (ctype);
833 :
834 13572 : if (TREE_CODE (ctype) == UNION_TYPE)
835 : {
836 110 : if (nelts == 0 && next_aggregate_field (field))
837 : {
838 2 : if (complain)
839 2 : error ("%<constexpr%> constructor for union %qT must "
840 : "initialize exactly one non-static data member", ctype);
841 2 : return true;
842 : }
843 108 : return false;
844 : }
845 :
846 : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
847 : need an explicit initialization. */
848 : bool bad = false;
849 28644 : for (unsigned i = 0; i <= nelts; ++i)
850 : {
851 28644 : tree index = NULL_TREE;
852 28644 : if (i < nelts)
853 : {
854 15182 : index = CONSTRUCTOR_ELT (body, i)->index;
855 : /* Skip vptr adjustment represented with a COMPONENT_REF. */
856 15182 : if (TREE_CODE (index) != FIELD_DECL)
857 627 : continue;
858 : }
859 :
860 494956 : for (; field != index; field = DECL_CHAIN (field))
861 : {
862 466945 : tree ftype;
863 466945 : if (TREE_CODE (field) != FIELD_DECL)
864 932073 : continue;
865 1789 : if (DECL_UNNAMED_BIT_FIELD (field))
866 10 : continue;
867 : /* Artificial fields can be ignored unless they're bases. */
868 1779 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
869 6 : continue;
870 1773 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
871 : {
872 : /* Recurse to check the anonymous aggregate member. */
873 8 : bad |= cx_check_missing_mem_inits
874 4 : (TREE_TYPE (field), NULL_TREE, complain);
875 4 : if (bad && !complain)
876 6 : return true;
877 4 : continue;
878 : }
879 1769 : ftype = TREE_TYPE (field);
880 1769 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
881 : /* A flexible array can't be initialized here, so don't complain
882 : that it isn't. */
883 1 : continue;
884 1768 : if (is_empty_field (field))
885 : /* An empty field doesn't need an initializer. */
886 1733 : continue;
887 35 : ftype = strip_array_types (ftype);
888 35 : if (type_has_constexpr_default_constructor (ftype))
889 : {
890 : /* It's OK to skip a member with a trivial constexpr ctor.
891 : A constexpr ctor that isn't trivial should have been
892 : added in by now. */
893 7 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
894 : || errorcount != 0);
895 7 : continue;
896 : }
897 28 : if (!complain)
898 : return true;
899 22 : auto_diagnostic_group d;
900 22 : error ("member %qD must be initialized by mem-initializer "
901 : "in %<constexpr%> constructor", field);
902 22 : inform (DECL_SOURCE_LOCATION (field), "declared here");
903 22 : bad = true;
904 22 : }
905 28011 : if (field == NULL_TREE)
906 : break;
907 :
908 14555 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
909 : {
910 : /* Check the anonymous aggregate initializer is valid. */
911 76 : bad |= cx_check_missing_mem_inits
912 38 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
913 38 : if (bad && !complain)
914 : return true;
915 : }
916 14555 : field = DECL_CHAIN (field);
917 : }
918 :
919 : return bad;
920 : }
921 :
922 : /* We are processing the definition of the constexpr function FUN.
923 : Check that its body fulfills the appropriate requirements and
924 : enter it in the constexpr function definition table. */
925 :
926 : void
927 159124523 : maybe_save_constexpr_fundef (tree fun)
928 : {
929 159124523 : if (processing_template_decl
930 65654672 : || cp_function_chain->invalid_constexpr
931 224761165 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
932 129968668 : return;
933 :
934 : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
935 : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
936 50155112 : bool implicit = false;
937 50155112 : if (flag_implicit_constexpr)
938 : {
939 22 : if (DECL_DELETING_DESTRUCTOR_P (fun)
940 22 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
941 : /* Don't inherit implicit constexpr from the non-deleting
942 : destructor. */
943 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
944 :
945 22 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
946 18 : && DECL_DECLARED_INLINE_P (fun)
947 36 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
948 : implicit = true;
949 : }
950 :
951 50155112 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
952 : return;
953 :
954 29211412 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
955 :
956 29211412 : if (!is_valid_constexpr_fn (fun, complain))
957 : return;
958 :
959 29156069 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
960 29156069 : if (massaged == NULL_TREE || massaged == error_mark_node)
961 : {
962 72 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
963 22 : error ("body of %<constexpr%> function %qD not a return-statement",
964 : fun);
965 36 : return;
966 : }
967 :
968 29156033 : bool potential = potential_rvalue_constant_expression (massaged);
969 29156033 : if (!potential && complain)
970 253 : require_potential_rvalue_constant_expression_fncheck (massaged);
971 :
972 32626839 : if (DECL_CONSTRUCTOR_P (fun) && potential
973 32623558 : && !DECL_DEFAULTED_FN (fun))
974 : {
975 3155357 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
976 : massaged, complain))
977 : potential = false;
978 3155331 : else if (cxx_dialect > cxx11)
979 : {
980 : /* What we got from massage_constexpr_body is pretty much just the
981 : ctor-initializer, also check the body. */
982 3150534 : massaged = DECL_SAVED_TREE (fun);
983 3150534 : potential = potential_rvalue_constant_expression (massaged);
984 3150534 : if (!potential && complain)
985 18 : require_potential_rvalue_constant_expression_fncheck (massaged);
986 : }
987 : }
988 :
989 29156033 : if (!potential && complain
990 : /* If -Wno-invalid-constexpr was specified, we haven't complained
991 : about non-constant expressions yet. Register the function and
992 : complain in explain_invalid_constexpr_fn if the function is
993 : called. */
994 291 : && warn_invalid_constexpr != 0)
995 : return;
996 :
997 29155866 : if (implicit)
998 : {
999 14 : if (potential)
1000 : {
1001 3 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
1002 3 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
1003 6 : if (DECL_CONSTRUCTOR_P (fun))
1004 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
1005 : }
1006 : else
1007 : /* Don't bother keeping the pre-generic body of unsuitable functions
1008 : not explicitly declared constexpr. */
1009 : return;
1010 : }
1011 :
1012 29155855 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1013 29155855 : bool clear_ctx = false;
1014 29155855 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1015 : {
1016 29155855 : clear_ctx = true;
1017 29155855 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1018 : }
1019 29155855 : tree saved_fn = current_function_decl;
1020 29155855 : current_function_decl = fun;
1021 29155855 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1022 29155855 : current_function_decl = saved_fn;
1023 29155855 : if (clear_ctx)
1024 29155855 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1025 29155855 : if (!potential)
1026 : /* For a template instantiation, we want to remember the pre-generic body
1027 : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1028 : that it doesn't need to bother trying to expand the function. */
1029 57209 : entry.result = error_mark_node;
1030 :
1031 29155855 : register_constexpr_fundef (entry);
1032 : }
1033 :
1034 : /* BODY is a validated and massaged definition of a constexpr
1035 : function. Register it in the hash table. */
1036 :
1037 : void
1038 29193990 : register_constexpr_fundef (const constexpr_fundef &value)
1039 : {
1040 : /* Create the constexpr function table if necessary. */
1041 29193990 : if (constexpr_fundef_table == NULL)
1042 26742 : constexpr_fundef_table
1043 26742 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1044 :
1045 29193990 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1046 29193990 : (const_cast<constexpr_fundef *> (&value), INSERT);
1047 :
1048 29193990 : gcc_assert (*slot == NULL);
1049 29193990 : *slot = ggc_alloc<constexpr_fundef> ();
1050 29193990 : **slot = value;
1051 29193990 : }
1052 :
1053 : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1054 : function called in a context that requires a constant expression).
1055 : If it comes from a constexpr template, explain why the instantiation
1056 : isn't constexpr. Otherwise, explain why the function cannot be used
1057 : in a constexpr context. */
1058 :
1059 : void
1060 842 : explain_invalid_constexpr_fn (tree fun)
1061 : {
1062 842 : static hash_set<tree> *diagnosed;
1063 842 : tree body;
1064 :
1065 : /* Don't try to explain a function we already complained about. */
1066 842 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1067 638 : if (f->language->erroneous)
1068 842 : return;
1069 :
1070 : /* In C++23, a function marked 'constexpr' may not actually be a constant
1071 : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1072 : wasn't enabled. The function was called, so diagnose why it cannot be
1073 : used in a constant expression. */
1074 793 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1075 : /* Go on. */;
1076 : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1077 757 : else if (!DECL_DEFAULTED_FN (fun)
1078 1009 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1079 720 : && !(flag_implicit_constexpr
1080 9 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1081 9 : && DECL_DECLARED_INLINE_P (fun))
1082 1471 : && !is_instantiation_of_constexpr (fun))
1083 : {
1084 696 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1085 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1086 699 : && decl_defined_p (fun))
1087 3 : inform (DECL_SOURCE_LOCATION (fun),
1088 : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1089 696 : return;
1090 : }
1091 97 : if (diagnosed == NULL)
1092 74 : diagnosed = new hash_set<tree>;
1093 97 : if (diagnosed->add (fun))
1094 : /* Already explained. */
1095 : return;
1096 :
1097 96 : iloc_sentinel ils = input_location;
1098 96 : if (!lambda_static_thunk_p (fun))
1099 : {
1100 : /* Diagnostics should completely ignore the static thunk, so leave
1101 : input_location set to our caller's location. */
1102 84 : input_location = DECL_SOURCE_LOCATION (fun);
1103 84 : inform (input_location,
1104 : "%qD is not usable as a %<constexpr%> function because:", fun);
1105 : }
1106 : /* First check the declaration. */
1107 96 : if (is_valid_constexpr_fn (fun, true))
1108 : {
1109 : /* Then if it's OK, the body. */
1110 90 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1111 90 : && DECL_DEFAULTED_FN (fun))
1112 10 : explain_implicit_non_constexpr (fun);
1113 : else
1114 : {
1115 80 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1116 54 : body = fd->body;
1117 : else
1118 26 : body = DECL_SAVED_TREE (fun);
1119 80 : tree massaged = massage_constexpr_body (fun, body);
1120 80 : require_potential_rvalue_constant_expression (massaged);
1121 160 : if (DECL_CONSTRUCTOR_P (fun))
1122 : {
1123 14 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1124 14 : if (cxx_dialect > cxx11)
1125 : /* Also check the body, not just the ctor-initializer. */
1126 12 : require_potential_rvalue_constant_expression (body);
1127 : }
1128 66 : else if (massaged == NULL_TREE || massaged == error_mark_node)
1129 1 : error ("body of %<constexpr%> function %qD not a return-statement",
1130 : fun);
1131 : }
1132 : }
1133 96 : }
1134 :
1135 : /* Objects of this type represent calls to constexpr functions
1136 : along with the bindings of parameters to their arguments, for
1137 : the purpose of compile time evaluation. */
1138 :
1139 : struct GTY((for_user)) constexpr_call {
1140 : /* Description of the constexpr function definition. */
1141 : constexpr_fundef *fundef = nullptr;
1142 : /* Parameter bindings environment. A TREE_VEC of arguments. */
1143 : tree bindings = NULL_TREE;
1144 : /* Result of the call, indexed by the value of
1145 : constexpr_ctx::manifestly_const_eval.
1146 : unknown_type_node means the call is being evaluated.
1147 : error_mark_node means that the evaluation was erroneous or otherwise
1148 : uncacheable (e.g. because it depends on the caller).
1149 : Otherwise, the actual value of the call. */
1150 : tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1151 : /* The hash of this call; we remember it here to avoid having to
1152 : recalculate it when expanding the hash table. */
1153 : hashval_t hash = 0;
1154 :
1155 : /* The result slot corresponding to the given mce_value. */
1156 50401953 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1157 : };
1158 :
1159 : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1160 : {
1161 : static hashval_t hash (constexpr_call *);
1162 : static bool equal (constexpr_call *, constexpr_call *);
1163 : };
1164 :
1165 : enum constexpr_switch_state {
1166 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1167 : and default: label for that switch has not been seen yet. */
1168 : css_default_not_seen,
1169 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1170 : and default: label for that switch has been seen already. */
1171 : css_default_seen,
1172 : /* Used when processing a switch for the second time by
1173 : cxx_eval_switch_expr, where default: label should match. */
1174 : css_default_processing
1175 : };
1176 :
1177 : /* The constexpr expansion context part which needs one instance per
1178 : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1179 : variables initialized within the expression. */
1180 :
1181 : class constexpr_global_ctx {
1182 : /* Values for any temporaries or local variables within the
1183 : constant-expression. Objects outside their lifetime have
1184 : value 'void_node'. */
1185 : hash_map<tree,tree> values;
1186 : public:
1187 : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1188 : on simple constants or location wrappers) encountered during current
1189 : cxx_eval_outermost_constant_expr call. */
1190 : HOST_WIDE_INT constexpr_ops_count;
1191 : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1192 : expression. */
1193 : auto_vec<tree, 16> heap_vars;
1194 : /* Vector of caught exceptions, including exceptions still not active at
1195 : the start of a handler (those are immediately followed up by HANDLER_TYPE
1196 : until __cxa_begin_catch finishes). */
1197 : auto_vec<tree, 2> caught_exceptions;
1198 : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1199 : vec<tree> *cleanups;
1200 : /* If non-null, only allow modification of existing values of the variables
1201 : in this set. Set by modifiable_tracker, below. */
1202 : hash_set<tree> *modifiable;
1203 : /* If cxx_eval_outermost_constant_expr is called on the consteval block
1204 : operator (), this is the FUNCTION_DECL of that operator (). */
1205 : tree consteval_block;
1206 : /* Number of heap VAR_DECL deallocations. */
1207 : unsigned heap_dealloc_count;
1208 : /* Number of uncaught exceptions. */
1209 : unsigned uncaught_exceptions;
1210 : /* A contract statement that failed or was not constant, we only store the
1211 : first one that fails. */
1212 : tree contract_statement;
1213 : /* [basic.contract.eval]/7.3 if this expression would otherwise be constant
1214 : then a non-const contract makes the program ill-formed. */
1215 : bool contract_condition_non_const;
1216 : /* Some metafunctions aren't dependent just on their arguments, but also
1217 : on various other dependencies, e.g. has_identifier on a function parameter
1218 : reflection can change depending on further declarations of corresponding
1219 : function, is_complete_type depends on type definitions and template
1220 : specializations in between the calls, define_aggregate even defines
1221 : class types, etc. Thus, we need to arrange for calls which call
1222 : at least some metafunctions to be non-cacheable, because their behavior
1223 : might not be the same. Until we figure out which exact metafunctions
1224 : need this and which don't, do it for all of them. */
1225 : bool metafns_called;
1226 :
1227 : /* Constructor. */
1228 510610935 : constexpr_global_ctx ()
1229 1021221870 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1230 510610935 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1231 510610935 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1232 510610935 : contract_condition_non_const (false), metafns_called (false) {}
1233 :
1234 364983406 : bool is_outside_lifetime (tree t)
1235 : {
1236 364983406 : if (tree *p = values.get (t))
1237 150902862 : if (*p == void_node)
1238 193 : return true;
1239 : return false;
1240 : }
1241 460337200 : tree get_value (tree t)
1242 : {
1243 460337200 : if (tree *p = values.get (t))
1244 227684406 : if (*p != void_node)
1245 221690925 : return *p;
1246 : return NULL_TREE;
1247 : }
1248 91817026 : tree *get_value_ptr (tree t, bool initializing)
1249 : {
1250 91817026 : if (modifiable && !modifiable->contains (t))
1251 : return nullptr;
1252 91817015 : if (tree *p = values.get (t))
1253 : {
1254 91811737 : if (*p != void_node)
1255 : return p;
1256 48 : else if (initializing)
1257 : {
1258 6 : *p = NULL_TREE;
1259 6 : return p;
1260 : }
1261 : }
1262 : return nullptr;
1263 : }
1264 222293610 : void put_value (tree t, tree v)
1265 : {
1266 222293610 : bool already_in_map = values.put (t, v);
1267 222293610 : if (!already_in_map && modifiable)
1268 30 : modifiable->add (t);
1269 222293610 : }
1270 176212352 : void destroy_value (tree t)
1271 : {
1272 176212352 : if (TREE_CODE (t) == VAR_DECL
1273 176212352 : || TREE_CODE (t) == PARM_DECL
1274 : || TREE_CODE (t) == RESULT_DECL)
1275 176206026 : values.put (t, void_node);
1276 : else
1277 6326 : values.remove (t);
1278 176212352 : }
1279 13302663 : void clear_value (tree t)
1280 : {
1281 26605326 : values.remove (t);
1282 : }
1283 : };
1284 :
1285 : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1286 : side-effects from evaluation of a particular subexpression of a
1287 : constant-expression. In such cases we use modifiable_tracker to prevent
1288 : modification of variables created outside of that subexpression.
1289 :
1290 : ??? We could change the hash_set to a hash_map, allow and track external
1291 : modifications, and roll them back in the destructor. It's not clear to me
1292 : that this would be worthwhile. */
1293 :
1294 : class modifiable_tracker
1295 : {
1296 : hash_set<tree> set;
1297 : constexpr_global_ctx *global;
1298 : public:
1299 173478 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1300 : {
1301 173478 : global->modifiable = &set;
1302 173478 : }
1303 173478 : ~modifiable_tracker ()
1304 : {
1305 173508 : for (tree t: set)
1306 30 : global->clear_value (t);
1307 173478 : global->modifiable = nullptr;
1308 173478 : }
1309 : };
1310 :
1311 : /* The constexpr expansion context. CALL is the current function
1312 : expansion, CTOR is the current aggregate initializer, OBJECT is the
1313 : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1314 :
1315 : struct constexpr_ctx {
1316 : /* The part of the context that needs to be unique to the whole
1317 : cxx_eval_outermost_constant_expr invocation. */
1318 : constexpr_global_ctx *global;
1319 : /* The innermost call we're evaluating. */
1320 : constexpr_call *call;
1321 : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1322 : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1323 : vec<tree> *save_exprs;
1324 : /* The CONSTRUCTOR we're currently building up for an aggregate
1325 : initializer. */
1326 : tree ctor;
1327 : /* The object we're building the CONSTRUCTOR for. */
1328 : tree object;
1329 : /* If inside SWITCH_EXPR. */
1330 : constexpr_switch_state *css_state;
1331 : /* The aggregate initialization context inside which this one is nested. This
1332 : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1333 : const constexpr_ctx *parent;
1334 :
1335 : /* Whether we should error on a non-constant expression or fail quietly.
1336 : This flag needs to be here, but some of the others could move to global
1337 : if they get larger than a word. */
1338 : bool quiet;
1339 : /* Whether we are strictly conforming to constant expression rules or
1340 : trying harder to get a constant value. */
1341 : bool strict;
1342 : /* Whether __builtin_is_constant_evaluated () should be true. */
1343 : mce_value manifestly_const_eval;
1344 : };
1345 :
1346 : /* Return ctx->quiet. For use in reflect.cc. */
1347 :
1348 : bool
1349 606 : cxx_constexpr_quiet_p (const constexpr_ctx *ctx)
1350 : {
1351 606 : return ctx->quiet;
1352 : }
1353 :
1354 : /* Return ctx->manifestly_const_eval. For use in reflect.cc. */
1355 :
1356 : mce_value
1357 396 : cxx_constexpr_manifestly_const_eval (const constexpr_ctx *ctx)
1358 : {
1359 396 : return ctx->manifestly_const_eval;
1360 : }
1361 :
1362 : /* Return ctx->call->fundef->decl or NULL_TREE. For use in
1363 : reflect.cc. */
1364 :
1365 : tree
1366 2112 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1367 : {
1368 2112 : if (ctx->call)
1369 954 : return ctx->call->fundef->decl;
1370 : else
1371 : return NULL_TREE;
1372 : }
1373 :
1374 : /* Return ctx->global->consteval_block. For use in reflect.cc. */
1375 :
1376 : tree
1377 158 : cxx_constexpr_consteval_block (const constexpr_ctx *ctx)
1378 : {
1379 158 : return ctx->global->consteval_block;
1380 : }
1381 :
1382 : /* Predicates for the meaning of *jump_target. */
1383 :
1384 : static bool
1385 84677800 : returns (tree *jump_target)
1386 : {
1387 14257956 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1388 : }
1389 :
1390 : static bool
1391 81259010 : breaks (tree *jump_target)
1392 : {
1393 81259010 : return (*jump_target
1394 81259010 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1395 57 : && LABEL_DECL_BREAK (*jump_target))
1396 1355133 : || TREE_CODE (*jump_target) == BREAK_STMT
1397 1216947 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1398 : }
1399 :
1400 : static bool
1401 110505384 : continues (tree *jump_target)
1402 : {
1403 110505384 : return (*jump_target
1404 110505384 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1405 93 : && LABEL_DECL_CONTINUE (*jump_target))
1406 1234916 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1407 : }
1408 :
1409 : static bool
1410 11865909 : switches (tree *jump_target)
1411 : {
1412 76119 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1413 : }
1414 :
1415 : static bool
1416 1609672218 : throws (tree *jump_target)
1417 : {
1418 : /* void_node is for use in potential_constant_expression_1, otherwise
1419 : it should an artificial VAR_DECL created by constant evaluation
1420 : of __cxa_allocate_exception (). */
1421 154054074 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1422 : }
1423 :
1424 : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1425 : references and objects are in effect. */
1426 :
1427 : static bool
1428 208609837 : p2280_active_p (const constexpr_ctx *ctx)
1429 : {
1430 28707231 : if (ctx->manifestly_const_eval != mce_true)
1431 : /* Disable these relaxations during speculative constexpr folding,
1432 : as it can significantly increase compile time/memory use
1433 : (PR119387). */
1434 : return false;
1435 :
1436 : /* P2280R4 was accepted as a DR against C++11. */
1437 0 : return cxx_dialect >= cxx11;
1438 : }
1439 :
1440 : /* Remove T from the global values map, checking for attempts to destroy
1441 : a value that has already finished its lifetime. */
1442 :
1443 : static void
1444 175945679 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1445 : {
1446 175945679 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1447 : return;
1448 :
1449 : /* Don't error again here if we've already reported a problem. */
1450 175945655 : if (!*non_constant_p
1451 151676732 : && DECL_P (t)
1452 : /* Non-trivial destructors have their lifetimes ended explicitly
1453 : with a clobber, so don't worry about it here. */
1454 151670603 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1455 : /* ...except parameters are remapped in cxx_eval_call_expression,
1456 : and the destructor call during cleanup won't be able to tell that
1457 : this value has already been destroyed, so complain now. This is
1458 : not quite unobservable, but is extremely unlikely to crop up in
1459 : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1460 541947 : || TREE_CODE (t) == PARM_DECL)
1461 327075455 : && ctx->global->is_outside_lifetime (t))
1462 : {
1463 54 : if (!ctx->quiet)
1464 : {
1465 18 : auto_diagnostic_group d;
1466 18 : error ("destroying %qE outside its lifetime", t);
1467 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1468 18 : }
1469 54 : *non_constant_p = true;
1470 : }
1471 175945655 : ctx->global->destroy_value (t);
1472 : }
1473 :
1474 : /* This internal flag controls whether we should avoid doing anything during
1475 : constexpr evaluation that would cause extra DECL_UID generation, such as
1476 : template instantiation and function body copying. */
1477 :
1478 : static bool uid_sensitive_constexpr_evaluation_value;
1479 :
1480 : /* An internal counter that keeps track of the number of times
1481 : uid_sensitive_constexpr_evaluation_p returned true. */
1482 :
1483 : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1484 :
1485 : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1486 : increments the corresponding counter. */
1487 :
1488 : static bool
1489 8270948 : uid_sensitive_constexpr_evaluation_p ()
1490 : {
1491 8191142 : if (uid_sensitive_constexpr_evaluation_value)
1492 : {
1493 234984 : ++uid_sensitive_constexpr_evaluation_true_counter;
1494 234975 : return true;
1495 : }
1496 : else
1497 : return false;
1498 : }
1499 :
1500 : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1501 : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1502 : during the lifetime of the sentinel object. Upon its destruction, the
1503 : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1504 :
1505 76893666 : uid_sensitive_constexpr_evaluation_sentinel
1506 76893666 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1507 76893666 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1508 : {
1509 76893666 : }
1510 :
1511 : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1512 : records the current number of times that uid_sensitive_constexpr_evaluation_p
1513 : has been called and returned true. */
1514 :
1515 3624282945 : uid_sensitive_constexpr_evaluation_checker
1516 3624282945 : ::uid_sensitive_constexpr_evaluation_checker ()
1517 3624282945 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1518 : {
1519 3624282945 : }
1520 :
1521 : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1522 : some constexpr evaluation was restricted due to u_s_c_e_p being called
1523 : and returning true during the lifetime of this checker object. */
1524 :
1525 : bool
1526 2401318546 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1527 : {
1528 2401318546 : return (uid_sensitive_constexpr_evaluation_value
1529 2401318546 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1530 : }
1531 :
1532 :
1533 : /* A table of all constexpr calls that have been evaluated by the
1534 : compiler in this translation unit. */
1535 :
1536 : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1537 :
1538 : /* Compute a hash value for a constexpr call representation. */
1539 :
1540 : inline hashval_t
1541 191381137 : constexpr_call_hasher::hash (constexpr_call *info)
1542 : {
1543 191381137 : return info->hash;
1544 : }
1545 :
1546 : /* Return true if the objects pointed to by P and Q represent calls
1547 : to the same constexpr function with the same arguments.
1548 : Otherwise, return false. */
1549 :
1550 : bool
1551 199671465 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1552 : {
1553 199671465 : if (lhs == rhs)
1554 : return true;
1555 199671465 : if (lhs->hash != rhs->hash)
1556 : return false;
1557 20862998 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1558 : return false;
1559 20862998 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1560 : }
1561 :
1562 : /* Initialize the constexpr call table, if needed. */
1563 :
1564 : static void
1565 28700038 : maybe_initialize_constexpr_call_table (void)
1566 : {
1567 28700038 : if (constexpr_call_table == NULL)
1568 17782 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1569 28700038 : }
1570 :
1571 : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1572 : a function happens to get called recursively, we unshare the callee
1573 : function's body and evaluate this unshared copy instead of evaluating the
1574 : original body.
1575 :
1576 : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1577 : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1578 : that's keyed off of the original FUNCTION_DECL and whose value is a
1579 : TREE_LIST of this function's unused copies awaiting reuse.
1580 :
1581 : This is not GC-deletable to avoid GC affecting UID generation. */
1582 :
1583 : static GTY(()) decl_tree_map *fundef_copies_table;
1584 :
1585 : /* Reuse a copy or create a new unshared copy of the function FUN.
1586 : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1587 : is parms, TYPE is result. */
1588 :
1589 : static tree
1590 64376849 : get_fundef_copy (constexpr_fundef *fundef)
1591 : {
1592 64376849 : tree copy;
1593 64376849 : bool existed;
1594 64376849 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1595 64376849 : (fundef_copies_table, fundef->decl, &existed, 127));
1596 :
1597 64376849 : if (!existed)
1598 : {
1599 : /* There is no cached function available, or in use. We can use
1600 : the function directly. That the slot is now created records
1601 : that this function is now in use. */
1602 7060992 : copy = build_tree_list (fundef->body, fundef->parms);
1603 7060992 : TREE_TYPE (copy) = fundef->result;
1604 : }
1605 57315857 : else if (*slot == NULL_TREE)
1606 : {
1607 4402 : if (uid_sensitive_constexpr_evaluation_p ())
1608 9 : return NULL_TREE;
1609 :
1610 : /* We've already used the function itself, so make a copy. */
1611 4393 : copy = build_tree_list (NULL, NULL);
1612 4393 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1613 4393 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1614 4393 : tree saved_result = DECL_RESULT (fundef->decl);
1615 4393 : tree saved_fn = current_function_decl;
1616 4393 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1617 4393 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1618 4393 : DECL_RESULT (fundef->decl) = fundef->result;
1619 4393 : current_function_decl = fundef->decl;
1620 4393 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1621 4393 : TREE_TYPE (copy));
1622 4393 : current_function_decl = saved_fn;
1623 4393 : DECL_RESULT (fundef->decl) = saved_result;
1624 4393 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1625 4393 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1626 : }
1627 : else
1628 : {
1629 : /* We have a cached function available. */
1630 57311455 : copy = *slot;
1631 57311455 : *slot = TREE_CHAIN (copy);
1632 : }
1633 :
1634 : return copy;
1635 : }
1636 :
1637 : /* Save the copy COPY of function FUN for later reuse by
1638 : get_fundef_copy(). By construction, there will always be an entry
1639 : to find. */
1640 :
1641 : static void
1642 64376838 : save_fundef_copy (tree fun, tree copy)
1643 : {
1644 64376838 : tree *slot = fundef_copies_table->get (fun);
1645 64376838 : TREE_CHAIN (copy) = *slot;
1646 64376838 : *slot = copy;
1647 64376838 : }
1648 :
1649 : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1650 : value_cat, bool *, bool *, tree *);
1651 : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1652 : bool *, tree *);
1653 : static tree find_heap_var_refs (tree *, int *, void *);
1654 :
1655 : /* For exception object EXC if it has class type and usable what () method
1656 : which returns cv char * return the xmalloced string literal which it returns
1657 : if possible, otherwise return NULL. */
1658 :
1659 : static char *
1660 396 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1661 : {
1662 396 : tree type = strip_array_types (TREE_TYPE (exc));
1663 396 : if (!CLASS_TYPE_P (type))
1664 : return NULL;
1665 340 : tree std_exception = lookup_qualified_name (std_node, "exception",
1666 : LOOK_want::NORMAL, false);
1667 340 : if (TREE_CODE (std_exception) != TYPE_DECL)
1668 : return NULL;
1669 334 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1670 : return NULL;
1671 334 : base_kind b_kind;
1672 334 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1673 : tf_none);
1674 334 : if (binfo == NULL_TREE || binfo == error_mark_node)
1675 : return NULL;
1676 326 : if (type != TREE_TYPE (exc))
1677 288 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1678 326 : tree call
1679 326 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1680 : tf_none);
1681 326 : if (call == error_mark_node)
1682 : return NULL;
1683 326 : releasing_vec what_args;
1684 326 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1685 326 : if (call == error_mark_node)
1686 : return NULL;
1687 326 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1688 326 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1689 326 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1690 326 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1691 326 : TYPE_SIZE_UNIT (char_type_node))
1692 652 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1693 0 : return NULL;
1694 326 : if (!potential_constant_expression (call))
1695 : return NULL;
1696 326 : bool non_constant_p = false, overflow_p = false;
1697 326 : tree jmp_target = NULL;
1698 326 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1699 : &non_constant_p, &overflow_p,
1700 : &jmp_target);
1701 326 : if (throws (&jmp_target) || non_constant_p)
1702 : return NULL;
1703 326 : if (reduced_constant_expression_p (ptr))
1704 42 : if (const char *msg = c_getstr (ptr))
1705 42 : return xstrdup (msg);
1706 284 : auto_vec <char, 32> v;
1707 11754 : for (unsigned i = 0; i < INT_MAX; ++i)
1708 : {
1709 11754 : tree t = call;
1710 11754 : if (i)
1711 11470 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1712 11754 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1713 11754 : non_constant_p = false;
1714 11754 : overflow_p = false;
1715 11754 : jmp_target = NULL;
1716 11754 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1717 : &non_constant_p, &overflow_p,
1718 : &jmp_target);
1719 11754 : if (throws (&jmp_target)
1720 11754 : || non_constant_p
1721 11754 : || !tree_fits_shwi_p (t2))
1722 0 : return NULL;
1723 11754 : char c = tree_to_shwi (t2);
1724 11754 : v.safe_push (c);
1725 11754 : if (c == '\0')
1726 : break;
1727 : }
1728 568 : return xstrdup (v.address ());
1729 610 : }
1730 :
1731 : /* Diagnose constant expression evaluation encountering call to
1732 : std::terminate due to exception EXC. */
1733 :
1734 : static void
1735 22 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1736 : {
1737 22 : tree type = strip_array_types (TREE_TYPE (exc));
1738 22 : if (char *str = exception_what_str (ctx, exc))
1739 : {
1740 4 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1741 : "%<what()%>: %qs", "std::terminate", type, str);
1742 4 : free (str);
1743 : }
1744 : else
1745 : {
1746 18 : if (type != TREE_TYPE (exc))
1747 18 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1748 18 : bool non_constant_p = false, overflow_p = false;
1749 18 : tree jmp_target = NULL;
1750 18 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1751 : &non_constant_p, &overflow_p,
1752 : &jmp_target);
1753 18 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1754 18 : if (reduced_constant_expression_p (val))
1755 18 : error_at (loc, "%qs called after throwing an exception %qE",
1756 : "std::terminate", val);
1757 : else
1758 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1759 : "std::terminate", type);
1760 : }
1761 22 : }
1762 :
1763 : /* Diagnose constant expression evaluation encountering call to
1764 : uncaught exception EXC. */
1765 :
1766 : static void
1767 374 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1768 : {
1769 374 : tree type = strip_array_types (TREE_TYPE (exc));
1770 374 : if (char *str = exception_what_str (ctx, exc))
1771 : {
1772 322 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1773 322 : free (str);
1774 : }
1775 : else
1776 : {
1777 52 : if (type != TREE_TYPE (exc))
1778 52 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1779 52 : bool non_constant_p = false, overflow_p = false;
1780 52 : tree jmp_target = NULL;
1781 52 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1782 : &non_constant_p, &overflow_p,
1783 : &jmp_target);
1784 52 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1785 52 : if (reduced_constant_expression_p (val))
1786 50 : error_at (loc, "uncaught exception %qE", val);
1787 : else
1788 2 : error_at (loc, "uncaught exception of type %qT", type);
1789 : }
1790 374 : }
1791 :
1792 : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1793 : magic constexpr functions for C++26. */
1794 :
1795 : enum cxa_builtin {
1796 : CXA_NONE = 0,
1797 : CXA_ALLOCATE_EXCEPTION = 1,
1798 : CXA_FREE_EXCEPTION = 2,
1799 : CXA_THROW = 3,
1800 : CXA_BEGIN_CATCH = 4,
1801 : CXA_END_CATCH = 5,
1802 : CXA_RETHROW = 6,
1803 : CXA_GET_EXCEPTION_PTR = 7,
1804 : CXA_BAD_CAST = 8,
1805 : CXA_BAD_TYPEID = 9,
1806 : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1807 : STD_RETHROW_EXCEPTION = 11,
1808 : BUILTIN_EH_PTR_ADJUST_REF = 12,
1809 : BUILTIN_UNCAUGHT_EXCEPTIONS = 13,
1810 : BUILTIN_CURRENT_EXCEPTION = 14
1811 : };
1812 :
1813 : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1814 : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1815 :
1816 : static enum cxa_builtin
1817 25974508 : cxx_cxa_builtin_fn_p (tree fndecl)
1818 : {
1819 25974508 : if (cxx_dialect < cxx26)
1820 : return CXA_NONE;
1821 2029485 : if (DECL_LANGUAGE (fndecl) != lang_c)
1822 : {
1823 1609657 : if (!decl_in_std_namespace_p (fndecl))
1824 : return CXA_NONE;
1825 729536 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1826 : return STD_RETHROW_EXCEPTION;
1827 : return CXA_NONE;
1828 : }
1829 419828 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1830 : return CXA_NONE;
1831 57261 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1832 : return CXA_ALLOCATE_EXCEPTION;
1833 17455 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1834 : return CXA_FREE_EXCEPTION;
1835 17453 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1836 : return CXA_THROW;
1837 10579 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1838 : return CXA_BEGIN_CATCH;
1839 4109 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1840 : return CXA_END_CATCH;
1841 947 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1842 : return CXA_RETHROW;
1843 859 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1844 : return CXA_GET_EXCEPTION_PTR;
1845 823 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1846 : return CXA_BAD_CAST;
1847 527 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1848 : return CXA_BAD_TYPEID;
1849 514 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1850 : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1851 : return CXA_NONE;
1852 : }
1853 :
1854 : /* Helper function for cxx_eval_cxa_builtin_fn.
1855 : Check if ARG is a valid first argument of __cxa_throw or
1856 : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1857 : not, otherwise return the artificial __cxa_allocate_exception allocated
1858 : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1859 :
1860 : static tree
1861 1088 : cxa_check_throw_arg (tree arg, bool free_exc)
1862 : {
1863 1088 : STRIP_NOPS (arg);
1864 1088 : if (TREE_CODE (arg) != ADDR_EXPR)
1865 : return NULL_TREE;
1866 1088 : arg = TREE_OPERAND (arg, 0);
1867 1088 : if (!VAR_P (arg)
1868 1088 : || !DECL_ARTIFICIAL (arg)
1869 1088 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1870 1088 : && DECL_NAME (arg) != heap_identifier)
1871 2176 : || !DECL_LANG_SPECIFIC (arg))
1872 0 : return NULL_TREE;
1873 : return arg;
1874 : }
1875 :
1876 : /* Helper function for cxx_eval_cxa_builtin_fn.
1877 : "Allocate" on the constexpr heap an exception object of TYPE
1878 : with REFCOUNT. */
1879 :
1880 : static tree
1881 12050 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1882 : tree refcount)
1883 : {
1884 12050 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1885 12050 : DECL_ARTIFICIAL (var) = 1;
1886 12050 : retrofit_lang_decl (var);
1887 12050 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1888 12050 : ctx->global->heap_vars.safe_push (var);
1889 12050 : return var;
1890 : }
1891 :
1892 : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1893 : C++26 constexpr exception support (P3068R5). */
1894 :
1895 : static tree
1896 19111 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1897 : enum cxa_builtin kind, tree fndecl,
1898 : bool *non_constant_p, bool *overflow_p,
1899 : tree *jump_target)
1900 : {
1901 19111 : int nargs = call_expr_nargs (call);
1902 19111 : location_t loc = cp_expr_loc_or_input_loc (call);
1903 19111 : tree args[4], arg;
1904 19111 : if (nargs > 4)
1905 : {
1906 0 : invalid_nargs:
1907 0 : if (!ctx->quiet)
1908 0 : error_at (loc, "call to %qD function with incorrect "
1909 : "number of arguments", fndecl);
1910 0 : *non_constant_p = true;
1911 0 : return call;
1912 : }
1913 19111 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1914 4850 : && nargs == 1
1915 4850 : && (arg = CALL_EXPR_ARG (call, 0))
1916 4850 : && TREE_CODE (arg) == CALL_EXPR
1917 4850 : && call_expr_nargs (arg) == 1
1918 23961 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1919 4850 : if (tree fun = get_function_named_in_call (arg))
1920 4850 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1921 : {
1922 4850 : if (ctx->global->caught_exceptions.length () < 2)
1923 : {
1924 1656 : no_caught_exceptions:
1925 1656 : if (!ctx->quiet)
1926 0 : error_at (loc, "%qD called with no caught exceptions pending",
1927 : fndecl);
1928 1656 : *non_constant_p = true;
1929 1656 : return call;
1930 : }
1931 : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1932 : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1933 : ctx->global->caught_exceptions vector to end with
1934 : __cxa_allocate_exception created artificial VAR_DECL (the
1935 : exception object) followed by handler type, pushed by TRY_BLOCK
1936 : evaluation. The only difference between the functions is that
1937 : __cxa_begin_catch pops the handler type from the vector and keeps
1938 : the VAR_DECL last and decreases uncaught_exceptions. The
1939 : VAR_DECL after __cxa_begin_catch serves as the current exception
1940 : and is then popped in __cxa_end_catch evaluation. */
1941 3194 : tree handler_type = ctx->global->caught_exceptions.last ();
1942 3194 : if (handler_type && VAR_P (handler_type))
1943 0 : goto no_caught_exceptions;
1944 3194 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1945 3194 : arg = ctx->global->caught_exceptions[idx];
1946 3194 : gcc_assert (VAR_P (arg));
1947 3194 : if (kind == CXA_BEGIN_CATCH)
1948 : {
1949 3166 : ctx->global->caught_exceptions.pop ();
1950 3166 : --ctx->global->uncaught_exceptions;
1951 : }
1952 3194 : if (handler_type == NULL_TREE)
1953 : /* Used for catch (...). Just return void. */
1954 58 : return void_node;
1955 3136 : else if (POINTER_TYPE_P (handler_type))
1956 : {
1957 : /* Used for catch of a pointer. */
1958 66 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1959 66 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1960 : size_zero_node, NULL_TREE, NULL_TREE);
1961 66 : arg = cp_convert (handler_type, arg,
1962 66 : ctx->quiet ? tf_none : tf_warning_or_error);
1963 66 : if (arg == error_mark_node)
1964 : {
1965 0 : *non_constant_p = true;
1966 0 : return call;
1967 : }
1968 : }
1969 : else
1970 : {
1971 : /* Used for catch of a non-pointer type. */
1972 3070 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1973 3070 : tree exc_ptr_type = build_pointer_type (exc_type);
1974 3070 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
1975 3070 : if (CLASS_TYPE_P (handler_type))
1976 : {
1977 2991 : tree ptr_type = build_pointer_type (handler_type);
1978 2991 : arg = cp_convert (ptr_type, arg,
1979 2991 : ctx->quiet ? tf_none
1980 : : tf_warning_or_error);
1981 2991 : if (arg == error_mark_node)
1982 : {
1983 0 : *non_constant_p = true;
1984 0 : return call;
1985 : }
1986 : }
1987 : }
1988 3136 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
1989 : non_constant_p, overflow_p,
1990 3136 : jump_target);
1991 : }
1992 26141 : for (int i = 0; i < nargs; ++i)
1993 : {
1994 11880 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1995 : vc_prvalue, non_constant_p,
1996 : overflow_p, jump_target);
1997 11880 : if (*non_constant_p)
1998 : return call;
1999 11880 : if (*jump_target)
2000 : return NULL_TREE;
2001 : }
2002 14261 : switch (kind)
2003 : {
2004 8751 : case CXA_ALLOCATE_EXCEPTION:
2005 8751 : if (nargs != 1)
2006 0 : goto invalid_nargs;
2007 8751 : if (!tree_fits_uhwi_p (args[0]))
2008 : {
2009 0 : if (!ctx->quiet)
2010 0 : error_at (loc, "cannot allocate exception: size not constant");
2011 0 : *non_constant_p = true;
2012 0 : return call;
2013 : }
2014 : else
2015 : {
2016 17502 : tree type = build_array_type_nelts (char_type_node,
2017 8751 : tree_to_uhwi (args[0]));
2018 8751 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2019 8751 : ctx->global->put_value (var, NULL_TREE);
2020 8751 : return fold_convert (ptr_type_node, build_address (var));
2021 : }
2022 2 : case CXA_FREE_EXCEPTION:
2023 2 : if (nargs != 1)
2024 0 : goto invalid_nargs;
2025 2 : arg = cxa_check_throw_arg (args[0], true);
2026 2 : if (arg == NULL_TREE)
2027 : {
2028 0 : invalid_ptr:
2029 0 : if (!ctx->quiet)
2030 0 : error_at (loc, "first argument to %qD function not result of "
2031 : "%<__cxa_allocate_exception%>", fndecl);
2032 0 : *non_constant_p = true;
2033 0 : return call;
2034 : }
2035 2 : DECL_NAME (arg) = heap_deleted_identifier;
2036 2 : ctx->global->destroy_value (arg);
2037 2 : ctx->global->heap_dealloc_count++;
2038 2 : return void_node;
2039 982 : case CXA_THROW:
2040 982 : if (nargs != 3)
2041 0 : goto invalid_nargs;
2042 982 : arg = cxa_check_throw_arg (args[0], false);
2043 982 : if (arg == NULL_TREE)
2044 0 : goto invalid_ptr;
2045 982 : DECL_EXCEPTION_REFCOUNT (arg)
2046 982 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2047 : size_one_node);
2048 982 : ++ctx->global->uncaught_exceptions;
2049 982 : *jump_target = arg;
2050 982 : return void_node;
2051 0 : case CXA_BEGIN_CATCH:
2052 0 : case CXA_GET_EXCEPTION_PTR:
2053 0 : goto invalid_nargs;
2054 3162 : case CXA_END_CATCH:
2055 3162 : if (nargs != 0)
2056 0 : goto invalid_nargs;
2057 3162 : if (ctx->global->caught_exceptions.is_empty ())
2058 : {
2059 0 : no_active_exc:
2060 0 : if (!ctx->quiet)
2061 0 : error_at (loc, "%qD called with no caught exceptions active",
2062 : fndecl);
2063 0 : *non_constant_p = true;
2064 0 : return call;
2065 : }
2066 : else
2067 : {
2068 3162 : arg = ctx->global->caught_exceptions.pop ();
2069 3162 : if (arg == NULL_TREE || !VAR_P (arg))
2070 0 : goto no_active_exc;
2071 3162 : free_except:
2072 3210 : DECL_EXCEPTION_REFCOUNT (arg)
2073 3210 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2074 : size_one_node);
2075 3210 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2076 : {
2077 3057 : if (type_build_dtor_call (TREE_TYPE (arg)))
2078 : {
2079 : /* So that we don't complain about out-of-consteval use. */
2080 2914 : temp_override<tree> ovr (current_function_decl);
2081 2914 : if (ctx->call && ctx->call->fundef)
2082 2914 : current_function_decl = ctx->call->fundef->decl;
2083 2914 : tree cleanup
2084 2936 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2085 : : tf_warning_or_error));
2086 2914 : if (cleanup == error_mark_node)
2087 0 : *non_constant_p = true;
2088 2914 : tree jmp_target = NULL_TREE;
2089 2914 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2090 : non_constant_p, overflow_p,
2091 : &jmp_target);
2092 2914 : if (throws (&jmp_target))
2093 0 : *jump_target = jmp_target;
2094 2914 : }
2095 3057 : DECL_NAME (arg) = heap_deleted_identifier;
2096 3057 : ctx->global->destroy_value (arg);
2097 3057 : ctx->global->heap_dealloc_count++;
2098 : }
2099 : }
2100 3210 : return void_node;
2101 82 : case CXA_RETHROW:
2102 82 : if (nargs != 0)
2103 0 : goto invalid_nargs;
2104 82 : unsigned idx;
2105 164 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2106 68 : if (arg == NULL_TREE || !VAR_P (arg))
2107 0 : --idx;
2108 : else
2109 : break;
2110 82 : if (arg == NULL_TREE)
2111 : {
2112 14 : if (!ctx->quiet)
2113 4 : error_at (loc, "%qD called with no caught exceptions active",
2114 : fndecl);
2115 14 : *non_constant_p = true;
2116 14 : return call;
2117 : }
2118 68 : DECL_EXCEPTION_REFCOUNT (arg)
2119 68 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2120 68 : ++ctx->global->uncaught_exceptions;
2121 68 : *jump_target = arg;
2122 68 : return void_node;
2123 163 : case CXA_BAD_CAST:
2124 163 : case CXA_BAD_TYPEID:
2125 163 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2126 163 : if (nargs != 0)
2127 0 : goto invalid_nargs;
2128 : else
2129 : {
2130 163 : tree name;
2131 163 : switch (kind)
2132 : {
2133 123 : case CXA_BAD_CAST:
2134 123 : name = get_identifier ("bad_cast");
2135 123 : break;
2136 7 : case CXA_BAD_TYPEID:
2137 7 : name = get_identifier ("bad_typeid");
2138 7 : break;
2139 33 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2140 33 : name = get_identifier ("bad_array_new_length");
2141 33 : break;
2142 : default:
2143 : gcc_unreachable ();
2144 : }
2145 163 : tree decl = lookup_qualified_name (std_node, name);
2146 163 : if (TREE_CODE (decl) != TYPE_DECL
2147 135 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2148 298 : || !type_build_ctor_call (TREE_TYPE (decl)))
2149 : {
2150 28 : if (!ctx->quiet)
2151 8 : error_at (loc, "%qD called without %<std::%D%> being defined",
2152 : fndecl, name);
2153 28 : *non_constant_p = true;
2154 28 : return call;
2155 : }
2156 135 : tree type = TREE_TYPE (decl);
2157 135 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2158 135 : tree ctor
2159 135 : = build_special_member_call (var, complete_ctor_identifier,
2160 : NULL, type, LOOKUP_NORMAL,
2161 135 : ctx->quiet ? tf_none
2162 : : tf_warning_or_error);
2163 135 : if (ctor == error_mark_node)
2164 : {
2165 0 : *non_constant_p = true;
2166 0 : return call;
2167 : }
2168 135 : if (TREE_CONSTANT (ctor))
2169 0 : ctx->global->put_value (var, ctor);
2170 : else
2171 : {
2172 135 : ctx->global->put_value (var, NULL_TREE);
2173 135 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2174 : non_constant_p, overflow_p,
2175 : jump_target);
2176 135 : if (*non_constant_p)
2177 : return call;
2178 135 : if (throws (jump_target))
2179 : return NULL_TREE;
2180 : }
2181 135 : ++ctx->global->uncaught_exceptions;
2182 135 : *jump_target = var;
2183 : }
2184 135 : return void_node;
2185 60 : case BUILTIN_UNCAUGHT_EXCEPTIONS:
2186 60 : if (nargs != 0)
2187 0 : goto invalid_nargs;
2188 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2189 : want to give a definite answer during mce_unknown evaluation,
2190 : because that might prevent evaluation later on when some
2191 : exceptions might be uncaught. But unlike that, we don't
2192 : want to constant fold it even during cp_fold, because at runtime
2193 : std::uncaught_exceptions () might still be non-zero. */
2194 60 : if (ctx->manifestly_const_eval != mce_true)
2195 : {
2196 34 : *non_constant_p = true;
2197 34 : return call;
2198 : }
2199 26 : return build_int_cst (integer_type_node,
2200 26 : ctx->global->uncaught_exceptions);
2201 955 : case BUILTIN_CURRENT_EXCEPTION:
2202 955 : if (nargs != 0)
2203 0 : goto invalid_nargs;
2204 : else
2205 : {
2206 955 : tree name = get_identifier ("exception_ptr");
2207 955 : tree decl = lookup_qualified_name (std_node, name);
2208 955 : tree fld;
2209 955 : if (TREE_CODE (decl) != TYPE_DECL
2210 955 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2211 955 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2212 955 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2213 955 : || DECL_ARTIFICIAL (fld)
2214 955 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2215 955 : || next_aggregate_field (DECL_CHAIN (fld))
2216 1910 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2217 955 : TYPE_SIZE (TREE_TYPE (fld))))
2218 : {
2219 0 : if (!ctx->quiet)
2220 0 : error_at (loc, "%qD called without supportable %qs",
2221 : fndecl, "std::exception_ptr");
2222 0 : *non_constant_p = true;
2223 0 : return call;
2224 : }
2225 1910 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2226 31 : if (arg == NULL_TREE || !VAR_P (arg))
2227 0 : --idx;
2228 : else
2229 : break;
2230 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2231 : want to give a definite answer during mce_unknown evaluation,
2232 : because that might prevent evaluation later on when some
2233 : exceptions might be current. But unlike that, we don't
2234 : want to constant fold it to null even during cp_fold, because
2235 : at runtime std::current_exception () might still be non-null. */
2236 955 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2237 : {
2238 918 : *non_constant_p = true;
2239 918 : return call;
2240 : }
2241 37 : if (arg == NULL_TREE)
2242 6 : arg = build_zero_cst (TREE_TYPE (fld));
2243 : else
2244 : {
2245 31 : DECL_EXCEPTION_REFCOUNT (arg)
2246 31 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2247 : size_one_node);
2248 31 : arg = fold_convert (ptr_type_node, build_address (arg));
2249 : }
2250 37 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2251 : }
2252 27 : case STD_RETHROW_EXCEPTION:
2253 27 : if (nargs != 1)
2254 0 : goto invalid_nargs;
2255 27 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2256 : {
2257 27 : arg = args[0];
2258 27 : STRIP_NOPS (arg);
2259 27 : if (TREE_CODE (arg) == ADDR_EXPR)
2260 : {
2261 27 : args[0]
2262 27 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2263 : vc_prvalue, non_constant_p,
2264 : overflow_p, jump_target);
2265 27 : if (*non_constant_p)
2266 : return call;
2267 27 : if (*jump_target)
2268 : return NULL_TREE;
2269 : }
2270 : }
2271 27 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2272 27 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2273 : {
2274 0 : invalid_std_rethrow:
2275 0 : if (!ctx->quiet)
2276 0 : error_at (loc, "%qD called with unexpected %qs argument",
2277 : fndecl, "std::exception_ptr");
2278 0 : *non_constant_p = true;
2279 0 : return void_node;
2280 : }
2281 27 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2282 27 : if (arg == NULL_TREE)
2283 0 : goto invalid_std_rethrow;
2284 27 : DECL_EXCEPTION_REFCOUNT (arg)
2285 27 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2286 27 : ++ctx->global->uncaught_exceptions;
2287 27 : *jump_target = arg;
2288 27 : return void_node;
2289 77 : case BUILTIN_EH_PTR_ADJUST_REF:
2290 77 : if (nargs != 2)
2291 0 : goto invalid_nargs;
2292 77 : arg = cxa_check_throw_arg (args[0], false);
2293 77 : if (arg == NULL_TREE)
2294 0 : goto invalid_ptr;
2295 77 : if (integer_onep (args[1]))
2296 29 : DECL_EXCEPTION_REFCOUNT (arg)
2297 58 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2298 : size_one_node);
2299 48 : else if (integer_minus_onep (args[1]))
2300 48 : goto free_except;
2301 : else
2302 : {
2303 0 : if (!ctx->quiet)
2304 0 : error_at (loc, "%qD called with second argument "
2305 : "other than 1 or -1", fndecl);
2306 0 : *non_constant_p = true;
2307 : }
2308 29 : return void_node;
2309 0 : default:
2310 0 : gcc_unreachable ();
2311 : }
2312 : }
2313 :
2314 : /* Variables and functions to manage constexpr call expansion context.
2315 : These do not need to be marked for PCH or GC. */
2316 :
2317 : /* FIXME remember and print actual constant arguments. */
2318 : static vec<tree> call_stack;
2319 : static int call_stack_tick;
2320 : static int last_cx_error_tick;
2321 :
2322 : /* Attempt to evaluate T which represents a call to __builtin_constexpr_diag.
2323 : The arguments should be an integer (0 for inform, 1 for warning, 2 for
2324 : error) optionally with 16 ored in if it should use caller's caller location
2325 : instead of caller's location and 2 messages which are either a pointer to
2326 : a STRING_CST or class with data () and size () member functions like
2327 : string_view or u8string_view. The first message is a tag, with "" passed
2328 : for no tag, data () should return const char *, the tag should only contain
2329 : alphanumeric letters or underscores. The second message is the diagnostic
2330 : message, data () can be either const char * or const char8_t *. size ()
2331 : should return the corresponding length of the strings in bytes as an
2332 : integer. */
2333 :
2334 : static tree
2335 208 : cxx_eval_constexpr_diag (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
2336 : bool *overflow_p, tree *jump_target)
2337 : {
2338 208 : location_t loc = EXPR_LOCATION (t);
2339 208 : if (call_expr_nargs (t) != 3)
2340 : {
2341 12 : if (!ctx->quiet)
2342 12 : error_at (loc, "wrong number of arguments to %qs call",
2343 : "__builtin_constexpr_diag");
2344 12 : *non_constant_p = true;
2345 12 : return t;
2346 : }
2347 : tree args[3];
2348 784 : for (int i = 0; i < 3; ++i)
2349 : {
2350 588 : tree arg = convert_from_reference (CALL_EXPR_ARG (t, i));
2351 588 : arg = cxx_eval_constant_expression (ctx, arg,
2352 : (i == 0
2353 530 : || POINTER_TYPE_P (TREE_TYPE (arg)))
2354 980 : ? vc_prvalue : vc_glvalue,
2355 : non_constant_p, overflow_p,
2356 : jump_target);
2357 588 : if (*jump_target)
2358 : return NULL_TREE;
2359 588 : if (*non_constant_p)
2360 : return t;
2361 588 : args[i] = arg;
2362 : }
2363 392 : if (TREE_CODE (args[0]) != INTEGER_CST
2364 196 : || wi::to_widest (args[0]) < 0
2365 192 : || wi::to_widest (args[0]) > 18
2366 392 : || (wi::to_widest (args[0]) & 15) > 2)
2367 : {
2368 8 : if (!ctx->quiet)
2369 8 : error_at (loc, "first %qs call argument should be 0, 1, 2, 16, 17 or "
2370 : "18", "__builtin_constexpr_diag");
2371 8 : *non_constant_p = true;
2372 8 : return t;
2373 : }
2374 188 : const char *msgs[2] = {};
2375 188 : int lens[3] = {};
2376 1128 : cexpr_str cstrs[2];
2377 472 : diagnostics::kind kind = diagnostics::kind::error;
2378 472 : for (int i = 1; i < 3; ++i)
2379 : {
2380 340 : tree arg = args[i];
2381 340 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2382 : {
2383 202 : tree str = arg;
2384 202 : STRIP_NOPS (str);
2385 202 : if (TREE_CODE (str) == ADDR_EXPR
2386 202 : && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST)
2387 : {
2388 202 : str = TREE_OPERAND (str, 0);
2389 202 : tree eltype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)));
2390 202 : if (eltype == char_type_node
2391 24 : || (i == 2 && eltype == char8_type_node))
2392 340 : arg = str;
2393 : }
2394 : }
2395 340 : cstrs[i - 1].message = arg;
2396 340 : if (!cstrs[i - 1].type_check (loc, i == 2))
2397 : {
2398 40 : *non_constant_p = true;
2399 40 : return t;
2400 : }
2401 300 : if (!cstrs[i - 1].extract (loc, msgs[i - 1], lens[i - 1], ctx,
2402 : non_constant_p, overflow_p, jump_target))
2403 : {
2404 16 : if (*jump_target)
2405 : return NULL_TREE;
2406 16 : *non_constant_p = true;
2407 16 : return t;
2408 : }
2409 : }
2410 132 : if (msgs[0])
2411 : {
2412 724 : for (int i = 0; i < lens[0]; ++i)
2413 596 : if (!ISALNUM (msgs[0][i]) && msgs[0][i] != '_')
2414 : {
2415 4 : if (!ctx->quiet)
2416 4 : error_at (loc, "%qs tag string contains %qc character other than"
2417 : " letters, digits or %<_%>",
2418 : "__builtin_constexpr_diag", msgs[0][i]);
2419 4 : *non_constant_p = true;
2420 4 : return t;
2421 : }
2422 : }
2423 128 : if (ctx->manifestly_const_eval == mce_unknown)
2424 : {
2425 0 : *non_constant_p = true;
2426 0 : return t;
2427 : }
2428 128 : int arg0 = tree_to_uhwi (args[0]);
2429 128 : if (arg0 & 16)
2430 : {
2431 32 : arg0 &= 15;
2432 32 : if (!call_stack.is_empty ())
2433 : {
2434 32 : tree call = call_stack.last ();
2435 32 : if (EXPR_HAS_LOCATION (call))
2436 32 : loc = EXPR_LOCATION (call);
2437 : }
2438 : }
2439 128 : if (arg0 == 0)
2440 : kind = diagnostics::kind::note;
2441 80 : else if (arg0 == 1)
2442 36 : kind = diagnostics::kind::warning;
2443 128 : if (lens[0])
2444 : {
2445 88 : const char *color = "error";
2446 88 : if (kind == diagnostics::kind::note)
2447 : color = "note";
2448 60 : else if (kind == diagnostics::kind::warning)
2449 32 : color = "warning";
2450 88 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s [%r%.*s%R]",
2451 : lens[1], msgs[1], color, lens[0], msgs[0]);
2452 : }
2453 : else
2454 40 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s",
2455 : lens[1], msgs[1]);
2456 128 : return void_node;
2457 564 : }
2458 :
2459 : /* Attempt to evaluate T which represents a call to a builtin function.
2460 : We assume here that all builtin functions evaluate to scalar types
2461 : represented by _CST nodes. */
2462 :
2463 : static tree
2464 15621726 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2465 : value_cat lval,
2466 : bool *non_constant_p, bool *overflow_p,
2467 : tree *jump_target)
2468 : {
2469 15621726 : const int nargs = call_expr_nargs (t);
2470 15621726 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2471 15621726 : tree new_call;
2472 15621726 : int i;
2473 :
2474 : /* Don't fold __builtin_constant_p within a constexpr function. */
2475 15621726 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2476 :
2477 : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2478 : in a constexpr function until we have values for the parameters. */
2479 3131233 : if (bi_const_p
2480 3131233 : && ctx->manifestly_const_eval != mce_true
2481 1575919 : && current_function_decl
2482 1574761 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2483 : {
2484 1290468 : *non_constant_p = true;
2485 1290468 : return t;
2486 : }
2487 :
2488 14331258 : if (fndecl_built_in_p (fun, BUILT_IN_FRONTEND))
2489 3886487 : switch (DECL_FE_FUNCTION_CODE (fun))
2490 : {
2491 3874064 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
2492 : /* For __builtin_is_constant_evaluated, defer it if not
2493 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2494 : without manifestly_const_eval even expressions or parts thereof
2495 : which will later be manifestly const_eval evaluated), otherwise fold
2496 : it to true. */
2497 3874064 : if (ctx->manifestly_const_eval == mce_unknown)
2498 : {
2499 3827168 : *non_constant_p = true;
2500 3827168 : return t;
2501 : }
2502 46896 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2503 46896 : boolean_type_node);
2504 :
2505 6270 : case CP_BUILT_IN_SOURCE_LOCATION:
2506 6270 : {
2507 6270 : temp_override<tree> ovr (current_function_decl);
2508 6270 : if (ctx->call && ctx->call->fundef)
2509 2753 : current_function_decl = ctx->call->fundef->decl;
2510 6270 : return fold_builtin_source_location (t);
2511 6270 : }
2512 :
2513 77 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
2514 77 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2515 : fun, non_constant_p, overflow_p,
2516 77 : jump_target);
2517 :
2518 955 : case CP_BUILT_IN_CURRENT_EXCEPTION:
2519 955 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_CURRENT_EXCEPTION,
2520 : fun, non_constant_p, overflow_p,
2521 955 : jump_target);
2522 :
2523 60 : case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
2524 60 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_UNCAUGHT_EXCEPTIONS,
2525 : fun, non_constant_p, overflow_p,
2526 60 : jump_target);
2527 :
2528 208 : case CP_BUILT_IN_CONSTEXPR_DIAG:
2529 208 : return cxx_eval_constexpr_diag (ctx, t, non_constant_p, overflow_p,
2530 208 : jump_target);
2531 :
2532 : default:
2533 : break;
2534 : }
2535 :
2536 10449624 : int strops = 0;
2537 10449624 : int strret = 0;
2538 10449624 : bool bos = false;
2539 10449624 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2540 9738801 : switch (DECL_FUNCTION_CODE (fun))
2541 : {
2542 : case BUILT_IN_STRLEN:
2543 : case BUILT_IN_STRNLEN:
2544 10449534 : strops = 1;
2545 : break;
2546 112073 : case BUILT_IN_MEMCHR:
2547 112073 : case BUILT_IN_STRCHR:
2548 112073 : case BUILT_IN_STRRCHR:
2549 112073 : strops = 1;
2550 112073 : strret = 1;
2551 112073 : break;
2552 73468 : case BUILT_IN_MEMCMP:
2553 73468 : case BUILT_IN_STRCMP:
2554 73468 : strops = 2;
2555 73468 : break;
2556 28608 : case BUILT_IN_STRSTR:
2557 28608 : strops = 2;
2558 28608 : strret = 1;
2559 28608 : break;
2560 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2561 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2562 42 : case BUILT_IN_OBSERVABLE_CHKPT:
2563 : /* These builtins shall be ignored during constant expression
2564 : evaluation. */
2565 42 : return void_node;
2566 48 : case BUILT_IN_UNREACHABLE:
2567 48 : case BUILT_IN_TRAP:
2568 48 : if (!*non_constant_p && !ctx->quiet)
2569 : {
2570 : /* Do not allow__builtin_unreachable in constexpr function.
2571 : The __builtin_unreachable call with BUILTINS_LOCATION
2572 : comes from cp_maybe_instrument_return. */
2573 13 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2574 0 : error ("%<constexpr%> call flows off the end of the function");
2575 : else
2576 13 : error ("%q+E is not a constant expression", t);
2577 : }
2578 48 : *non_constant_p = true;
2579 48 : return t;
2580 2669 : case BUILT_IN_OBJECT_SIZE:
2581 2669 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
2582 2669 : bos = ctx->manifestly_const_eval == mce_true;
2583 2669 : break;
2584 : default:
2585 : break;
2586 : }
2587 :
2588 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2589 : return constant false for a non-constant argument. */
2590 10449534 : constexpr_ctx new_ctx = *ctx;
2591 10449534 : new_ctx.quiet = true;
2592 28215318 : for (i = 0; i < nargs; ++i)
2593 : {
2594 17765785 : tree arg = CALL_EXPR_ARG (t, i);
2595 17765785 : tree oarg = arg;
2596 :
2597 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2598 : expand_builtin doesn't know how to look in the values table. */
2599 17765785 : bool strop = i < strops;
2600 17765785 : if (strop)
2601 : {
2602 441757 : STRIP_NOPS (arg);
2603 441757 : if (TREE_CODE (arg) == ADDR_EXPR)
2604 7847 : arg = TREE_OPERAND (arg, 0);
2605 : else
2606 : strop = false;
2607 : }
2608 :
2609 : /* If builtin_valid_in_constant_expr_p is true,
2610 : potential_constant_expression_1 has not recursed into the arguments
2611 : of the builtin, verify it here. */
2612 17765785 : if (!builtin_valid_in_constant_expr_p (fun)
2613 17765785 : || potential_constant_expression (arg))
2614 : {
2615 17765654 : bool dummy1 = false, dummy2 = false;
2616 17765654 : tree jmp_target = NULL_TREE;
2617 17765654 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2618 : &dummy1, &dummy2, &jmp_target);
2619 17765653 : if (jmp_target)
2620 : {
2621 0 : *jump_target = jmp_target;
2622 0 : return NULL_TREE;
2623 : }
2624 : }
2625 :
2626 17765784 : if (bi_const_p)
2627 : /* For __builtin_constant_p, fold all expressions with constant values
2628 : even if they aren't C++ constant-expressions. */
2629 1840764 : arg = cp_fold_rvalue (arg);
2630 15925020 : else if (strop)
2631 : {
2632 7847 : if (TREE_CODE (arg) == CONSTRUCTOR)
2633 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2634 7847 : if (TREE_CODE (arg) == STRING_CST)
2635 2934 : arg = build_address (arg);
2636 : else
2637 : arg = oarg;
2638 : }
2639 :
2640 17765784 : args[i] = arg;
2641 : }
2642 10449533 : if (bos)
2643 : {
2644 96 : tree arg = args[0];
2645 96 : STRIP_NOPS (arg);
2646 96 : if (TREE_CODE (arg) == ADDR_EXPR)
2647 96 : args[0] = arg;
2648 : }
2649 :
2650 10449533 : bool save_ffbcp = force_folding_builtin_constant_p;
2651 10449533 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2652 10449533 : tree save_cur_fn = current_function_decl;
2653 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2654 10449533 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2655 37 : && ctx->call
2656 10449537 : && ctx->call->fundef)
2657 4 : current_function_decl = ctx->call->fundef->decl;
2658 10449533 : if (fndecl_built_in_p (fun,
2659 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2660 : BUILT_IN_FRONTEND))
2661 : {
2662 336 : location_t loc = EXPR_LOCATION (t);
2663 336 : if (nargs >= 1)
2664 333 : VERIFY_CONSTANT (args[0]);
2665 136 : new_call
2666 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2667 : args);
2668 : }
2669 10449197 : else if (fndecl_built_in_p (fun,
2670 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2671 : BUILT_IN_FRONTEND))
2672 : {
2673 471 : location_t loc = EXPR_LOCATION (t);
2674 471 : if (nargs >= 2)
2675 : {
2676 465 : VERIFY_CONSTANT (args[0]);
2677 237 : VERIFY_CONSTANT (args[1]);
2678 : }
2679 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2680 : }
2681 10448726 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
2682 : BUILT_IN_FRONTEND))
2683 : {
2684 4046 : location_t loc = EXPR_LOCATION (t);
2685 4046 : if (nargs >= 1)
2686 : {
2687 4046 : tree arg = CALL_EXPR_ARG (t, 0);
2688 4046 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2689 : non_constant_p, overflow_p,
2690 : jump_target);
2691 4046 : if (*jump_target)
2692 : return NULL_TREE;
2693 4046 : args[0] = arg;
2694 : }
2695 4046 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
2696 : }
2697 : else
2698 20889360 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2699 10444680 : CALL_EXPR_FN (t), nargs, args);
2700 10449105 : current_function_decl = save_cur_fn;
2701 10449105 : force_folding_builtin_constant_p = save_ffbcp;
2702 10449105 : if (new_call == NULL)
2703 : {
2704 6404391 : if (!*non_constant_p && !ctx->quiet)
2705 : {
2706 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2707 0 : CALL_EXPR_FN (t), nargs, args);
2708 0 : error ("%q+E is not a constant expression", new_call);
2709 : }
2710 6404391 : *non_constant_p = true;
2711 6404391 : return t;
2712 : }
2713 :
2714 4044714 : if (!potential_constant_expression (new_call))
2715 : {
2716 737 : if (!*non_constant_p && !ctx->quiet)
2717 4 : error ("%q+E is not a constant expression", new_call);
2718 737 : *non_constant_p = true;
2719 737 : return t;
2720 : }
2721 :
2722 4043977 : if (strret)
2723 : {
2724 : /* memchr returns a pointer into the first argument, but we replaced the
2725 : argument above with a STRING_CST; put it back it now. */
2726 283 : tree op = CALL_EXPR_ARG (t, strret-1);
2727 283 : STRIP_NOPS (new_call);
2728 283 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2729 142 : TREE_OPERAND (new_call, 0) = op;
2730 141 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2731 4043977 : new_call = op;
2732 : }
2733 :
2734 4043977 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2735 : non_constant_p, overflow_p,
2736 4043977 : jump_target);
2737 : }
2738 :
2739 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2740 : the type of the value to match. */
2741 :
2742 : static tree
2743 101328837 : adjust_temp_type (tree type, tree temp)
2744 : {
2745 101328837 : if (same_type_p (TREE_TYPE (temp), type))
2746 : return temp;
2747 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2748 45355196 : if (TREE_CODE (temp) == CONSTRUCTOR)
2749 : {
2750 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2751 3277136 : tree t = copy_node (temp);
2752 3277136 : TREE_TYPE (t) = type;
2753 3277136 : return t;
2754 : }
2755 42078060 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2756 0 : return build0 (EMPTY_CLASS_EXPR, type);
2757 42078060 : gcc_assert (scalarish_type_p (type));
2758 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2759 : type is cv-unqualified. */
2760 42078060 : return cp_fold_convert (cv_unqualified (type), temp);
2761 : }
2762 :
2763 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2764 : sub-CONSTRUCTORs. Otherwise return T.
2765 :
2766 : We use this whenever we initialize an object as a whole, whether it's a
2767 : parameter, a local variable, or a subobject, so that subsequent
2768 : modifications don't affect other places where it was used. */
2769 :
2770 : tree
2771 94815990 : unshare_constructor (tree t MEM_STAT_DECL)
2772 : {
2773 94815990 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2774 : return t;
2775 14280938 : auto_vec <tree*, 4> ptrs;
2776 14280938 : ptrs.safe_push (&t);
2777 14280938 : while (!ptrs.is_empty ())
2778 : {
2779 18275699 : tree *p = ptrs.pop ();
2780 18275699 : tree n = copy_node (*p PASS_MEM_STAT);
2781 29489646 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2782 18275699 : *p = n;
2783 18275699 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2784 18275699 : constructor_elt *ce;
2785 70251222 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2786 19418886 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2787 3994761 : ptrs.safe_push (&ce->value);
2788 : }
2789 14280938 : return t;
2790 14280938 : }
2791 :
2792 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2793 :
2794 : static void
2795 805930 : free_constructor (tree t)
2796 : {
2797 805930 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2798 0 : return;
2799 805930 : releasing_vec ctors;
2800 805930 : vec_safe_push (ctors, t);
2801 1649693 : while (!ctors->is_empty ())
2802 : {
2803 843763 : tree c = ctors->pop ();
2804 843763 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2805 : {
2806 : constructor_elt *ce;
2807 320315 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2808 206636 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2809 37833 : vec_safe_push (ctors, ce->value);
2810 113679 : ggc_free (elts);
2811 : }
2812 843763 : ggc_free (c);
2813 : }
2814 805930 : }
2815 :
2816 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2817 : if *TP is address of a static variable (or part of it) currently being
2818 : constructed or of a heap artificial variable. */
2819 :
2820 : static tree
2821 38530904 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2822 : {
2823 38530904 : if (TREE_CODE (*tp) == ADDR_EXPR)
2824 6774178 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2825 6774178 : if (VAR_P (var) && TREE_STATIC (var))
2826 : {
2827 4460250 : if (DECL_NAME (var) == heap_uninit_identifier
2828 4460250 : || DECL_NAME (var) == heap_identifier
2829 4460250 : || DECL_NAME (var) == heap_vec_uninit_identifier
2830 8920500 : || DECL_NAME (var) == heap_vec_identifier)
2831 : return var;
2832 :
2833 4460250 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2834 4460250 : if (global->get_value (var))
2835 : return var;
2836 : }
2837 38107743 : if (TYPE_P (*tp))
2838 2491 : *walk_subtrees = false;
2839 : return NULL_TREE;
2840 : }
2841 :
2842 : /* Subroutine of cxx_eval_call_expression.
2843 : We are processing a call expression (either CALL_EXPR or
2844 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2845 : all arguments and bind their values to correspondings
2846 : parameters, making up the NEW_CALL context. */
2847 :
2848 : static tree
2849 139237074 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2850 : tree orig_fun, bool *non_constant_p,
2851 : bool *overflow_p, bool *non_constant_args,
2852 : tree *jump_target)
2853 : {
2854 139237074 : int nargs = call_expr_nargs (t);
2855 139237074 : tree parms = DECL_ARGUMENTS (fun);
2856 139237074 : int i, j = 0;
2857 139237074 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2858 1473 : ++nargs;
2859 139237074 : if (DECL_HAS_VTT_PARM_P (fun)
2860 1475 : && fun != orig_fun
2861 139238547 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2862 1058 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2863 436 : ++nargs;
2864 : /* We don't record ellipsis args below. */
2865 139237074 : int nparms = list_length (parms);
2866 139237074 : int nbinds = nargs < nparms ? nargs : nparms;
2867 139237074 : tree binds = make_tree_vec (nbinds);
2868 :
2869 : /* The call is not a constant expression if it involves the cdtor for a type
2870 : with virtual bases before C++26. */
2871 139237074 : if (cxx_dialect < cxx26
2872 139237074 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2873 : {
2874 17 : if (!ctx->quiet)
2875 : {
2876 3 : error_at (cp_expr_loc_or_input_loc (t),
2877 : "call to non-%<constexpr%> function %qD", fun);
2878 3 : explain_invalid_constexpr_fn (fun);
2879 : }
2880 17 : *non_constant_p = true;
2881 17 : return binds;
2882 : }
2883 :
2884 245745526 : for (i = 0; i < nargs; ++i)
2885 : {
2886 164734681 : tree x, arg;
2887 164734681 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2888 164734681 : if (parms && DECL_BY_REFERENCE (parms))
2889 9367 : type = TREE_TYPE (type);
2890 164734681 : if (i == 1
2891 164734681 : && j == 0
2892 34399397 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2893 164736043 : && orig_fun != fun)
2894 : {
2895 1362 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2896 1362 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2897 416 : x = boolean_true_node;
2898 : else
2899 946 : x = boolean_false_node;
2900 : j = -1;
2901 : }
2902 164733319 : else if (i == 2
2903 164733319 : && j == -1
2904 1362 : && DECL_HAS_VTT_PARM_P (fun)
2905 1362 : && orig_fun != fun
2906 164734681 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2907 967 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2908 : {
2909 416 : x = build_zero_cst (type);
2910 416 : j = -2;
2911 : }
2912 : else
2913 164732903 : x = get_nth_callarg (t, i + j);
2914 : /* For member function, the first argument is a pointer to the implied
2915 : object. For a constructor, it might still be a dummy object, in
2916 : which case we get the real argument from ctx. */
2917 243615918 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2918 182118766 : && is_dummy_object (x))
2919 : {
2920 8204507 : x = ctx->object;
2921 8204507 : x = build_address (x);
2922 : }
2923 164734681 : if (TREE_ADDRESSABLE (type))
2924 : {
2925 : /* Undo convert_for_arg_passing work here. */
2926 16092 : x = convert_from_reference (x);
2927 16092 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2928 : non_constant_p, overflow_p,
2929 : jump_target);
2930 : }
2931 : else
2932 : /* Normally we would strip a TARGET_EXPR in an initialization context
2933 : such as this, but here we do the elision differently: we keep the
2934 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2935 164718589 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2936 : non_constant_p, overflow_p,
2937 : jump_target);
2938 164734681 : if (*jump_target)
2939 : break;
2940 : /* Check we aren't dereferencing a null pointer when calling a non-static
2941 : member function, which is undefined behaviour. */
2942 121807904 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2943 68950111 : && integer_zerop (arg)
2944 : /* But ignore calls from within compiler-generated code, to handle
2945 : cases like lambda function pointer conversion operator thunks
2946 : which pass NULL as the 'this' pointer. */
2947 164848875 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2948 : {
2949 314 : if (!ctx->quiet)
2950 6 : error_at (cp_expr_loc_or_input_loc (x),
2951 : "dereferencing a null pointer");
2952 314 : *non_constant_p = true;
2953 : }
2954 : /* Don't VERIFY_CONSTANT here. */
2955 164734609 : if (*non_constant_p && ctx->quiet)
2956 : break;
2957 : /* Just discard ellipsis args after checking their constantitude. */
2958 106508469 : if (!parms)
2959 288 : continue;
2960 :
2961 106508181 : if (!*non_constant_p)
2962 : {
2963 : /* Make sure the binding has the same type as the parm. But
2964 : only for constant args. */
2965 106507519 : if (TREE_ADDRESSABLE (type))
2966 : {
2967 9894 : if (!same_type_p (type, TREE_TYPE (arg)))
2968 : {
2969 9 : arg = build_fold_addr_expr (arg);
2970 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2971 9 : arg = convert_from_reference (arg);
2972 : }
2973 : }
2974 106497625 : else if (!TYPE_REF_P (type))
2975 75126757 : arg = adjust_temp_type (type, arg);
2976 106507519 : if (!TREE_CONSTANT (arg))
2977 69231655 : *non_constant_args = true;
2978 37275864 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2979 : /* The destructor needs to see any modifications the callee makes
2980 : to the argument. */
2981 0 : *non_constant_args = true;
2982 : /* If arg is or contains address of a heap artificial variable or
2983 : of a static variable being constructed, avoid caching the
2984 : function call, as those variables might be modified by the
2985 : function, or might be modified by the callers in between
2986 : the cached function and just read by the function. */
2987 37275864 : else if (!*non_constant_args
2988 37275864 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2989 : NULL))
2990 423161 : *non_constant_args = true;
2991 :
2992 : /* For virtual calls, adjust the this argument, so that it is
2993 : the object on which the method is called, rather than
2994 : one of its bases. */
2995 106507519 : if (i == 0 && DECL_VIRTUAL_P (fun))
2996 : {
2997 25137 : tree addr = arg;
2998 25137 : STRIP_NOPS (addr);
2999 25137 : if (TREE_CODE (addr) == ADDR_EXPR)
3000 : {
3001 25106 : tree obj = TREE_OPERAND (addr, 0);
3002 25106 : while (TREE_CODE (obj) == COMPONENT_REF
3003 10772 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
3004 35963 : && !same_type_ignoring_top_level_qualifiers_p
3005 10720 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
3006 137 : obj = TREE_OPERAND (obj, 0);
3007 25106 : if (obj != TREE_OPERAND (addr, 0))
3008 134 : arg = build_fold_addr_expr_with_type (obj,
3009 : TREE_TYPE (arg));
3010 : }
3011 : }
3012 106507519 : TREE_VEC_ELT (binds, i) = arg;
3013 : }
3014 106508181 : parms = TREE_CHAIN (parms);
3015 : }
3016 :
3017 : return binds;
3018 : }
3019 :
3020 : static int
3021 80518325 : push_cx_call_context (tree call)
3022 : {
3023 80518325 : ++call_stack_tick;
3024 80518325 : if (!EXPR_HAS_LOCATION (call))
3025 68903 : SET_EXPR_LOCATION (call, input_location);
3026 80518325 : call_stack.safe_push (call);
3027 80518325 : int len = call_stack.length ();
3028 80518325 : if (len > max_constexpr_depth)
3029 30 : return false;
3030 : return len;
3031 : }
3032 :
3033 : static void
3034 80518323 : pop_cx_call_context (void)
3035 : {
3036 80518323 : ++call_stack_tick;
3037 80518323 : call_stack.pop ();
3038 80518323 : }
3039 :
3040 : vec<tree>
3041 248797 : cx_error_context (void)
3042 : {
3043 248797 : vec<tree> r = vNULL;
3044 248797 : if (call_stack_tick != last_cx_error_tick
3045 248797 : && !call_stack.is_empty ())
3046 : r = call_stack;
3047 248797 : last_cx_error_tick = call_stack_tick;
3048 248797 : return r;
3049 : }
3050 :
3051 : /* E is an operand of a failed assertion, fold it either with or without
3052 : constexpr context. */
3053 :
3054 : static tree
3055 697 : fold_operand (tree e, const constexpr_ctx *ctx)
3056 : {
3057 697 : if (ctx)
3058 : {
3059 32 : bool new_non_constant_p = false, new_overflow_p = false;
3060 32 : tree jmp_target = NULL_TREE;
3061 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3062 : &new_non_constant_p,
3063 : &new_overflow_p, &jmp_target);
3064 : }
3065 : else
3066 665 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3067 697 : return e;
3068 : }
3069 :
3070 : /* If we have a condition in conjunctive normal form (CNF), find the first
3071 : failing clause. In other words, given an expression like
3072 :
3073 : true && true && false && true && false
3074 :
3075 : return the first 'false'. EXPR is the expression. */
3076 :
3077 : static tree
3078 320 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3079 : {
3080 421 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3081 : {
3082 : /* First check the left side... */
3083 186 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3084 186 : if (e == NULL_TREE)
3085 : /* ...if we didn't find a false clause, check the right side. */
3086 101 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3087 : return e;
3088 : }
3089 235 : tree e = contextual_conv_bool (expr, tf_none);
3090 235 : e = fold_operand (e, ctx);
3091 235 : if (integer_zerop (e))
3092 : /* This is the failing clause. */
3093 134 : return expr;
3094 : return NULL_TREE;
3095 : }
3096 :
3097 : /* Wrapper for find_failing_clause_r. */
3098 :
3099 : tree
3100 1674 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3101 : {
3102 1674 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3103 134 : if (tree e = find_failing_clause_r (ctx, expr))
3104 1674 : expr = e;
3105 1674 : return expr;
3106 : }
3107 :
3108 : /* Emit additional diagnostics for failing condition BAD.
3109 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3110 : If SHOW_EXPR_P is true, print the condition (because it was
3111 : instantiation-dependent). */
3112 :
3113 : void
3114 1674 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3115 : const constexpr_ctx *ctx /* = nullptr */)
3116 : {
3117 : /* Nobody wants to see the artificial (bool) cast. */
3118 1674 : bad = tree_strip_nop_conversions (bad);
3119 1674 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3120 3 : bad = TREE_OPERAND (bad, 0);
3121 :
3122 1674 : auto_diagnostic_nesting_level sentinel;
3123 :
3124 : /* Actually explain the failure if this is a concept check or a
3125 : requires-expression. */
3126 1674 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3127 287 : diagnose_constraints (cloc, bad, NULL_TREE);
3128 : /* Similarly if this is a standard trait. */
3129 1387 : else if (maybe_diagnose_standard_trait (cloc, bad))
3130 : ;
3131 1055 : else if (COMPARISON_CLASS_P (bad)
3132 1055 : && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))
3133 42 : || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))))
3134 : {
3135 231 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3136 231 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3137 231 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3138 231 : inform (cloc, "the comparison reduces to %qE", cond);
3139 : }
3140 824 : else if (show_expr_p)
3141 578 : inform (cloc, "%qE evaluates to false", bad);
3142 1674 : }
3143 :
3144 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3145 : do it without changing the current evaluation state. If it evaluates to
3146 : false, complain and return false; otherwise, return true. */
3147 :
3148 : static bool
3149 173470 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3150 : location_t loc, bool evaluated,
3151 : bool *non_constant_p, bool *overflow_p)
3152 : {
3153 173470 : if (*non_constant_p)
3154 : return true;
3155 :
3156 173470 : tree eval, jmp_target = NULL_TREE;
3157 173470 : if (!evaluated)
3158 : {
3159 173470 : if (!potential_rvalue_constant_expression (arg))
3160 16 : return true;
3161 :
3162 173454 : constexpr_ctx new_ctx = *ctx;
3163 173454 : new_ctx.quiet = true;
3164 173454 : bool new_non_constant_p = false, new_overflow_p = false;
3165 : /* Avoid modification of existing values. */
3166 173454 : modifiable_tracker ms (new_ctx.global);
3167 173454 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3168 : &new_non_constant_p,
3169 : &new_overflow_p, &jmp_target);
3170 173454 : }
3171 : else
3172 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3173 : non_constant_p,
3174 : overflow_p, &jmp_target);
3175 173454 : if (jmp_target)
3176 : return true;
3177 :
3178 173454 : if (!*non_constant_p && integer_zerop (eval))
3179 : {
3180 42 : if (!ctx->quiet)
3181 : {
3182 : /* See if we can find which clause was failing
3183 : (for logical AND). */
3184 13 : tree bad = find_failing_clause (ctx, arg);
3185 : /* If not, or its location is unusable, fall back to the
3186 : previous location. */
3187 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3188 :
3189 : /* Report the error. */
3190 13 : auto_diagnostic_group d;
3191 13 : error_at (cloc, msg);
3192 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3193 13 : return bad;
3194 13 : }
3195 29 : *non_constant_p = true;
3196 29 : return false;
3197 : }
3198 :
3199 : return true;
3200 : }
3201 :
3202 : /* Evaluate a call T to a GCC internal function when possible and return
3203 : the evaluated result or, under the control of CTX, give an error, set
3204 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3205 :
3206 : static tree
3207 371469 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3208 : value_cat lval,
3209 : bool *non_constant_p, bool *overflow_p,
3210 : tree *jump_target)
3211 : {
3212 371469 : enum tree_code opcode = ERROR_MARK;
3213 :
3214 371469 : switch (CALL_EXPR_IFN (t))
3215 : {
3216 165 : case IFN_UBSAN_NULL:
3217 165 : case IFN_UBSAN_BOUNDS:
3218 165 : case IFN_UBSAN_VPTR:
3219 165 : case IFN_FALLTHROUGH:
3220 165 : return void_node;
3221 :
3222 173470 : case IFN_ASSUME:
3223 173470 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3224 : G_("failed %<assume%> attribute assumption"),
3225 173470 : EXPR_LOCATION (t), /*eval*/false,
3226 : non_constant_p, overflow_p))
3227 : return t;
3228 173441 : return void_node;
3229 :
3230 : case IFN_ADD_OVERFLOW:
3231 : opcode = PLUS_EXPR;
3232 : break;
3233 359 : case IFN_SUB_OVERFLOW:
3234 359 : opcode = MINUS_EXPR;
3235 359 : break;
3236 194833 : case IFN_MUL_OVERFLOW:
3237 194833 : opcode = MULT_EXPR;
3238 194833 : break;
3239 :
3240 74 : case IFN_LAUNDER:
3241 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3242 : vc_prvalue, non_constant_p,
3243 74 : overflow_p, jump_target);
3244 :
3245 0 : case IFN_DEFERRED_INIT:
3246 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3247 :
3248 90 : case IFN_BSWAP:
3249 90 : case IFN_BITREVERSE:
3250 90 : {
3251 90 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3252 : vc_prvalue, non_constant_p,
3253 : overflow_p, jump_target);
3254 90 : if (*jump_target)
3255 : return NULL_TREE;
3256 90 : if (*non_constant_p)
3257 : return t;
3258 60 : location_t loc = cp_expr_loc_or_input_loc (t);
3259 60 : if (TREE_CODE (arg) != INTEGER_CST
3260 60 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg))
3261 60 : || !TYPE_UNSIGNED (TREE_TYPE (arg))
3262 120 : || (CALL_EXPR_IFN (t) == IFN_BSWAP
3263 30 : && (TYPE_PRECISION (TREE_TYPE (arg)) % 8) != 0))
3264 : {
3265 0 : if (!ctx->quiet)
3266 0 : error_at (loc, "call to internal function %qE", t);
3267 0 : *non_constant_p = true;
3268 0 : return t;
3269 : }
3270 60 : return fold_build_builtin_bswapg_bitreverseg (loc, CALL_EXPR_IFN (t),
3271 60 : arg);
3272 : }
3273 :
3274 2011 : case IFN_VEC_CONVERT:
3275 2011 : {
3276 2011 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3277 : vc_prvalue, non_constant_p,
3278 : overflow_p, jump_target);
3279 2011 : if (*jump_target)
3280 : return NULL_TREE;
3281 2011 : if (TREE_CODE (arg) == VECTOR_CST)
3282 1912 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3283 : return r;
3284 : }
3285 : /* FALLTHRU */
3286 :
3287 104 : default:
3288 104 : if (!ctx->quiet)
3289 0 : error_at (cp_expr_loc_or_input_loc (t),
3290 : "call to internal function %qE", t);
3291 104 : *non_constant_p = true;
3292 104 : return t;
3293 : }
3294 :
3295 : /* Evaluate constant arguments using OPCODE and return a complex
3296 : number containing the result and the overflow bit. */
3297 195659 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3298 : non_constant_p, overflow_p,
3299 : jump_target);
3300 195659 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3301 : non_constant_p, overflow_p,
3302 : jump_target);
3303 195659 : if (*jump_target)
3304 : return NULL_TREE;
3305 195659 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3306 : {
3307 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3308 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3309 2 : tree result = fold_binary_loc (loc, opcode, type,
3310 : fold_convert_loc (loc, type, arg0),
3311 : fold_convert_loc (loc, type, arg1));
3312 2 : tree ovf
3313 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3314 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3315 2 : if (TREE_OVERFLOW (result))
3316 0 : TREE_OVERFLOW (result) = 0;
3317 :
3318 2 : return build_complex (TREE_TYPE (t), result, ovf);
3319 : }
3320 :
3321 195657 : *non_constant_p = true;
3322 195657 : return t;
3323 : }
3324 :
3325 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3326 :
3327 : static void
3328 4620484 : clear_no_implicit_zero (tree ctor)
3329 : {
3330 4620484 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3331 : {
3332 1131120 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3333 4836685 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3334 1526019 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3335 433474 : clear_no_implicit_zero (e.value);
3336 : }
3337 4620484 : }
3338 :
3339 : /* Complain about a const object OBJ being modified in a constant expression.
3340 : EXPR is the MODIFY_EXPR expression performing the modification. */
3341 :
3342 : static void
3343 63 : modifying_const_object_error (tree expr, tree obj)
3344 : {
3345 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3346 63 : auto_diagnostic_group d;
3347 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3348 63 : "a constant expression", TREE_OPERAND (expr, 0));
3349 :
3350 : /* Find the underlying object that was declared as const. */
3351 63 : location_t decl_loc = UNKNOWN_LOCATION;
3352 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3353 75 : switch (TREE_CODE (probe))
3354 : {
3355 39 : case BIT_FIELD_REF:
3356 39 : case COMPONENT_REF:
3357 39 : {
3358 39 : tree elt = TREE_OPERAND (probe, 1);
3359 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3360 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3361 39 : probe = TREE_OPERAND (probe, 0);
3362 : }
3363 39 : break;
3364 :
3365 0 : case ARRAY_REF:
3366 0 : case REALPART_EXPR:
3367 0 : case IMAGPART_EXPR:
3368 0 : probe = TREE_OPERAND (probe, 0);
3369 0 : break;
3370 :
3371 36 : default:
3372 36 : decl_loc = location_of (probe);
3373 36 : break;
3374 : }
3375 63 : inform (decl_loc, "originally declared %<const%> here");
3376 63 : }
3377 :
3378 : /* Return true if FNDECL is a replaceable global allocation function that
3379 : should be usable during constant expression evaluation. */
3380 :
3381 : static inline bool
3382 27298500 : cxx_replaceable_global_alloc_fn (tree fndecl)
3383 : {
3384 27298500 : return (cxx_dialect >= cxx20
3385 25781598 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3386 1654303 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3387 28952489 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3388 844749 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3389 : }
3390 :
3391 : /* Return true if FNDECL is a placement new function that should be
3392 : usable during constant expression evaluation of std::construct_at. */
3393 :
3394 : static inline bool
3395 26286433 : cxx_placement_new_fn (tree fndecl)
3396 : {
3397 26286433 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3398 : }
3399 :
3400 : /* Return true if FNDECL is std::construct_at. */
3401 :
3402 : static inline bool
3403 2079747 : is_std_construct_at (tree fndecl)
3404 : {
3405 2079747 : if (!decl_in_std_namespace_p (fndecl))
3406 : return false;
3407 :
3408 2055598 : tree name = DECL_NAME (fndecl);
3409 2055598 : return name && id_equal (name, "construct_at");
3410 : }
3411 :
3412 : /* Overload for the above taking constexpr_call*. */
3413 :
3414 : static inline bool
3415 1838057 : is_std_construct_at (const constexpr_call *call)
3416 : {
3417 1838057 : return (call
3418 1700099 : && call->fundef
3419 3538156 : && is_std_construct_at (call->fundef->decl));
3420 : }
3421 :
3422 : /* True if CTX is an instance of std::NAME class. */
3423 :
3424 : bool
3425 10572189 : is_std_class (tree ctx, const char *name)
3426 : {
3427 10572189 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3428 : return false;
3429 :
3430 10571606 : tree decl = TYPE_MAIN_DECL (ctx);
3431 10571606 : tree dname = DECL_NAME (decl);
3432 10571606 : if (dname == NULL_TREE || !id_equal (dname, name))
3433 : return false;
3434 :
3435 576049 : return decl_in_std_namespace_p (decl);
3436 : }
3437 :
3438 : /* True if CTX is an instance of std::allocator. */
3439 :
3440 : bool
3441 422301 : is_std_allocator (tree ctx)
3442 : {
3443 422301 : return is_std_class (ctx, "allocator");
3444 : }
3445 :
3446 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3447 :
3448 : static bool
3449 445124 : is_std_allocator_allocate (tree fndecl)
3450 : {
3451 445124 : tree name = DECL_NAME (fndecl);
3452 445124 : if (name == NULL_TREE
3453 445124 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3454 : return false;
3455 :
3456 420842 : return is_std_allocator (DECL_CONTEXT (fndecl));
3457 : }
3458 :
3459 : /* Overload for the above taking constexpr_call*. */
3460 :
3461 : static inline bool
3462 272878 : is_std_allocator_allocate (const constexpr_call *call)
3463 : {
3464 272878 : return (call
3465 180902 : && call->fundef
3466 453780 : && is_std_allocator_allocate (call->fundef->decl));
3467 : }
3468 :
3469 : /* Return true if FNDECL is std::source_location::current. */
3470 :
3471 : static inline bool
3472 35182 : is_std_source_location_current (tree fndecl)
3473 : {
3474 35182 : if (!decl_in_std_namespace_p (fndecl))
3475 : return false;
3476 :
3477 24836 : tree name = DECL_NAME (fndecl);
3478 24836 : if (name == NULL_TREE || !id_equal (name, "current"))
3479 : return false;
3480 :
3481 159 : tree ctx = DECL_CONTEXT (fndecl);
3482 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3483 : return false;
3484 :
3485 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3486 159 : return name && id_equal (name, "source_location");
3487 : }
3488 :
3489 : /* Overload for the above taking constexpr_call*. */
3490 :
3491 : static inline bool
3492 43564 : is_std_source_location_current (const constexpr_call *call)
3493 : {
3494 43564 : return (call
3495 35182 : && call->fundef
3496 78746 : && is_std_source_location_current (call->fundef->decl));
3497 : }
3498 :
3499 : /* Return true if FNDECL is __dynamic_cast. */
3500 :
3501 : static inline bool
3502 25981231 : cxx_dynamic_cast_fn_p (tree fndecl)
3503 : {
3504 25981231 : return (cxx_dialect >= cxx20
3505 24464324 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3506 6727 : && CP_DECL_CONTEXT (fndecl) == abi_node
3507 : /* Only consider implementation-detail __dynamic_cast calls that
3508 : correspond to a dynamic_cast, and ignore direct calls to
3509 : abi::__dynamic_cast. */
3510 25987958 : && DECL_ARTIFICIAL (fndecl));
3511 : }
3512 :
3513 : /* Often, we have an expression in the form of address + offset, e.g.
3514 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3515 :
3516 : static tree
3517 5788 : extract_obj_from_addr_offset (tree expr)
3518 : {
3519 5788 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3520 2763 : expr = TREE_OPERAND (expr, 0);
3521 5788 : STRIP_NOPS (expr);
3522 5788 : if (TREE_CODE (expr) == ADDR_EXPR)
3523 5777 : expr = TREE_OPERAND (expr, 0);
3524 5788 : return expr;
3525 : }
3526 :
3527 : /* Given a PATH like
3528 :
3529 : g.D.2181.D.2154.D.2102.D.2093
3530 :
3531 : find a component with type TYPE. Return NULL_TREE if not found, and
3532 : error_mark_node if the component is not accessible. If STOP is non-null,
3533 : this function will return NULL_TREE if STOP is found before TYPE. */
3534 :
3535 : static tree
3536 2808 : get_component_with_type (tree path, tree type, tree stop)
3537 : {
3538 7852 : while (true)
3539 : {
3540 5330 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3541 : /* Found it. */
3542 : return path;
3543 3738 : else if (stop
3544 3738 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3545 : stop)))
3546 : return NULL_TREE;
3547 3693 : else if (TREE_CODE (path) == COMPONENT_REF
3548 3693 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3549 : {
3550 : /* We need to check that the component we're accessing is in fact
3551 : accessible. */
3552 3693 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3553 3693 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3554 1171 : return error_mark_node;
3555 2522 : path = TREE_OPERAND (path, 0);
3556 : }
3557 : else
3558 : return NULL_TREE;
3559 : }
3560 : }
3561 :
3562 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3563 :
3564 : The declaration of __dynamic_cast is:
3565 :
3566 : void* __dynamic_cast (const void* __src_ptr,
3567 : const __class_type_info* __src_type,
3568 : const __class_type_info* __dst_type,
3569 : ptrdiff_t __src2dst);
3570 :
3571 : where src2dst has the following possible values
3572 :
3573 : >-1: src_type is a unique public non-virtual base of dst_type
3574 : dst_ptr + src2dst == src_ptr
3575 : -1: unspecified relationship
3576 : -2: src_type is not a public base of dst_type
3577 : -3: src_type is a multiple public non-virtual base of dst_type */
3578 :
3579 : static tree
3580 3144 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3581 : bool *non_constant_p, bool *overflow_p,
3582 : tree *jump_target)
3583 : {
3584 : /* T will be something like
3585 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3586 : dismantle it. */
3587 3144 : gcc_assert (call_expr_nargs (call) == 4);
3588 3144 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3589 3144 : tree obj = CALL_EXPR_ARG (call, 0);
3590 3144 : tree type = CALL_EXPR_ARG (call, 2);
3591 3144 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3592 3144 : location_t loc = cp_expr_loc_or_input_loc (call);
3593 :
3594 : /* Get the target type of the dynamic_cast. */
3595 3144 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3596 3144 : type = TREE_OPERAND (type, 0);
3597 3144 : type = TREE_TYPE (DECL_NAME (type));
3598 :
3599 : /* TYPE can only be either T* or T&. We can't know which of these it
3600 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3601 : and something like "(T*)(T&)(T*) x" in the second case.
3602 : This is true for the reference cases in C++ < 26 or when exceptions
3603 : aren't enabled, in that case we should diagnose errors. For C++26
3604 : with exceptions we should silently evaluate to null pointer and
3605 : let the callers call __cxa_bad_cast () later to throw an exception. */
3606 3144 : bool fail_for_non_constant_p = false;
3607 12847 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3608 : {
3609 9703 : if (cxx_dialect < cxx26 || !flag_exceptions)
3610 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3611 9703 : obj = TREE_OPERAND (obj, 0);
3612 : }
3613 :
3614 : /* Evaluate the object so that we know its dynamic type. */
3615 3144 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3616 : overflow_p, jump_target);
3617 3144 : if (*non_constant_p)
3618 : return call;
3619 3025 : if (*jump_target)
3620 : return NULL_TREE;
3621 :
3622 : /* For dynamic_cast from classes with virtual bases we can get something
3623 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3624 : d.D.1234 using cxx_fold_indirect_ref. */
3625 3025 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3626 : {
3627 1183 : tree objo = obj;
3628 1183 : STRIP_NOPS (objo);
3629 1183 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3630 : {
3631 1152 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3632 : obj, NULL, jump_target);
3633 1152 : if (objo)
3634 1152 : obj = build_fold_addr_expr (objo);
3635 : }
3636 : }
3637 :
3638 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3639 : but when HINT is > 0, it can also be something like
3640 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3641 3025 : obj = extract_obj_from_addr_offset (obj);
3642 3025 : const tree objtype = TREE_TYPE (obj);
3643 : /* If OBJ doesn't refer to a base field, we're done. */
3644 5977 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3645 3025 : ? TREE_OPERAND (obj, 1) : obj))
3646 3025 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3647 : {
3648 73 : if (fail_for_non_constant_p)
3649 : {
3650 55 : if (!ctx->quiet)
3651 : {
3652 2 : auto_diagnostic_group d;
3653 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3654 2 : inform (loc, "dynamic type %qT of its operand does "
3655 : "not have a base class of type %qT",
3656 : objtype, type);
3657 2 : }
3658 55 : *non_constant_p = true;
3659 : }
3660 73 : return integer_zero_node;
3661 : }
3662 :
3663 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3664 : or in a destructor ... if the operand of the dynamic_cast refers
3665 : to the object under construction or destruction, this object is
3666 : considered to be a most derived object that has the type of the
3667 : constructor or destructor's class. */
3668 2952 : tree vtable = build_vfield_ref (obj, objtype);
3669 2952 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3670 : non_constant_p, overflow_p,
3671 : jump_target);
3672 2952 : if (*non_constant_p)
3673 : return call;
3674 2775 : if (*jump_target)
3675 : return NULL_TREE;
3676 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3677 : so it's possible that we got a null pointer now. */
3678 2775 : if (integer_zerop (vtable))
3679 : {
3680 12 : if (!ctx->quiet)
3681 3 : error_at (loc, "virtual table pointer is used uninitialized");
3682 12 : *non_constant_p = true;
3683 12 : return integer_zero_node;
3684 : }
3685 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3686 2763 : vtable = extract_obj_from_addr_offset (vtable);
3687 2763 : const tree mdtype = DECL_CONTEXT (vtable);
3688 :
3689 : /* Given dynamic_cast<T>(v),
3690 :
3691 : [expr.dynamic.cast] If C is the class type to which T points or refers,
3692 : the runtime check logically executes as follows:
3693 :
3694 : If, in the most derived object pointed (referred) to by v, v points
3695 : (refers) to a public base class subobject of a C object, and if only
3696 : one object of type C is derived from the subobject pointed (referred)
3697 : to by v the result points (refers) to that C object.
3698 :
3699 : In this case, HINT >= 0 or -3. */
3700 2763 : if (hint >= 0 || hint == -3)
3701 : {
3702 : /* Look for a component with type TYPE. */
3703 664 : tree t = get_component_with_type (obj, type, mdtype);
3704 : /* If not accessible, give an error. */
3705 664 : if (t == error_mark_node)
3706 : {
3707 198 : if (fail_for_non_constant_p)
3708 : {
3709 108 : if (!ctx->quiet)
3710 : {
3711 12 : auto_diagnostic_group d;
3712 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3713 12 : inform (loc, "static type %qT of its operand is a "
3714 : "non-public base class of dynamic type %qT",
3715 : objtype, type);
3716 :
3717 12 : }
3718 108 : *non_constant_p = true;
3719 : }
3720 198 : return integer_zero_node;
3721 : }
3722 466 : else if (t)
3723 : /* The result points to the TYPE object. */
3724 421 : return cp_build_addr_expr (t, complain);
3725 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3726 : Fall through to the normal processing. */
3727 : }
3728 :
3729 : /* Otherwise, if v points (refers) to a public base class subobject of the
3730 : most derived object, and the type of the most derived object has a base
3731 : class, of type C, that is unambiguous and public, the result points
3732 : (refers) to the C subobject of the most derived object.
3733 :
3734 : But it can also be an invalid case. */
3735 :
3736 : /* Get the most derived object. */
3737 2144 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3738 2144 : if (obj == error_mark_node)
3739 : {
3740 973 : if (fail_for_non_constant_p)
3741 : {
3742 350 : if (!ctx->quiet)
3743 : {
3744 38 : auto_diagnostic_group d;
3745 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3746 38 : inform (loc, "static type %qT of its operand is a non-public"
3747 : " base class of dynamic type %qT", objtype, mdtype);
3748 38 : }
3749 350 : *non_constant_p = true;
3750 : }
3751 973 : return integer_zero_node;
3752 : }
3753 : else
3754 1171 : gcc_assert (obj);
3755 :
3756 : /* Check that the type of the most derived object has a base class
3757 : of type TYPE that is unambiguous and public. */
3758 1171 : base_kind b_kind;
3759 1171 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3760 1171 : if (!binfo || binfo == error_mark_node)
3761 : {
3762 441 : if (fail_for_non_constant_p)
3763 : {
3764 134 : if (!ctx->quiet)
3765 : {
3766 16 : auto_diagnostic_group d;
3767 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3768 16 : if (b_kind == bk_ambig)
3769 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3770 : "type %qT of its operand", type, mdtype);
3771 : else
3772 10 : inform (loc, "dynamic type %qT of its operand does not "
3773 : "have an unambiguous public base class %qT",
3774 : mdtype, type);
3775 16 : }
3776 134 : *non_constant_p = true;
3777 : }
3778 441 : return integer_zero_node;
3779 : }
3780 : /* If so, return the TYPE subobject of the most derived object. */
3781 730 : obj = convert_to_base_statically (obj, binfo);
3782 730 : return cp_build_addr_expr (obj, complain);
3783 : }
3784 :
3785 : /* Data structure used by replace_decl and replace_decl_r. */
3786 :
3787 : struct replace_decl_data
3788 : {
3789 : /* The _DECL we want to replace. */
3790 : tree decl;
3791 : /* The replacement for DECL. */
3792 : tree replacement;
3793 : /* Trees we've visited. */
3794 : hash_set<tree> *pset;
3795 : /* Whether we've performed any replacements. */
3796 : bool changed;
3797 : };
3798 :
3799 : /* Helper function for replace_decl, called through cp_walk_tree. */
3800 :
3801 : static tree
3802 11813504 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3803 : {
3804 11813504 : replace_decl_data *d = (replace_decl_data *) data;
3805 :
3806 : /* We could be replacing
3807 : &<retval>.bar -> &foo.bar
3808 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3809 : on the ADDR_EXPR around it. */
3810 11813504 : if (TREE_CODE (*tp) == ADDR_EXPR)
3811 : {
3812 1065240 : d->pset->add (*tp);
3813 1065240 : auto save_changed = d->changed;
3814 1065240 : d->changed = false;
3815 1065240 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3816 1065240 : if (d->changed)
3817 : {
3818 1518 : cxx_mark_addressable (*tp);
3819 1518 : recompute_tree_invariant_for_addr_expr (*tp);
3820 : }
3821 : else
3822 1063722 : d->changed = save_changed;
3823 1065240 : *walk_subtrees = 0;
3824 : }
3825 10748264 : else if (*tp == d->decl)
3826 : {
3827 1741 : *tp = unshare_expr (d->replacement);
3828 1741 : d->changed = true;
3829 1741 : *walk_subtrees = 0;
3830 : }
3831 10746523 : else if (TYPE_P (*tp)
3832 10746523 : || d->pset->add (*tp))
3833 2398633 : *walk_subtrees = 0;
3834 :
3835 11813504 : return NULL_TREE;
3836 : }
3837 :
3838 : /* Replace every occurrence of DECL with (an unshared copy of)
3839 : REPLACEMENT within the expression *TP. Returns true iff a
3840 : replacement was performed. */
3841 :
3842 : bool
3843 1529739 : replace_decl (tree *tp, tree decl, tree replacement)
3844 : {
3845 1529739 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3846 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3847 1529739 : hash_set<tree> pset;
3848 1529739 : replace_decl_data data = { decl, replacement, &pset, false };
3849 1529739 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3850 1529739 : return data.changed;
3851 1529739 : }
3852 :
3853 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3854 :
3855 : static tree
3856 30 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3857 : value_cat lval,
3858 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3859 : {
3860 30 : tree function = THUNK_TARGET (thunk_fndecl);
3861 :
3862 30 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3863 : {
3864 13 : if (!ctx->quiet)
3865 : {
3866 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3867 : {
3868 0 : error ("call to non-%<constexpr%> function %qD", function);
3869 0 : explain_invalid_constexpr_fn (function);
3870 : }
3871 : else
3872 : /* virtual_offset is only set for virtual bases, which make the
3873 : class non-literal, so we don't need to handle it here. */
3874 3 : error ("calling constexpr member function %qD through virtual "
3875 : "base subobject", function);
3876 : }
3877 13 : *non_constant_p = true;
3878 13 : return t;
3879 : }
3880 :
3881 17 : tree new_call = copy_node (t);
3882 17 : CALL_EXPR_FN (new_call) = function;
3883 17 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3884 :
3885 17 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3886 :
3887 17 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3888 : {
3889 : /* 'this'-adjusting thunk. */
3890 11 : tree this_arg = CALL_EXPR_ARG (t, 0);
3891 11 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3892 : this_arg, offset);
3893 11 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3894 : }
3895 : else
3896 : /* Return-adjusting thunk. */
3897 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3898 : new_call, offset);
3899 :
3900 17 : return cxx_eval_constant_expression (ctx, new_call, lval,
3901 : non_constant_p, overflow_p,
3902 17 : jump_target);
3903 : }
3904 :
3905 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3906 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3907 : 'tors to detect modifying const objects in a constexpr context. */
3908 :
3909 : static void
3910 8388713 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3911 : bool readonly_p, bool *non_constant_p,
3912 : bool *overflow_p, tree *jump_target)
3913 : {
3914 16777426 : if (CLASS_TYPE_P (TREE_TYPE (object))
3915 16777426 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3916 : {
3917 : /* Subobjects might not be stored in ctx->global->values but we
3918 : can get its CONSTRUCTOR by evaluating *this. */
3919 979274 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3920 : non_constant_p, overflow_p,
3921 : jump_target);
3922 979274 : if (!*non_constant_p
3923 9329723 : && !throws (jump_target)
3924 1920284 : && TREE_CODE (e) == CONSTRUCTOR)
3925 941010 : TREE_READONLY (e) = readonly_p;
3926 : }
3927 8388713 : }
3928 :
3929 : /* Allocate an exception for OBJECT and throw it. */
3930 :
3931 : tree
3932 3164 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
3933 : tree object)
3934 : {
3935 3164 : tree type = TREE_TYPE (object);
3936 : /* This simulates a call to __cxa_allocate_exception. We need
3937 : (struct exception *) &heap -- memory on the heap so that
3938 : it can survive the stack being unwound. */
3939 3164 : tree arr = build_array_of_n_type (type, 1);
3940 3164 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
3941 3164 : DECL_NAME (var) = heap_identifier;
3942 3164 : ctx->global->put_value (var, NULL_TREE);
3943 :
3944 : /* *(struct exception *) &heap = exc{ ... } */
3945 3164 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
3946 3164 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
3947 3164 : bool non_constant_p = false, overflow_p = false;
3948 3164 : tree jump_target = NULL_TREE;
3949 3164 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
3950 : &overflow_p, &jump_target);
3951 3164 : if (non_constant_p)
3952 : {
3953 0 : if (!ctx->quiet)
3954 0 : error_at (loc, "couldn%'t throw %qT", type);
3955 0 : return NULL_TREE;
3956 : }
3957 :
3958 : /* Now we can __cxa_throw. */
3959 3164 : DECL_EXCEPTION_REFCOUNT (var)
3960 3164 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
3961 3164 : ++ctx->global->uncaught_exceptions;
3962 :
3963 3164 : return var;
3964 : }
3965 :
3966 : /* Subroutine of cxx_eval_constant_expression.
3967 : Evaluate the call expression tree T in the context of OLD_CALL expression
3968 : evaluation. */
3969 :
3970 : static tree
3971 156084973 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3972 : value_cat lval,
3973 : bool *non_constant_p, bool *overflow_p,
3974 : tree *jump_target)
3975 : {
3976 156084973 : location_t loc = cp_expr_loc_or_input_loc (t);
3977 156084973 : tree fun = get_function_named_in_call (t);
3978 :
3979 156084973 : if (fun == NULL_TREE)
3980 371469 : return cxx_eval_internal_function (ctx, t, lval,
3981 : non_constant_p, overflow_p,
3982 371469 : jump_target);
3983 :
3984 155713504 : if (TREE_CODE (fun) != FUNCTION_DECL)
3985 : {
3986 : /* Might be a constexpr function pointer. */
3987 224750 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3988 : non_constant_p, overflow_p,
3989 : jump_target);
3990 224750 : if (*jump_target)
3991 : return NULL_TREE;
3992 224750 : STRIP_NOPS (fun);
3993 224750 : if (TREE_CODE (fun) == ADDR_EXPR)
3994 30391 : fun = TREE_OPERAND (fun, 0);
3995 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3996 : indirection, the called expression is a pointer into the
3997 : virtual table which should contain FDESC_EXPR. Extract the
3998 : FUNCTION_DECL from there. */
3999 : else if (TARGET_VTABLE_USES_DESCRIPTORS
4000 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
4001 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
4002 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
4003 : {
4004 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
4005 : if (VAR_P (d)
4006 : && DECL_VTABLE_OR_VTT_P (d)
4007 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
4008 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
4009 : && DECL_INITIAL (d)
4010 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
4011 : {
4012 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
4013 : TYPE_SIZE_UNIT (vtable_entry_type));
4014 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
4015 : if (idx >= 0)
4016 : {
4017 : tree fdesc
4018 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
4019 : if (TREE_CODE (fdesc) == FDESC_EXPR
4020 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
4021 : fun = TREE_OPERAND (fdesc, 0);
4022 : }
4023 : }
4024 : }
4025 : }
4026 155713504 : if (TREE_CODE (fun) != FUNCTION_DECL)
4027 : {
4028 194359 : if (!ctx->quiet && !*non_constant_p)
4029 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
4030 : "function", fun);
4031 194359 : *non_constant_p = true;
4032 194359 : return t;
4033 : }
4034 155519145 : tree orig_fun = fun;
4035 155519145 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
4036 18354650 : fun = DECL_CLONED_FUNCTION (fun);
4037 :
4038 155519145 : if (is_ubsan_builtin_p (fun))
4039 58 : return void_node;
4040 :
4041 155519087 : if (fndecl_built_in_p (fun))
4042 15621726 : return cxx_eval_builtin_function_call (ctx, t, fun,
4043 : lval, non_constant_p, overflow_p,
4044 15621725 : jump_target);
4045 139897361 : if (DECL_THUNK_P (fun))
4046 30 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
4047 30 : jump_target);
4048 139897331 : if (metafunction_p (fun))
4049 : {
4050 : /* To be able to evaluate a metafunction, we may have to instantiate
4051 : constexpr functions. If we're not allowed to instantiate, leave
4052 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4053 : otherwise we might fold those prematurely. See
4054 : g++.dg/reflect/p2996-17.C. */
4055 75404 : if (uid_sensitive_constexpr_evaluation_p ()
4056 74844 : || ctx->manifestly_const_eval == mce_unknown)
4057 : {
4058 20176 : *non_constant_p = true;
4059 20176 : return t;
4060 : }
4061 55228 : ctx->global->metafns_called = true;
4062 55228 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4063 : jump_target);
4064 55228 : if (*jump_target)
4065 : return NULL_TREE;
4066 52052 : if (*non_constant_p)
4067 : return t;
4068 51754 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4069 : non_constant_p, overflow_p,
4070 : jump_target);
4071 51754 : if (*jump_target)
4072 : return NULL_TREE;
4073 51754 : if (*non_constant_p)
4074 : return t;
4075 : return e;
4076 : }
4077 139821927 : bool non_constexpr_call = false;
4078 139821927 : if (!maybe_constexpr_fn (fun))
4079 : {
4080 603069 : if (TREE_CODE (t) == CALL_EXPR
4081 601392 : && cxx_replaceable_global_alloc_fn (fun)
4082 996605 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4083 179951 : || is_std_allocator_allocate (ctx->call)))
4084 : {
4085 309942 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4086 309942 : const int nargs = call_expr_nargs (t);
4087 309942 : tree arg0 = NULL_TREE;
4088 518383 : for (int i = 0; i < nargs; ++i)
4089 : {
4090 310746 : tree arg = CALL_EXPR_ARG (t, i);
4091 310746 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4092 : non_constant_p, overflow_p,
4093 : jump_target);
4094 310746 : if (*jump_target)
4095 : return NULL_TREE;
4096 : /* Deleting a non-constant pointer has a better error message
4097 : below. */
4098 310736 : if (new_op_p || i != 0)
4099 262934 : VERIFY_CONSTANT (arg);
4100 160639 : if (i == 0)
4101 : arg0 = arg;
4102 : }
4103 207637 : gcc_assert (arg0);
4104 207637 : if (new_op_p)
4105 : {
4106 159835 : if (!tree_fits_uhwi_p (arg0))
4107 : {
4108 : /* We should not get here; the VERIFY_CONSTANT above
4109 : should have already caught it. */
4110 0 : gcc_checking_assert (false);
4111 : if (!ctx->quiet)
4112 : error_at (loc, "cannot allocate array: size not constant");
4113 : *non_constant_p = true;
4114 : return t;
4115 : }
4116 319670 : tree type = build_array_type_nelts (char_type_node,
4117 159835 : tree_to_uhwi (arg0));
4118 159835 : tree var = build_decl (loc, VAR_DECL,
4119 159835 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4120 : & OVL_OP_FLAG_VEC)
4121 : ? heap_vec_uninit_identifier
4122 : : heap_uninit_identifier,
4123 159835 : type);
4124 159835 : DECL_ARTIFICIAL (var) = 1;
4125 159835 : ctx->global->heap_vars.safe_push (var);
4126 159835 : ctx->global->put_value (var, NULL_TREE);
4127 159835 : return fold_convert (ptr_type_node, build_address (var));
4128 : }
4129 : else
4130 : {
4131 47802 : STRIP_NOPS (arg0);
4132 47802 : if (TREE_CODE (arg0) == ADDR_EXPR
4133 47802 : && VAR_P (TREE_OPERAND (arg0, 0)))
4134 : {
4135 47802 : tree var = TREE_OPERAND (arg0, 0);
4136 47802 : if (DECL_NAME (var) == heap_uninit_identifier
4137 47802 : || DECL_NAME (var) == heap_identifier)
4138 : {
4139 47628 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4140 : & OVL_OP_FLAG_VEC)
4141 : {
4142 9 : if (!ctx->quiet)
4143 : {
4144 3 : auto_diagnostic_group d;
4145 3 : error_at (loc, "array deallocation of object "
4146 : "allocated with non-array "
4147 : "allocation");
4148 3 : inform (DECL_SOURCE_LOCATION (var),
4149 : "allocation performed here");
4150 3 : }
4151 9 : *non_constant_p = true;
4152 9 : return t;
4153 : }
4154 47619 : DECL_NAME (var) = heap_deleted_identifier;
4155 47619 : ctx->global->destroy_value (var);
4156 47619 : ctx->global->heap_dealloc_count++;
4157 47619 : return void_node;
4158 : }
4159 174 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4160 174 : || DECL_NAME (var) == heap_vec_identifier)
4161 : {
4162 150 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4163 : & OVL_OP_FLAG_VEC) == 0)
4164 : {
4165 9 : if (!ctx->quiet)
4166 : {
4167 3 : auto_diagnostic_group d;
4168 3 : error_at (loc, "non-array deallocation of "
4169 : "object allocated with array "
4170 : "allocation");
4171 3 : inform (DECL_SOURCE_LOCATION (var),
4172 : "allocation performed here");
4173 3 : }
4174 9 : *non_constant_p = true;
4175 9 : return t;
4176 : }
4177 141 : DECL_NAME (var) = heap_deleted_identifier;
4178 141 : ctx->global->destroy_value (var);
4179 141 : ctx->global->heap_dealloc_count++;
4180 141 : return void_node;
4181 : }
4182 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4183 : {
4184 15 : if (!ctx->quiet)
4185 6 : error_at (loc, "deallocation of already deallocated "
4186 : "storage");
4187 15 : *non_constant_p = true;
4188 15 : return t;
4189 : }
4190 : }
4191 9 : if (!ctx->quiet)
4192 3 : error_at (loc, "deallocation of storage that was "
4193 : "not previously allocated");
4194 9 : *non_constant_p = true;
4195 9 : return t;
4196 : }
4197 : }
4198 : /* Allow placement new in std::construct_at, just return the second
4199 : argument. */
4200 293127 : if (TREE_CODE (t) == CALL_EXPR
4201 291450 : && cxx_placement_new_fn (fun)
4202 467633 : && is_std_construct_at (ctx->call))
4203 : {
4204 44891 : const int nargs = call_expr_nargs (t);
4205 44891 : tree arg1 = NULL_TREE;
4206 134673 : for (int i = 0; i < nargs; ++i)
4207 : {
4208 89782 : tree arg = CALL_EXPR_ARG (t, i);
4209 89782 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4210 : non_constant_p, overflow_p,
4211 : jump_target);
4212 89782 : if (*jump_target)
4213 : return NULL_TREE;
4214 89782 : if (i == 1)
4215 : arg1 = arg;
4216 : else
4217 89782 : VERIFY_CONSTANT (arg);
4218 : }
4219 44891 : gcc_assert (arg1);
4220 : return arg1;
4221 : }
4222 248236 : else if (cxx_dynamic_cast_fn_p (fun))
4223 3144 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4224 3144 : jump_target);
4225 245092 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4226 18019 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4227 : non_constant_p, overflow_p,
4228 18019 : jump_target);
4229 :
4230 : /* Calls to non-constexpr functions can be diagnosed right away
4231 : before C++26, though in C++26 evaluation of the arguments might
4232 : throw and if caught it could be still constant expression.
4233 : So for C++26 this is diagnosed only after
4234 : cxx_bind_parameters_in_call. */
4235 227073 : if (cxx_dialect >= cxx26)
4236 : non_constexpr_call = true;
4237 : else
4238 : {
4239 208857 : if (!ctx->quiet)
4240 : {
4241 193 : if (!lambda_static_thunk_p (fun))
4242 193 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4243 193 : explain_invalid_constexpr_fn (fun);
4244 : }
4245 208857 : *non_constant_p = true;
4246 208857 : return t;
4247 : }
4248 : }
4249 :
4250 139237074 : constexpr_ctx new_ctx = *ctx;
4251 139237074 : ctx = &new_ctx;
4252 156621176 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4253 141896069 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4254 : {
4255 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4256 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4257 2099 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4258 2099 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4259 2099 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4260 2099 : ctx->global->put_value (new_ctx.object, ctor);
4261 : }
4262 : /* An immediate invocation is manifestly constant evaluated including the
4263 : arguments of the call, so use mce_true even for the argument
4264 : evaluation. */
4265 278474148 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4266 3676772 : new_ctx.manifestly_const_eval = mce_true;
4267 :
4268 : /* We used to shortcut trivial constructor/op= here, but nowadays
4269 : we can only get a trivial function here with -fno-elide-constructors. */
4270 139237142 : gcc_checking_assert (!trivial_fn_p (fun)
4271 : || !flag_elide_constructors
4272 : /* Or it's a call from maybe_thunk_body (111075). */
4273 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4274 : : AGGR_INIT_FROM_THUNK_P (t))
4275 : /* We don't elide constructors when processing
4276 : a noexcept-expression. */
4277 : || cp_noexcept_operand);
4278 :
4279 139237074 : bool non_constant_args = false;
4280 139237074 : constexpr_call new_call;
4281 139237074 : new_call.bindings
4282 139237074 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4283 : overflow_p, &non_constant_args,
4284 : jump_target);
4285 :
4286 : /* We build up the bindings list before we know whether we already have this
4287 : call cached. If we don't end up saving these bindings, ggc_free them when
4288 : this function exits. */
4289 139237074 : class free_bindings
4290 : {
4291 : tree *bindings;
4292 : public:
4293 139237074 : free_bindings (tree &b): bindings (&b) { }
4294 139237072 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4295 4338062 : void preserve () { bindings = NULL; }
4296 139237074 : } fb (new_call.bindings);
4297 :
4298 139237074 : if (*jump_target)
4299 : return NULL_TREE;
4300 139237002 : if (*non_constant_p)
4301 : return t;
4302 81010192 : if (non_constexpr_call)
4303 : {
4304 2281 : if (!ctx->quiet)
4305 : {
4306 230 : if (!lambda_static_thunk_p (fun))
4307 230 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4308 230 : explain_invalid_constexpr_fn (fun);
4309 : }
4310 2281 : *non_constant_p = true;
4311 2281 : return t;
4312 : }
4313 :
4314 : /* We can't defer instantiating the function any longer. */
4315 81007911 : if (!DECL_INITIAL (fun)
4316 2914191 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4317 81007911 : && !uid_sensitive_constexpr_evaluation_p ())
4318 : {
4319 2679939 : location_t save_loc = input_location;
4320 2679939 : input_location = loc;
4321 2679939 : ++function_depth;
4322 2679939 : if (ctx->manifestly_const_eval == mce_true)
4323 341091 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4324 2679939 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4325 2679927 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4326 : else
4327 12 : synthesize_method (fun);
4328 2679939 : --function_depth;
4329 2679939 : input_location = save_loc;
4330 : }
4331 :
4332 : /* If in direct recursive call, optimize definition search. */
4333 81007911 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4334 27621 : new_call.fundef = ctx->call->fundef;
4335 : else
4336 : {
4337 80980290 : new_call.fundef = retrieve_constexpr_fundef (fun);
4338 80980290 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4339 80504056 : || new_call.fundef->result == error_mark_node
4340 80490707 : || fun == current_function_decl)
4341 : {
4342 489586 : if (!ctx->quiet)
4343 : {
4344 : /* We need to check for current_function_decl here in case we're
4345 : being called during cp_fold_function, because at that point
4346 : DECL_INITIAL is set properly and we have a fundef but we
4347 : haven't lowered invisirefs yet (c++/70344). */
4348 187 : if (DECL_INITIAL (fun) == error_mark_node
4349 187 : || fun == current_function_decl)
4350 15 : error_at (loc, "%qD called in a constant expression before its "
4351 : "definition is complete", fun);
4352 172 : else if (DECL_INITIAL (fun))
4353 : {
4354 : /* The definition of fun was somehow unsuitable. But pretend
4355 : that lambda static thunks don't exist. */
4356 134 : if (!lambda_static_thunk_p (fun))
4357 122 : error_at (loc, "%qD called in a constant expression", fun);
4358 134 : explain_invalid_constexpr_fn (fun);
4359 : }
4360 : else
4361 38 : error_at (loc, "%qD used before its definition", fun);
4362 : }
4363 489586 : *non_constant_p = true;
4364 489586 : return t;
4365 : }
4366 : }
4367 :
4368 : /* Don't complain about problems evaluating an ill-formed function. */
4369 80518325 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4370 80518325 : if (f->language->erroneous)
4371 606 : new_ctx.quiet = true;
4372 :
4373 80518325 : int depth_ok = push_cx_call_context (t);
4374 :
4375 : /* Remember the object we are constructing or destructing. */
4376 80518325 : tree new_obj = NULL_TREE;
4377 161036650 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4378 : {
4379 : /* In a cdtor, it should be the first `this' argument.
4380 : At this point it has already been evaluated in the call
4381 : to cxx_bind_parameters_in_call. */
4382 10249454 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4383 10249454 : bool empty_base = false;
4384 10249454 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4385 : &empty_base, jump_target);
4386 10249454 : if (*jump_target)
4387 0 : return NULL_TREE;
4388 : /* If we're initializing an empty class, don't set constness, because
4389 : cxx_fold_indirect_ref will return the wrong object to set constness
4390 : of. */
4391 10249454 : if (empty_base)
4392 : new_obj = NULL_TREE;
4393 4732975 : else if (ctx->call && ctx->call->fundef
4394 17901687 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4395 : {
4396 2548674 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4397 2548674 : STRIP_NOPS (cur_obj);
4398 2548674 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4399 2547396 : cur_obj = TREE_OPERAND (cur_obj, 0);
4400 2548674 : if (new_obj == cur_obj)
4401 : /* We're calling the target constructor of a delegating
4402 : constructor, or accessing a base subobject through a
4403 : NOP_EXPR as part of a call to a base constructor, so
4404 : there is no new (sub)object. */
4405 1860719 : new_obj = NULL_TREE;
4406 : }
4407 : }
4408 :
4409 80518325 : tree result = NULL_TREE;
4410 :
4411 80518325 : constexpr_call *entry = NULL;
4412 80518325 : if (depth_ok && !non_constant_args && ctx->strict)
4413 : {
4414 28700038 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4415 28700038 : new_call.hash
4416 28700038 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4417 :
4418 : /* If we have seen this call before, we are done. */
4419 28700038 : maybe_initialize_constexpr_call_table ();
4420 28700038 : bool insert = depth_ok < constexpr_cache_depth;
4421 28700038 : constexpr_call **slot
4422 36656045 : = constexpr_call_table->find_slot (&new_call,
4423 : insert ? INSERT : NO_INSERT);
4424 28700038 : entry = slot ? *slot : NULL;
4425 25200974 : if (entry == NULL)
4426 : {
4427 : /* Only cache up to constexpr_cache_depth to limit memory use. */
4428 7837126 : if (insert)
4429 : {
4430 : /* We need to keep a pointer to the entry, not just the slot, as
4431 : the slot can move during evaluation of the body. */
4432 4338062 : *slot = entry = ggc_alloc<constexpr_call> ();
4433 4338062 : *entry = new_call;
4434 4338062 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4435 4338062 : fb.preserve ();
4436 :
4437 : /* Unshare args going into the hash table to separate them
4438 : from the caller's context, for better GC and to avoid
4439 : problems with verify_gimple. */
4440 4338062 : tree bound = new_call.bindings;
4441 6992104 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4442 : {
4443 2654042 : tree &arg = TREE_VEC_ELT (bound, i);
4444 2654042 : arg = unshare_expr_without_location (arg);
4445 : }
4446 : }
4447 : }
4448 : /* Calls that are in progress have their result set to unknown_type_node,
4449 : so that we can detect circular dependencies. Now that we only cache
4450 : up to constexpr_cache_depth this won't catch circular dependencies that
4451 : start deeper, but they'll hit the recursion or ops limit. */
4452 20862912 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4453 : {
4454 6 : if (!ctx->quiet)
4455 0 : error ("call has circular dependency");
4456 6 : *non_constant_p = true;
4457 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4458 : }
4459 : else
4460 20862906 : result = entry->result (ctx->manifestly_const_eval);
4461 : }
4462 :
4463 80518325 : if (!depth_ok)
4464 : {
4465 30 : if (!ctx->quiet)
4466 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4467 : "%<-fconstexpr-depth=%> to increase the maximum)",
4468 : max_constexpr_depth);
4469 30 : *non_constant_p = true;
4470 30 : result = error_mark_node;
4471 : }
4472 : else
4473 : {
4474 80518295 : bool cacheable = !!entry;
4475 80518295 : if (result && result != error_mark_node)
4476 : /* OK */;
4477 64376849 : else if (!DECL_SAVED_TREE (fun))
4478 : {
4479 : /* When at_eof >= 3, cgraph has started throwing away
4480 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4481 : late code generation for VEC_INIT_EXPR, which needs to be
4482 : completely reconsidered. */
4483 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4484 0 : *non_constant_p = true;
4485 : }
4486 64376849 : else if (tree copy = get_fundef_copy (new_call.fundef))
4487 : {
4488 64376840 : tree body, parms, res;
4489 64376840 : releasing_vec ctors;
4490 :
4491 : /* Reuse or create a new unshared copy of this function's body. */
4492 64376840 : body = TREE_PURPOSE (copy);
4493 64376840 : parms = TREE_VALUE (copy);
4494 64376840 : res = TREE_TYPE (copy);
4495 :
4496 : /* Associate the bindings with the remapped parms. */
4497 64376840 : tree bound = new_call.bindings;
4498 64376840 : tree remapped = parms;
4499 155139276 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4500 : {
4501 90762436 : tree arg = TREE_VEC_ELT (bound, i);
4502 90762436 : if (entry)
4503 : {
4504 : /* Unshare again so the callee doesn't change the
4505 : argument values in the hash table. XXX Could we unshare
4506 : lazily in cxx_eval_store_expression? */
4507 2815343 : arg = unshare_constructor (arg);
4508 2815343 : if (TREE_CODE (arg) == CONSTRUCTOR)
4509 805428 : vec_safe_push (ctors, arg);
4510 : }
4511 90762436 : ctx->global->put_value (remapped, arg);
4512 90762436 : remapped = DECL_CHAIN (remapped);
4513 : }
4514 64376840 : if (remapped)
4515 : {
4516 : /* We shouldn't have any parms without args, but fail gracefully
4517 : in error recovery. */
4518 6 : gcc_checking_assert (seen_error ());
4519 6 : *non_constant_p = true;
4520 : }
4521 : /* Add the RESULT_DECL to the values map, too. */
4522 64376840 : gcc_assert (!DECL_BY_REFERENCE (res));
4523 64376840 : ctx->global->put_value (res, NULL_TREE);
4524 :
4525 : /* Remember the current call we're evaluating. */
4526 64376840 : constexpr_ctx call_ctx = *ctx;
4527 64376840 : call_ctx.call = &new_call;
4528 64376840 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4529 64376840 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4530 64376840 : bool save_metafns_called = ctx->global->metafns_called;
4531 :
4532 : /* Make sure we fold std::is_constant_evaluated to true in an
4533 : immediate function. */
4534 128753680 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4535 2627960 : call_ctx.manifestly_const_eval = mce_true;
4536 :
4537 : /* If this is a constexpr destructor, the object's const and volatile
4538 : semantics are no longer in effect; see [class.dtor]p5. */
4539 72765553 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4540 819605 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4541 : non_constant_p, overflow_p, jump_target);
4542 :
4543 : /* If this is a constructor, we are beginning the lifetime of the
4544 : object we are initializing. */
4545 64376840 : if (new_obj
4546 16777426 : && DECL_CONSTRUCTOR_P (fun)
4547 7569108 : && TREE_CODE (new_obj) == COMPONENT_REF
4548 66330083 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4549 : {
4550 6184 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4551 6184 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4552 6184 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4553 : new_obj, ctor);
4554 6184 : cxx_eval_constant_expression (ctx, activate,
4555 : lval, non_constant_p, overflow_p,
4556 : jump_target);
4557 6184 : ggc_free (activate);
4558 6184 : if (*jump_target)
4559 0 : return NULL_TREE;
4560 : }
4561 :
4562 64376840 : ctx->global->metafns_called = false;
4563 :
4564 64376840 : tree jmp_target = NULL_TREE;
4565 64376840 : cxx_eval_constant_expression (&call_ctx, body,
4566 : vc_discard, non_constant_p, overflow_p,
4567 : &jmp_target);
4568 :
4569 64376838 : if (!*non_constant_p && throws (&jmp_target))
4570 : {
4571 1913 : result = NULL_TREE;
4572 1913 : cacheable = false;
4573 1913 : *jump_target = jmp_target;
4574 : }
4575 128749850 : else if (DECL_CONSTRUCTOR_P (fun))
4576 : /* This can be null for a subobject constructor call, in
4577 : which case what we care about is the initialization
4578 : side-effects rather than the value. We could get at the
4579 : value by evaluating *this, but we don't bother; there's
4580 : no need to put such a call in the hash table. */
4581 9334642 : result = lval ? ctx->object : ctx->ctor;
4582 55040283 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4583 5569452 : result = void_node;
4584 : else
4585 : {
4586 49470831 : result = ctx->global->get_value (res);
4587 9025607 : if (result == NULL_TREE && !*non_constant_p
4588 49471045 : && !DECL_DESTRUCTOR_P (fun))
4589 : {
4590 107 : if (!ctx->quiet)
4591 6 : error ("%<constexpr%> call flows off the end "
4592 : "of the function");
4593 107 : *non_constant_p = true;
4594 : }
4595 : }
4596 :
4597 64376838 : if (ctx->global->metafns_called)
4598 65662 : cacheable = false;
4599 64376838 : ctx->global->metafns_called |= save_metafns_called;
4600 :
4601 : /* At this point, the object's constructor will have run, so
4602 : the object is no longer under construction, and its possible
4603 : 'const' semantics now apply. Make a note of this fact by
4604 : marking the CONSTRUCTOR TREE_READONLY. */
4605 72765551 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4606 7569108 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4607 : non_constant_p, overflow_p, jump_target);
4608 :
4609 : /* Remove the parms/result from the values map. */
4610 64376838 : destroy_value_checked (ctx, res, non_constant_p);
4611 155139279 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4612 90762441 : destroy_value_checked (ctx, parm, non_constant_p);
4613 :
4614 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4615 65182266 : while (!ctors->is_empty ())
4616 : {
4617 805428 : tree c = ctors->pop ();
4618 805428 : if (c != result)
4619 805428 : free_constructor (c);
4620 : }
4621 :
4622 : /* Make the unshared function copy we used available for re-use. */
4623 64376838 : save_fundef_copy (fun, copy);
4624 :
4625 : /* If the call allocated some heap object that hasn't been
4626 : deallocated during the call, or if it deallocated some heap
4627 : object it has not allocated, the call isn't really stateless
4628 : for the constexpr evaluation and should not be cached.
4629 : It is fine if the call allocates something and deallocates it
4630 : too. */
4631 64376838 : if (cacheable
4632 73432261 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4633 9052471 : || (save_heap_dealloc_count
4634 9052471 : != ctx->global->heap_dealloc_count)))
4635 : {
4636 2952 : tree heap_var;
4637 2952 : unsigned int i;
4638 2952 : if ((ctx->global->heap_vars.length ()
4639 2952 : - ctx->global->heap_dealloc_count)
4640 2952 : != save_heap_alloc_count - save_heap_dealloc_count)
4641 : cacheable = false;
4642 : else
4643 72384 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4644 : save_heap_alloc_count)
4645 70598 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4646 : {
4647 : cacheable = false;
4648 : break;
4649 : }
4650 : }
4651 :
4652 : /* Rewrite all occurrences of the function's RESULT_DECL with the
4653 : current object under construction. */
4654 64376838 : if (!*non_constant_p
4655 52511978 : && ctx->object
4656 12089539 : && CLASS_TYPE_P (TREE_TYPE (res))
4657 66699337 : && !is_empty_class (TREE_TYPE (res)))
4658 : {
4659 1529613 : if (!same_type_ignoring_top_level_qualifiers_p
4660 1529613 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4661 0 : *non_constant_p = true;
4662 1529613 : else if (replace_decl (&result, res, ctx->object))
4663 : {
4664 1466 : cacheable = false;
4665 1466 : result = cxx_eval_constant_expression (ctx, result, lval,
4666 : non_constant_p,
4667 : overflow_p,
4668 : jump_target);
4669 1466 : if (*jump_target)
4670 : {
4671 0 : cacheable = false;
4672 0 : result = NULL_TREE;
4673 : }
4674 : }
4675 : }
4676 :
4677 : /* Only cache a permitted result of a constant expression. */
4678 64375372 : if (cacheable && !reduced_constant_expression_p (result))
4679 : cacheable = false;
4680 :
4681 : /* Only cache a result without contract violations. */
4682 59736602 : if (cacheable && ctx->global->contract_statement)
4683 59963192 : cacheable = false;
4684 64376838 : }
4685 : else
4686 : /* Couldn't get a function copy to evaluate. */
4687 9 : *non_constant_p = true;
4688 :
4689 80518293 : if (result == error_mark_node)
4690 0 : *non_constant_p = true;
4691 80518293 : if (*non_constant_p || *overflow_p)
4692 11865025 : result = error_mark_node;
4693 68653268 : else if (!result)
4694 2630164 : result = void_node;
4695 80518293 : if (entry)
4696 : {
4697 50401946 : entry->result (ctx->manifestly_const_eval)
4698 25200973 : = cacheable ? result : error_mark_node;
4699 :
4700 25200973 : if (result != error_mark_node
4701 20599196 : && ctx->manifestly_const_eval == mce_unknown)
4702 : {
4703 : /* Evaluation succeeded and was independent of whether we're in a
4704 : manifestly constant-evaluated context, so we can also reuse
4705 : this result when evaluating this call with a fixed context. */
4706 1777661 : if (!entry->result (mce_true))
4707 670106 : entry->result (mce_true) = entry->result (mce_unknown);
4708 1777661 : if (!entry->result (mce_false))
4709 639469 : entry->result (mce_false) = entry->result (mce_unknown);
4710 : }
4711 : }
4712 : }
4713 :
4714 : /* The result of a constexpr function must be completely initialized.
4715 :
4716 : However, in C++20, a constexpr constructor doesn't necessarily have
4717 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4718 : in order to detect reading an uninitialized object in constexpr instead
4719 : of value-initializing it. (reduced_constant_expression_p is expected to
4720 : take care of clearing the flag.) */
4721 80518323 : if (TREE_CODE (result) == CONSTRUCTOR
4722 80518323 : && (cxx_dialect < cxx20
4723 17252376 : || !DECL_CONSTRUCTOR_P (fun)))
4724 4187010 : clear_no_implicit_zero (result);
4725 :
4726 80518323 : pop_cx_call_context ();
4727 80518323 : return result;
4728 139237072 : }
4729 :
4730 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4731 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4732 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4733 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4734 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4735 :
4736 : bool
4737 1122105907 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4738 : {
4739 1122105907 : if (t == NULL_TREE)
4740 : return false;
4741 :
4742 1117507157 : switch (TREE_CODE (t))
4743 : {
4744 : case PTRMEM_CST:
4745 : case REFLECT_EXPR:
4746 : /* Even if we can't lower this yet, it's constant. */
4747 : return true;
4748 :
4749 : case OMP_DECLARE_MAPPER:
4750 : return true;
4751 :
4752 54299292 : case CONSTRUCTOR:
4753 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4754 54299292 : tree field;
4755 54299292 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4756 : /* A constant vector would be folded to VECTOR_CST.
4757 : A CONSTRUCTOR of scalar type means uninitialized. */
4758 : return false;
4759 54283315 : if (CONSTRUCTOR_NO_CLEARING (t))
4760 : {
4761 3231221 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4762 : {
4763 : /* There must be a valid constant initializer at every array
4764 : index. */
4765 5208 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4766 5208 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4767 5208 : tree cursor = min;
4768 89763 : for (auto &e: CONSTRUCTOR_ELTS (t))
4769 : {
4770 74249 : if (!reduced_constant_expression_p (e.value))
4771 : return false;
4772 74199 : if (array_index_cmp (cursor, e.index) != 0)
4773 : return false;
4774 74139 : if (TREE_CODE (e.index) == RANGE_EXPR)
4775 0 : cursor = TREE_OPERAND (e.index, 1);
4776 74139 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4777 0 : cursor
4778 0 : = int_const_binop (PLUS_EXPR, cursor,
4779 0 : size_int (RAW_DATA_LENGTH (e.value)));
4780 : else
4781 74139 : cursor = int_const_binop (PLUS_EXPR, cursor,
4782 74139 : size_one_node);
4783 : }
4784 5098 : if (find_array_ctor_elt (t, max) == -1)
4785 : return false;
4786 5010 : goto ok;
4787 : }
4788 3226013 : else if (cxx_dialect >= cxx20
4789 3226013 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4790 : {
4791 : /* A union can have at most one active member. A union with no
4792 : active member has no constituent values, so all constituent
4793 : values are constant. */
4794 : field = NULL_TREE;
4795 : }
4796 : else
4797 3224050 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4798 : }
4799 : else
4800 : field = NULL_TREE;
4801 546780756 : for (auto &e: CONSTRUCTOR_ELTS (t))
4802 : {
4803 : /* If VAL is null, we're in the middle of initializing this
4804 : element. */
4805 437651445 : if (!reduced_constant_expression_p (e.value,
4806 437651445 : (e.index
4807 437651409 : && (TREE_CODE (e.index)
4808 : == FIELD_DECL))
4809 46896230 : ? DECL_SIZE (e.index)
4810 : : NULL_TREE))
4811 : return false;
4812 : /* We want to remove initializers for empty fields in a struct to
4813 : avoid confusing output_constructor. */
4814 437140710 : if (is_empty_field (e.index)
4815 437140710 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4816 : return false;
4817 : /* Check for non-empty fields between initialized fields when
4818 : CONSTRUCTOR_NO_CLEARING. */
4819 436093248 : for (; field && e.index != field;
4820 362261 : field = next_subobject_field (DECL_CHAIN (field)))
4821 365933 : if (!is_really_empty_class (TREE_TYPE (field),
4822 : /*ignore_vptr*/false))
4823 : return false;
4824 435727315 : if (field)
4825 3665136 : field = next_subobject_field (DECL_CHAIN (field));
4826 : }
4827 : /* There could be a non-empty field at the end. */
4828 52619565 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4829 270689 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4830 : {
4831 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4832 : the parent FIELD_DECL (if any) for classes with virtual
4833 : bases. */
4834 5101 : if (cxx_dialect >= cxx26
4835 3299 : && sz
4836 5610 : && tree_int_cst_le (sz, bit_position (field)))
4837 : break;
4838 4715 : return false;
4839 : }
4840 52348876 : ok:
4841 52354272 : if (CONSTRUCTOR_NO_CLEARING (t))
4842 : /* All the fields are initialized. */
4843 3117083 : CONSTRUCTOR_NO_CLEARING (t) = false;
4844 : return true;
4845 :
4846 1062885493 : default:
4847 : /* FIXME are we calling this too much? */
4848 1062885493 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4849 : }
4850 : }
4851 :
4852 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4853 : why and suggest what could be done about it. */
4854 :
4855 : static tree
4856 1012 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4857 : {
4858 1012 : bool ref_p = false;
4859 :
4860 : /* No need to look into types or unevaluated operands. */
4861 1012 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4862 : {
4863 0 : *walk_subtrees = false;
4864 0 : return NULL_TREE;
4865 : }
4866 :
4867 1012 : switch (TREE_CODE (*tp))
4868 : {
4869 86 : CASE_CONVERT:
4870 86 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4871 : break;
4872 67 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4873 67 : *tp = TREE_OPERAND (*tp, 0);
4874 199 : gcc_fallthrough ();
4875 199 : case ADDR_EXPR:
4876 199 : {
4877 199 : tree op = TREE_OPERAND (*tp, 0);
4878 199 : if (VAR_P (op)
4879 114 : && DECL_DECLARED_CONSTEXPR_P (op)
4880 39 : && !TREE_STATIC (op)
4881 : /* ??? We should also say something about temporaries. */
4882 235 : && !DECL_ARTIFICIAL (op))
4883 : {
4884 33 : if (ref_p)
4885 12 : inform (location_of (*tp), "reference to %qD is not a constant "
4886 : "expression", op);
4887 : else
4888 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4889 : "expression", op);
4890 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4891 33 : rich_location richloc (line_table, op_loc);
4892 33 : richloc.add_fixit_insert_before (op_loc, "static ");
4893 33 : inform (&richloc,
4894 : "address of non-static constexpr variable %qD may differ on "
4895 : "each invocation of the enclosing function; add %<static%> "
4896 : "to give it a constant address", op);
4897 33 : }
4898 : break;
4899 : }
4900 : default:
4901 : break;
4902 : }
4903 :
4904 : return NULL_TREE;
4905 : }
4906 :
4907 : /* Some expressions may have constant operands but are not constant
4908 : themselves, such as 1/0. Call this function to check for that
4909 : condition.
4910 :
4911 : We only call this in places that require an arithmetic constant, not in
4912 : places where we might have a non-constant expression that can be a
4913 : component of a constant expression, such as the address of a constexpr
4914 : variable that might be dereferenced later. */
4915 :
4916 : static bool
4917 588422839 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4918 : bool *overflow_p)
4919 : {
4920 578822396 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4921 590854458 : && t != void_node)
4922 : {
4923 2431204 : if (!allow_non_constant)
4924 : {
4925 263 : auto_diagnostic_group d;
4926 379 : error_at (cp_expr_loc_or_input_loc (t),
4927 : "%q+E is not a constant expression", t);
4928 263 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4929 : nullptr);
4930 263 : }
4931 2431204 : *non_constant_p = true;
4932 : }
4933 588422839 : if (TREE_OVERFLOW_P (t))
4934 : {
4935 678 : if (!allow_non_constant)
4936 : {
4937 155 : permerror (input_location, "overflow in constant expression");
4938 : /* If we're being permissive (and are in an enforcing
4939 : context), ignore the overflow. */
4940 155 : if (flag_permissive)
4941 75 : return *non_constant_p;
4942 : }
4943 603 : *overflow_p = true;
4944 : }
4945 588422764 : return *non_constant_p;
4946 : }
4947 :
4948 : /* Check whether the shift operation with code CODE and type TYPE on LHS
4949 : and RHS is undefined. If it is, give an error with an explanation,
4950 : and return true; return false otherwise. */
4951 :
4952 : static bool
4953 66829136 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4954 : enum tree_code code, tree type, tree lhs, tree rhs)
4955 : {
4956 66829136 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4957 4465015 : || TREE_CODE (lhs) != INTEGER_CST
4958 4464903 : || TREE_CODE (rhs) != INTEGER_CST)
4959 : return false;
4960 :
4961 4464903 : tree lhstype = TREE_TYPE (lhs);
4962 4464903 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4963 :
4964 : /* [expr.shift] The behavior is undefined if the right operand
4965 : is negative, or greater than or equal to the length in bits
4966 : of the promoted left operand. */
4967 4464903 : if (tree_int_cst_sgn (rhs) == -1)
4968 : {
4969 126 : if (!ctx->quiet)
4970 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4971 : build2_loc (loc, code, type, lhs, rhs));
4972 126 : return (!flag_permissive || ctx->quiet);
4973 : }
4974 4464777 : if (compare_tree_int (rhs, uprec) >= 0)
4975 : {
4976 200 : if (!ctx->quiet)
4977 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4978 : "than or equal to the precision %wu of the left operand",
4979 : build2_loc (loc, code, type, lhs, rhs), uprec);
4980 200 : return (!flag_permissive || ctx->quiet);
4981 : }
4982 :
4983 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4984 : if E1 has a signed type and non-negative value, and E1x2^E2 is
4985 : representable in the corresponding unsigned type of the result type,
4986 : then that value, converted to the result type, is the resulting value;
4987 : otherwise, the behavior is undefined.
4988 : For C++20:
4989 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4990 : 2^N, where N is the range exponent of the type of the result. */
4991 4464577 : if (code == LSHIFT_EXPR
4992 3585975 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4993 2053546 : && cxx_dialect >= cxx11
4994 6501109 : && cxx_dialect < cxx20)
4995 : {
4996 54034 : if (tree_int_cst_sgn (lhs) == -1)
4997 : {
4998 74 : if (!ctx->quiet)
4999 18 : permerror (loc,
5000 : "left operand of shift expression %q+E is negative",
5001 : build2_loc (loc, code, type, lhs, rhs));
5002 74 : return (!flag_permissive || ctx->quiet);
5003 : }
5004 : /* For signed x << y the following:
5005 : (unsigned) x >> ((prec (lhs) - 1) - y)
5006 : if > 1, is undefined. The right-hand side of this formula
5007 : is the highest bit of the LHS that can be set (starting from 0),
5008 : so that the shift doesn't overflow. We then right-shift the LHS
5009 : to see whether any other bit is set making the original shift
5010 : undefined -- the result is not representable in the corresponding
5011 : unsigned type. */
5012 53960 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
5013 53960 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
5014 53960 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
5015 53960 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
5016 53960 : if (tree_int_cst_lt (integer_one_node, t))
5017 : {
5018 41 : if (!ctx->quiet)
5019 7 : permerror (loc, "shift expression %q+E overflows",
5020 : build2_loc (loc, code, type, lhs, rhs));
5021 41 : return (!flag_permissive || ctx->quiet);
5022 : }
5023 : }
5024 : return false;
5025 : }
5026 :
5027 : /* Subroutine of cxx_eval_constant_expression.
5028 : Attempt to reduce the unary expression tree T to a compile time value.
5029 : If successful, return the value. Otherwise issue a diagnostic
5030 : and return error_mark_node. */
5031 :
5032 : static tree
5033 22513644 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
5034 : bool /*lval*/,
5035 : bool *non_constant_p, bool *overflow_p,
5036 : tree *jump_target)
5037 : {
5038 22513644 : tree r;
5039 22513644 : tree orig_arg = TREE_OPERAND (t, 0);
5040 22513644 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
5041 : non_constant_p, overflow_p,
5042 : jump_target);
5043 22513643 : if (*jump_target)
5044 : return NULL_TREE;
5045 22513641 : VERIFY_CONSTANT (arg);
5046 18932267 : location_t loc = EXPR_LOCATION (t);
5047 18932267 : enum tree_code code = TREE_CODE (t);
5048 18932267 : tree type = TREE_TYPE (t);
5049 18932267 : r = fold_unary_loc (loc, code, type, arg);
5050 18932267 : if (r == NULL_TREE)
5051 : {
5052 17 : if (arg == orig_arg)
5053 : r = t;
5054 : else
5055 13 : r = build1_loc (loc, code, type, arg);
5056 : }
5057 18932267 : VERIFY_CONSTANT (r);
5058 : return r;
5059 : }
5060 :
5061 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5062 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5063 : generic folding should be used. */
5064 :
5065 : static tree
5066 9441819 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5067 : tree lhs, tree rhs, bool *non_constant_p,
5068 : bool *overflow_p, tree *jump_target)
5069 : {
5070 9441819 : STRIP_NOPS (lhs);
5071 9441819 : if (TREE_CODE (lhs) != ADDR_EXPR)
5072 : return NULL_TREE;
5073 :
5074 8658429 : lhs = TREE_OPERAND (lhs, 0);
5075 :
5076 : /* &A[i] p+ j => &A[i + j] */
5077 8658429 : if (TREE_CODE (lhs) == ARRAY_REF
5078 194998 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5079 194998 : && TREE_CODE (rhs) == INTEGER_CST
5080 194998 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5081 8853427 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5082 : {
5083 194998 : tree orig_type = TREE_TYPE (t);
5084 194998 : location_t loc = EXPR_LOCATION (t);
5085 194998 : tree type = TREE_TYPE (lhs);
5086 :
5087 194998 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5088 194998 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5089 194998 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5090 : non_constant_p, overflow_p,
5091 : jump_target);
5092 194998 : if (*non_constant_p)
5093 : return NULL_TREE;
5094 194986 : if (*jump_target)
5095 : return NULL_TREE;
5096 : /* Don't fold an out-of-bound access. */
5097 194986 : if (!tree_int_cst_le (t, nelts))
5098 : return NULL_TREE;
5099 194986 : rhs = cp_fold_convert (ssizetype, rhs);
5100 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5101 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5102 194986 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5103 194986 : rhs, TYPE_SIZE_UNIT (type))))
5104 : return NULL_TREE;
5105 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5106 : as signed. */
5107 194954 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5108 194954 : TYPE_SIZE_UNIT (type));
5109 194954 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5110 194954 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5111 : t, NULL_TREE, NULL_TREE);
5112 194954 : t = cp_build_addr_expr (t, tf_warning_or_error);
5113 194954 : t = cp_fold_convert (orig_type, t);
5114 194954 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5115 : non_constant_p, overflow_p,
5116 194954 : jump_target);
5117 : }
5118 :
5119 : return NULL_TREE;
5120 : }
5121 :
5122 : /* Try to fold expressions like
5123 : (struct S *) (&a[0].D.2378 + 12)
5124 : into
5125 : &MEM <struct T> [(void *)&a + 12B]
5126 : This is something normally done by gimple_fold_stmt_to_constant_1
5127 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5128 : dereference the address because some details are lost.
5129 : For pointer comparisons we want such folding though so that
5130 : match.pd address_compare optimization works. */
5131 :
5132 : static tree
5133 10585740 : cxx_maybe_fold_addr_pointer_plus (tree t)
5134 : {
5135 21171486 : while (CONVERT_EXPR_P (t)
5136 11099004 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5137 513258 : t = TREE_OPERAND (t, 0);
5138 10585740 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5139 : return NULL_TREE;
5140 9425919 : tree op0 = TREE_OPERAND (t, 0);
5141 9425919 : tree op1 = TREE_OPERAND (t, 1);
5142 9425919 : if (TREE_CODE (op1) != INTEGER_CST)
5143 : return NULL_TREE;
5144 9425919 : while (CONVERT_EXPR_P (op0)
5145 18848438 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5146 9422519 : op0 = TREE_OPERAND (op0, 0);
5147 9425919 : if (TREE_CODE (op0) != ADDR_EXPR)
5148 : return NULL_TREE;
5149 9425919 : op1 = fold_convert (ptr_type_node, op1);
5150 9425919 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5151 9425919 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5152 : }
5153 :
5154 : /* Subroutine of cxx_eval_constant_expression.
5155 : Like cxx_eval_unary_expression, except for binary expressions. */
5156 :
5157 : static tree
5158 93212473 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5159 : value_cat lval,
5160 : bool *non_constant_p, bool *overflow_p,
5161 : tree *jump_target)
5162 : {
5163 93212473 : tree r = NULL_TREE;
5164 93212473 : tree orig_lhs = TREE_OPERAND (t, 0);
5165 93212473 : tree orig_rhs = TREE_OPERAND (t, 1);
5166 93212473 : tree lhs, rhs;
5167 93212473 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5168 : non_constant_p, overflow_p,
5169 : jump_target);
5170 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5171 : subtraction. */
5172 93212472 : if (*non_constant_p)
5173 : return t;
5174 75722842 : if (*jump_target)
5175 : return NULL_TREE;
5176 75722747 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5177 : non_constant_p, overflow_p,
5178 : jump_target);
5179 75722747 : if (*non_constant_p)
5180 : return t;
5181 74769621 : if (*jump_target)
5182 : return NULL_TREE;
5183 :
5184 74769615 : location_t loc = EXPR_LOCATION (t);
5185 74769615 : enum tree_code code = TREE_CODE (t);
5186 74769615 : tree type = TREE_TYPE (t);
5187 :
5188 74769615 : if (code == EQ_EXPR || code == NE_EXPR)
5189 : {
5190 16207781 : bool is_code_eq = (code == EQ_EXPR);
5191 :
5192 16207781 : if (TREE_CODE (lhs) == PTRMEM_CST
5193 199 : && TREE_CODE (rhs) == PTRMEM_CST)
5194 : {
5195 68 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5196 68 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5197 68 : bool eq;
5198 68 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5199 68 : && TREE_CODE (lmem) == FIELD_DECL
5200 68 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5201 95 : && same_type_p (DECL_CONTEXT (lmem),
5202 : DECL_CONTEXT (rmem)))
5203 : /* If both refer to (possibly different) members of the same union
5204 : (12.3), they compare equal. */
5205 : eq = true;
5206 : else
5207 41 : eq = cp_tree_equal (lhs, rhs);
5208 68 : r = constant_boolean_node (eq == is_code_eq, type);
5209 68 : }
5210 16207713 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5211 16207582 : || TREE_CODE (rhs) == PTRMEM_CST)
5212 16207713 : && (null_member_pointer_value_p (lhs)
5213 131 : || null_member_pointer_value_p (rhs)))
5214 131 : r = constant_boolean_node (!is_code_eq, type);
5215 16207582 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5216 0 : lhs = cplus_expand_constant (lhs);
5217 16207582 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5218 0 : rhs = cplus_expand_constant (rhs);
5219 16207582 : else if (REFLECT_EXPR_P (lhs) && REFLECT_EXPR_P (rhs))
5220 : {
5221 3904 : const bool eq = compare_reflections (lhs, rhs);
5222 3904 : r = constant_boolean_node (eq == is_code_eq, type);
5223 : }
5224 : }
5225 0 : if (r == NULL_TREE
5226 74765512 : && TREE_CODE_CLASS (code) == tcc_comparison
5227 32431465 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5228 : {
5229 5292870 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5230 4614825 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5231 5292870 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5232 4811094 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5233 : }
5234 9441974 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5235 84211589 : && integer_zerop (lhs) && !integer_zerop (rhs))
5236 : {
5237 155 : if (!ctx->quiet)
5238 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5239 155 : *non_constant_p = true;
5240 155 : return t;
5241 : }
5242 74769460 : else if (code == POINTER_PLUS_EXPR)
5243 : {
5244 9441819 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5245 : overflow_p, jump_target);
5246 9441819 : if (*jump_target)
5247 : return NULL_TREE;
5248 : }
5249 65327641 : else if (code == SPACESHIP_EXPR)
5250 : {
5251 25287 : r = genericize_spaceship (loc, type, lhs, rhs);
5252 25287 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5253 25287 : overflow_p, jump_target);
5254 : }
5255 :
5256 74744173 : if (r == NULL_TREE)
5257 : {
5258 74545116 : if (ctx->manifestly_const_eval == mce_true
5259 56528416 : && (flag_constexpr_fp_except
5260 56528413 : || TREE_CODE (type) != REAL_TYPE))
5261 : {
5262 56478676 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5263 56478676 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5264 56478676 : }
5265 : else
5266 18066440 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5267 : }
5268 :
5269 74545116 : if (r == NULL_TREE
5270 7915135 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5271 100 : && TREE_CODE (lhs) == INTEGER_CST
5272 98 : && TREE_CODE (rhs) == INTEGER_CST
5273 82659308 : && wi::neg_p (wi::to_wide (rhs)))
5274 : {
5275 : /* For diagnostics and -fpermissive emulate previous behavior of
5276 : handling shifts by negative amount. */
5277 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5278 98 : if (nrhs)
5279 119 : r = fold_binary_loc (loc,
5280 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5281 : type, lhs, nrhs);
5282 : }
5283 :
5284 74744173 : if (r == NULL_TREE)
5285 : {
5286 7915037 : if (lhs == orig_lhs && rhs == orig_rhs)
5287 : r = t;
5288 : else
5289 2675634 : r = build2_loc (loc, code, type, lhs, rhs);
5290 : }
5291 66829136 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5292 433 : *non_constant_p = true;
5293 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5294 : a local array in a constexpr function. */
5295 74744173 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5296 59531643 : if (!ptr)
5297 59531643 : VERIFY_CONSTANT (r);
5298 : return r;
5299 : }
5300 :
5301 : /* Subroutine of cxx_eval_constant_expression.
5302 : Attempt to evaluate condition expressions. */
5303 :
5304 : static tree
5305 27982189 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5306 : value_cat lval,
5307 : bool *non_constant_p, bool *overflow_p,
5308 : tree *jump_target)
5309 : {
5310 27982189 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5311 : vc_prvalue,
5312 : non_constant_p, overflow_p,
5313 : jump_target);
5314 27982189 : if (*jump_target)
5315 : return NULL_TREE;
5316 27982165 : VERIFY_CONSTANT (val);
5317 26473067 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5318 : {
5319 : /* Evaluate the condition as if it was
5320 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5321 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5322 : without manifestly_const_eval even expressions or parts thereof which
5323 : will later be manifestly const_eval evaluated), otherwise fold it to
5324 : true. */
5325 784724 : if (ctx->manifestly_const_eval == mce_unknown)
5326 : {
5327 719987 : *non_constant_p = true;
5328 719987 : return t;
5329 : }
5330 64737 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5331 : boolean_type_node);
5332 : }
5333 : /* Don't VERIFY_CONSTANT the other operands. */
5334 25753080 : const bool zero_p = integer_zerop (val);
5335 25753080 : if (zero_p)
5336 15537905 : val = TREE_OPERAND (t, 2);
5337 : else
5338 10215175 : val = TREE_OPERAND (t, 1);
5339 25753080 : if (TREE_CODE (t) == IF_STMT && !val)
5340 6197313 : val = void_node;
5341 :
5342 : /* P2564: If we aren't in immediate function context (including a manifestly
5343 : constant-evaluated expression), check any uses of immediate functions in
5344 : the arm we're discarding. But don't do this inside a call; we already
5345 : checked when parsing the function. */
5346 25753080 : if (ctx->manifestly_const_eval != mce_true
5347 3430157 : && !in_immediate_context ()
5348 3349319 : && !ctx->call
5349 26515396 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5350 762316 : ctx->manifestly_const_eval))
5351 : {
5352 7 : *non_constant_p = true;
5353 7 : return t;
5354 : }
5355 :
5356 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5357 : serve as the initializer for the same object as the outer TARGET_EXPR,
5358 : as in
5359 : A a = true ? A{} : A{};
5360 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5361 25753073 : if (TREE_CODE (val) == TARGET_EXPR)
5362 3298 : val = TARGET_EXPR_INITIAL (val);
5363 25753073 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5364 25753073 : overflow_p, jump_target);
5365 : }
5366 :
5367 : /* Subroutine of cxx_eval_constant_expression.
5368 : Attempt to evaluate vector condition expressions. Unlike
5369 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5370 : ternary arithmetics operation, where all 3 arguments have to be
5371 : evaluated as constants and then folding computes the result from
5372 : them. */
5373 :
5374 : static tree
5375 4582 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5376 : bool *non_constant_p, bool *overflow_p,
5377 : tree *jump_target)
5378 : {
5379 4582 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5380 : vc_prvalue,
5381 : non_constant_p, overflow_p,
5382 : jump_target);
5383 4582 : if (*jump_target)
5384 : return NULL_TREE;
5385 4582 : VERIFY_CONSTANT (arg1);
5386 3234 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5387 : vc_prvalue,
5388 : non_constant_p, overflow_p,
5389 : jump_target);
5390 3234 : if (*jump_target)
5391 : return NULL_TREE;
5392 3234 : VERIFY_CONSTANT (arg2);
5393 3167 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5394 : vc_prvalue,
5395 : non_constant_p, overflow_p,
5396 : jump_target);
5397 3167 : if (*jump_target)
5398 : return NULL_TREE;
5399 3167 : VERIFY_CONSTANT (arg3);
5400 3167 : location_t loc = EXPR_LOCATION (t);
5401 3167 : tree type = TREE_TYPE (t);
5402 3167 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5403 3167 : if (r == NULL_TREE)
5404 : {
5405 0 : if (arg1 == TREE_OPERAND (t, 0)
5406 0 : && arg2 == TREE_OPERAND (t, 1)
5407 0 : && arg3 == TREE_OPERAND (t, 2))
5408 : r = t;
5409 : else
5410 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5411 : }
5412 3167 : VERIFY_CONSTANT (r);
5413 : return r;
5414 : }
5415 :
5416 : /* Returns less than, equal to, or greater than zero if KEY is found to be
5417 : less than, to match, or to be greater than the constructor_elt's INDEX. */
5418 :
5419 : static int
5420 291546 : array_index_cmp (tree key, tree index)
5421 : {
5422 291546 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5423 :
5424 291546 : switch (TREE_CODE (index))
5425 : {
5426 288669 : case INTEGER_CST:
5427 288669 : return tree_int_cst_compare (key, index);
5428 2877 : case RANGE_EXPR:
5429 2877 : {
5430 2877 : tree lo = TREE_OPERAND (index, 0);
5431 2877 : tree hi = TREE_OPERAND (index, 1);
5432 2877 : if (tree_int_cst_lt (key, lo))
5433 : return -1;
5434 2591 : else if (tree_int_cst_lt (hi, key))
5435 : return 1;
5436 : else
5437 2591 : return 0;
5438 : }
5439 0 : default:
5440 0 : gcc_unreachable ();
5441 : }
5442 : }
5443 :
5444 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5445 : relative index OFF. */
5446 :
5447 : static tree
5448 29559 : raw_data_cst_elt (tree raw_data, unsigned int off)
5449 : {
5450 29559 : return build_int_cst (TREE_TYPE (raw_data),
5451 29559 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5452 59118 : ? (HOST_WIDE_INT)
5453 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
5454 : : (HOST_WIDE_INT)
5455 486 : RAW_DATA_SCHAR_ELT (raw_data, off));
5456 : }
5457 :
5458 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5459 : if none. If INSERT is true, insert a matching element rather than fail. */
5460 :
5461 : static HOST_WIDE_INT
5462 9802335 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5463 : {
5464 9802335 : if (tree_int_cst_sgn (dindex) < 0)
5465 : return -1;
5466 :
5467 9802335 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5468 9802335 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5469 9802335 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5470 :
5471 13485079 : unsigned HOST_WIDE_INT end = len;
5472 13485079 : unsigned HOST_WIDE_INT begin = 0;
5473 :
5474 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5475 : that the same is true of the other elements and index directly. */
5476 9467298 : if (end > 0)
5477 : {
5478 9342142 : tree cindex = (*elts)[end - 1].index;
5479 9342142 : if (cindex == NULL_TREE)
5480 : {
5481 : /* Verify that if the last index is missing, all indexes
5482 : are missing and there is no RAW_DATA_CST. */
5483 21021 : if (flag_checking)
5484 216718 : for (unsigned int j = 0; j < len - 1; ++j)
5485 195697 : gcc_assert ((*elts)[j].index == NULL_TREE
5486 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5487 21021 : if (i < end)
5488 21021 : return i;
5489 : else
5490 : {
5491 0 : begin = end;
5492 0 : if (i == end)
5493 : /* If the element is to be added right at the end,
5494 : make sure it is added with cleared index too. */
5495 4017781 : dindex = NULL_TREE;
5496 0 : else if (insert)
5497 : /* Otherwise, in order not to break the assumption
5498 : that CONSTRUCTOR either has all indexes or none,
5499 : we need to add indexes to all elements. */
5500 0 : for (unsigned int j = 0; j < len; ++j)
5501 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5502 : }
5503 : }
5504 9321121 : else if (TREE_CODE (cindex) == INTEGER_CST
5505 9321121 : && compare_tree_int (cindex, end - 1) == 0)
5506 : {
5507 9204384 : tree value = (*elts)[end - 1].value;
5508 9204384 : if (i < end)
5509 : {
5510 5763623 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5511 : begin = end - 1;
5512 : else
5513 5763533 : return i;
5514 : }
5515 3440761 : else if (TREE_CODE (value) == RAW_DATA_CST
5516 3472917 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5517 32156 : + RAW_DATA_LENGTH (value)))
5518 16078 : begin = end - 1;
5519 : else
5520 3424683 : begin = end;
5521 : }
5522 : }
5523 :
5524 : /* Otherwise, find a matching index by means of a binary search. */
5525 4141380 : while (begin != end)
5526 : {
5527 217344 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5528 217344 : constructor_elt &elt = (*elts)[middle];
5529 217344 : tree idx = elt.index;
5530 :
5531 217344 : int cmp = array_index_cmp (dindex, idx);
5532 217344 : if (cmp > 0
5533 61511 : && TREE_CODE (elt.value) == RAW_DATA_CST
5534 301152 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5535 83808 : + RAW_DATA_LENGTH (elt.value)))
5536 29417 : cmp = 0;
5537 217344 : if (cmp < 0)
5538 : end = middle;
5539 125839 : else if (cmp > 0)
5540 32094 : begin = middle + 1;
5541 : else
5542 : {
5543 93745 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5544 : {
5545 : /* We need to split the RAW_DATA_CST elt. */
5546 122 : constructor_elt e;
5547 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5548 244 : unsigned int off = (wi::to_offset (dindex)
5549 244 : - wi::to_offset (idx)).to_uhwi ();
5550 122 : tree value = elt.value;
5551 122 : unsigned int len = RAW_DATA_LENGTH (value);
5552 122 : if (off > 1 && len >= off + 3)
5553 22 : value = copy_node (elt.value);
5554 122 : if (off)
5555 : {
5556 33 : if (off > 1)
5557 33 : RAW_DATA_LENGTH (elt.value) = off;
5558 : else
5559 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5560 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5561 : build_int_cst (TREE_TYPE (elt.index),
5562 : off));
5563 33 : e.value = NULL_TREE;
5564 33 : ++middle;
5565 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5566 : }
5567 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5568 122 : if (len >= off + 2)
5569 : {
5570 111 : e.index = (*elts)[middle].index;
5571 111 : e.index = size_binop (PLUS_EXPR, e.index,
5572 : build_one_cst (TREE_TYPE (e.index)));
5573 111 : if (len >= off + 3)
5574 : {
5575 111 : RAW_DATA_LENGTH (value) -= off + 1;
5576 111 : RAW_DATA_POINTER (value) += off + 1;
5577 111 : e.value = value;
5578 : }
5579 : else
5580 0 : e.value = raw_data_cst_elt (value, off + 1);
5581 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5582 : }
5583 122 : return middle;
5584 : }
5585 18494 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5586 : {
5587 : /* We need to split the range. */
5588 343 : constructor_elt e;
5589 343 : tree lo = TREE_OPERAND (idx, 0);
5590 343 : tree hi = TREE_OPERAND (idx, 1);
5591 343 : tree value = elt.value;
5592 343 : dindex = fold_convert (sizetype, dindex);
5593 343 : if (tree_int_cst_lt (lo, dindex))
5594 : {
5595 : /* There are still some lower elts; shorten the range. */
5596 170 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5597 85 : size_one_node);
5598 85 : if (tree_int_cst_equal (lo, new_hi))
5599 : /* Only one element left, no longer a range. */
5600 33 : elt.index = lo;
5601 : else
5602 52 : TREE_OPERAND (idx, 1) = new_hi;
5603 : /* Append the element we want to insert. */
5604 85 : ++middle;
5605 85 : e.index = dindex;
5606 85 : e.value = unshare_constructor (value);
5607 85 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5608 : }
5609 : else
5610 : /* No lower elts, the range elt is now ours. */
5611 258 : elt.index = dindex;
5612 :
5613 343 : if (tree_int_cst_lt (dindex, hi))
5614 : {
5615 : /* There are still some higher elts; append a range. */
5616 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5617 239 : size_one_node);
5618 239 : if (tree_int_cst_equal (new_lo, hi))
5619 98 : e.index = hi;
5620 : else
5621 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5622 239 : e.value = unshare_constructor (value);
5623 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5624 : }
5625 : }
5626 93623 : return middle;
5627 : }
5628 : }
5629 :
5630 3924036 : if (insert)
5631 : {
5632 3857758 : constructor_elt e = { dindex, NULL_TREE };
5633 3857758 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5634 3857758 : return end;
5635 : }
5636 :
5637 : return -1;
5638 : }
5639 :
5640 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5641 : matching constructor_elt exists, then add one to CTOR.
5642 :
5643 : As an optimization, if POS_HINT is non-negative then it is used as a guess
5644 : for the (integer) index of the matching constructor_elt within CTOR.
5645 :
5646 : If POS_HINT is -2, it means do not insert. */
5647 :
5648 : static constructor_elt *
5649 30712405 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5650 : {
5651 : /* Check the hint first. */
5652 2658805 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5653 33371210 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5654 : return CONSTRUCTOR_ELT (ctor, pos_hint);
5655 :
5656 28053650 : bool insertp = (pos_hint != -2);
5657 28053650 : tree type = TREE_TYPE (ctor);
5658 28053650 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5659 : {
5660 11226 : gcc_assert (insertp);
5661 11226 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5662 11226 : return &CONSTRUCTOR_ELTS (ctor)->last();
5663 : }
5664 28042424 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5665 : {
5666 5220455 : if (TREE_CODE (index) == RANGE_EXPR)
5667 : {
5668 : /* Support for RANGE_EXPR index lookups is currently limited to
5669 : accessing an existing element via POS_HINT, or appending a new
5670 : element to the end of CTOR. ??? Support for other access
5671 : patterns may also be needed. */
5672 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5673 3 : if (vec_safe_length (elts))
5674 : {
5675 3 : tree lo = TREE_OPERAND (index, 0);
5676 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5677 : }
5678 3 : gcc_assert (insertp);
5679 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5680 3 : return &elts->last();
5681 : }
5682 :
5683 5220452 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5684 5220452 : if (i < 0)
5685 : {
5686 969 : gcc_assert (!insertp);
5687 : return nullptr;
5688 : }
5689 5219483 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5690 5219483 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5691 : {
5692 : /* Split a range even if we aren't inserting new entries. */
5693 0 : gcc_assert (!insertp);
5694 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5695 0 : gcc_assert (i >= 0);
5696 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5697 : }
5698 5219483 : gcc_assert (cep->index == NULL_TREE
5699 : || TREE_CODE (cep->index) != RANGE_EXPR);
5700 : return cep;
5701 : }
5702 : else
5703 : {
5704 22821969 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5705 : && (same_type_ignoring_top_level_qualifiers_p
5706 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5707 :
5708 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5709 : Usually we meet initializers in that order, but it is
5710 : possible for base types to be placed not in program
5711 : order. */
5712 22821969 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5713 22821969 : unsigned HOST_WIDE_INT idx = 0;
5714 22821969 : constructor_elt *cep = NULL;
5715 :
5716 : /* Check if we're changing the active member of a union. */
5717 501002 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5718 23148218 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5719 9793 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5720 : /* If the bit offset of INDEX is larger than that of the last
5721 : constructor_elt, then we can just immediately append a new
5722 : constructor_elt to the end of CTOR. */
5723 22812176 : else if (CONSTRUCTOR_NELTS (ctor)
5724 31764836 : && tree_int_cst_compare (bit_position (index),
5725 31218990 : bit_position (CONSTRUCTOR_ELTS (ctor)
5726 15609495 : ->last().index)) > 0)
5727 : {
5728 4034349 : idx = CONSTRUCTOR_NELTS (ctor);
5729 4034349 : goto insert;
5730 : }
5731 :
5732 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5733 : appropriately. */
5734 :
5735 22832118 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5736 4044498 : idx++, fields = DECL_CHAIN (fields))
5737 : {
5738 15619644 : if (index == cep->index)
5739 11537208 : goto found;
5740 :
5741 : /* The field we're initializing must be on the field
5742 : list. Look to see if it is present before the
5743 : field the current ELT initializes. */
5744 46124856 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5745 42080358 : if (index == fields)
5746 37938 : goto insert;
5747 : }
5748 : /* We fell off the end of the CONSTRUCTOR, so insert a new
5749 : entry at the end. */
5750 :
5751 11284761 : insert:
5752 11284761 : if (!insertp)
5753 : return nullptr;
5754 :
5755 11284758 : {
5756 11284758 : constructor_elt ce = { index, NULL_TREE };
5757 :
5758 11284758 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5759 11284758 : cep = CONSTRUCTOR_ELT (ctor, idx);
5760 : }
5761 22821966 : found:;
5762 :
5763 22821966 : return cep;
5764 : }
5765 : }
5766 :
5767 : /* Under the control of CTX, issue a detailed diagnostic for
5768 : an out-of-bounds subscript INDEX into the expression ARRAY. */
5769 :
5770 : static void
5771 574 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5772 : {
5773 574 : if (!ctx->quiet)
5774 : {
5775 117 : tree arraytype = TREE_TYPE (array);
5776 :
5777 : /* Convert the unsigned array subscript to a signed integer to avoid
5778 : printing huge numbers for small negative values. */
5779 117 : tree sidx = fold_convert (ssizetype, index);
5780 117 : STRIP_ANY_LOCATION_WRAPPER (array);
5781 117 : if (DECL_P (array))
5782 : {
5783 76 : auto_diagnostic_group d;
5784 76 : if (TYPE_DOMAIN (arraytype))
5785 70 : error_at (loc, "array subscript value %qE is outside the bounds "
5786 : "of array %qD of type %qT", sidx, array, arraytype);
5787 : else
5788 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5789 : "type %qT with unknown bounds", sidx, array, arraytype);
5790 76 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5791 76 : }
5792 41 : else if (TYPE_DOMAIN (arraytype))
5793 38 : error_at (loc, "array subscript value %qE is outside the bounds "
5794 : "of array type %qT", sidx, arraytype);
5795 : else
5796 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5797 : "with unknown bounds", sidx, arraytype);
5798 : }
5799 574 : }
5800 :
5801 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5802 : a VECTOR_TYPE). */
5803 :
5804 : static tree
5805 16835163 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5806 : bool *non_constant_p, bool *overflow_p,
5807 : tree *jump_target)
5808 : {
5809 16835163 : tree nelts;
5810 16835163 : if (TREE_CODE (type) == ARRAY_TYPE)
5811 : {
5812 16835163 : if (TYPE_DOMAIN (type))
5813 16834915 : nelts = array_type_nelts_top (type);
5814 : else
5815 248 : nelts = size_zero_node;
5816 : }
5817 0 : else if (VECTOR_TYPE_P (type))
5818 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5819 : else
5820 0 : gcc_unreachable ();
5821 :
5822 : /* For VLAs, the number of elements won't be an integer constant. */
5823 16835163 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5824 : non_constant_p, overflow_p,
5825 : jump_target);
5826 16835163 : return nelts;
5827 : }
5828 :
5829 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5830 : STRING_CST STRING. */
5831 :
5832 : static tree
5833 1841733 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5834 : {
5835 1841733 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5836 1841733 : tree r;
5837 :
5838 1841733 : if (chars_per_elt == 1)
5839 1650846 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5840 : else
5841 : {
5842 190887 : const unsigned char *ptr
5843 190887 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5844 190887 : + index * chars_per_elt);
5845 190887 : r = native_interpret_expr (type, ptr, chars_per_elt);
5846 : }
5847 1841733 : return r;
5848 : }
5849 :
5850 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5851 : subscript, diagnose any problems with it, and return the result. */
5852 :
5853 : static tree
5854 17027123 : eval_and_check_array_index (const constexpr_ctx *ctx,
5855 : tree t, bool allow_one_past,
5856 : bool *non_constant_p, bool *overflow_p,
5857 : tree *jump_target)
5858 : {
5859 17027123 : location_t loc = cp_expr_loc_or_input_loc (t);
5860 17027123 : tree ary = TREE_OPERAND (t, 0);
5861 17027123 : t = TREE_OPERAND (t, 1);
5862 17027123 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5863 : non_constant_p, overflow_p,
5864 : jump_target);
5865 17027123 : if (*jump_target)
5866 : return NULL_TREE;
5867 17027123 : VERIFY_CONSTANT (index);
5868 :
5869 16834610 : if (!tree_fits_shwi_p (index)
5870 16834610 : || tree_int_cst_sgn (index) < 0)
5871 : {
5872 112 : diag_array_subscript (loc, ctx, ary, index);
5873 112 : *non_constant_p = true;
5874 112 : return t;
5875 : }
5876 :
5877 16834498 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5878 : overflow_p, jump_target);
5879 16834498 : if (*jump_target)
5880 : return NULL_TREE;
5881 16834498 : VERIFY_CONSTANT (nelts);
5882 16834435 : if (allow_one_past
5883 16834435 : ? !tree_int_cst_le (index, nelts)
5884 11459056 : : !tree_int_cst_lt (index, nelts))
5885 : {
5886 462 : diag_array_subscript (loc, ctx, ary, index);
5887 462 : *non_constant_p = true;
5888 462 : return t;
5889 : }
5890 :
5891 : return index;
5892 : }
5893 :
5894 : /* Subroutine of cxx_eval_constant_expression.
5895 : Attempt to reduce a reference to an array slot. */
5896 :
5897 : static tree
5898 12228363 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5899 : value_cat lval,
5900 : bool *non_constant_p, bool *overflow_p,
5901 : tree *jump_target)
5902 : {
5903 12228363 : tree oldary = TREE_OPERAND (t, 0);
5904 12228363 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5905 : lval,
5906 : non_constant_p, overflow_p,
5907 : jump_target);
5908 12228363 : if (*non_constant_p)
5909 : return t;
5910 12030140 : if (*jump_target)
5911 : return NULL_TREE;
5912 12030140 : if (!lval
5913 6653426 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5914 54095 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5915 12084235 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5916 54095 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5917 54095 : ary = TREE_OPERAND (ary, 0);
5918 :
5919 12030140 : tree oldidx = TREE_OPERAND (t, 1);
5920 12030140 : tree index = eval_and_check_array_index (ctx, t, lval,
5921 : non_constant_p, overflow_p,
5922 : jump_target);
5923 12030140 : if (*non_constant_p)
5924 : return t;
5925 11837059 : if (*jump_target)
5926 : return NULL_TREE;
5927 :
5928 11837059 : if (lval && ary == oldary && index == oldidx)
5929 : return t;
5930 7494048 : else if (lval == vc_discard)
5931 : return t;
5932 7494048 : else if (lval)
5933 1032128 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5934 :
5935 6461920 : unsigned len = 0, elem_nchars = 1;
5936 6461920 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5937 6461920 : if (TREE_CODE (ary) == CONSTRUCTOR)
5938 4576785 : len = CONSTRUCTOR_NELTS (ary);
5939 1885135 : else if (TREE_CODE (ary) == STRING_CST)
5940 : {
5941 1831048 : elem_nchars = (TYPE_PRECISION (elem_type)
5942 1831048 : / TYPE_PRECISION (char_type_node));
5943 1831048 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5944 : }
5945 54087 : else if (TREE_CODE (ary) == VECTOR_CST)
5946 : /* We don't create variable-length VECTOR_CSTs. */
5947 54087 : len = VECTOR_CST_NELTS (ary).to_constant ();
5948 : else
5949 : {
5950 : /* We can't do anything with other tree codes, so use
5951 : VERIFY_CONSTANT to complain and fail. */
5952 0 : VERIFY_CONSTANT (ary);
5953 0 : gcc_unreachable ();
5954 : }
5955 :
5956 6461920 : bool found;
5957 6461920 : HOST_WIDE_INT i = 0;
5958 6461920 : if (TREE_CODE (ary) == CONSTRUCTOR)
5959 : {
5960 4576785 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5961 4576785 : found = (ix >= 0);
5962 4576785 : if (found)
5963 4511564 : i = ix;
5964 : }
5965 : else
5966 : {
5967 1885135 : i = tree_to_shwi (index);
5968 1885135 : found = (i < len);
5969 : }
5970 :
5971 6461920 : if (found)
5972 : {
5973 6396248 : tree r;
5974 6396248 : if (TREE_CODE (ary) == CONSTRUCTOR)
5975 : {
5976 4511564 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5977 4511564 : if (TREE_CODE (r) == RAW_DATA_CST)
5978 : {
5979 29437 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5980 29437 : gcc_checking_assert (ridx);
5981 29437 : unsigned int off
5982 29437 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5983 29437 : r = raw_data_cst_elt (r, off);
5984 : }
5985 : }
5986 1884684 : else if (TREE_CODE (ary) == VECTOR_CST)
5987 54087 : r = VECTOR_CST_ELT (ary, i);
5988 : else
5989 1830597 : r = extract_string_elt (ary, elem_nchars, i);
5990 :
5991 1914121 : if (r)
5992 : /* Don't VERIFY_CONSTANT here. */
5993 6396248 : return r;
5994 :
5995 : /* Otherwise the element doesn't have a value yet. */
5996 : }
5997 :
5998 : /* Not found. */
5999 :
6000 65672 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
6001 90 : return build_constructor (elem_type, NULL);
6002 :
6003 65582 : if (TREE_CODE (ary) == CONSTRUCTOR
6004 65582 : && CONSTRUCTOR_NO_CLEARING (ary))
6005 : {
6006 : /* 'ary' is part of the aggregate initializer we're currently
6007 : building; if there's no initializer for this element yet,
6008 : that's an error. */
6009 51 : if (!ctx->quiet)
6010 16 : error ("accessing uninitialized array element");
6011 51 : *non_constant_p = true;
6012 51 : return t;
6013 : }
6014 :
6015 : /* If it's within the array bounds but doesn't have an explicit
6016 : initializer, it's initialized from {}. But use build_value_init
6017 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
6018 65531 : tree val;
6019 65531 : constexpr_ctx new_ctx;
6020 65531 : if (CP_AGGREGATE_TYPE_P (elem_type))
6021 : {
6022 500 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
6023 500 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
6024 : }
6025 : else
6026 65031 : val = build_value_init (elem_type, tf_warning_or_error);
6027 :
6028 : /* Create a new constructor only if we don't already have a suitable one. */
6029 65531 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
6030 66063 : && (!ctx->ctor
6031 45 : || !same_type_ignoring_top_level_qualifiers_p
6032 45 : (elem_type, TREE_TYPE (ctx->ctor))));
6033 523 : if (new_ctor)
6034 : {
6035 523 : new_ctx = *ctx;
6036 : /* We clear the object here. We used to replace it with T, but that
6037 : caused problems (101371, 108158); and anyway, T is the initializer,
6038 : not the target object. */
6039 523 : new_ctx.object = NULL_TREE;
6040 523 : new_ctx.ctor = build_constructor (elem_type, NULL);
6041 523 : ctx = &new_ctx;
6042 : }
6043 65531 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6044 : overflow_p, jump_target);
6045 65531 : if (new_ctor && t != ctx->ctor)
6046 502 : free_constructor (ctx->ctor);
6047 : return t;
6048 : }
6049 :
6050 : /* Subroutine of cxx_eval_constant_expression.
6051 : Attempt to reduce a field access of a value of class type. */
6052 :
6053 : static tree
6054 78828415 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
6055 : value_cat lval,
6056 : bool *non_constant_p, bool *overflow_p,
6057 : tree *jump_target)
6058 : {
6059 78828415 : unsigned HOST_WIDE_INT i;
6060 78828415 : tree field;
6061 78828415 : tree value;
6062 78828415 : tree part = TREE_OPERAND (t, 1);
6063 78828415 : tree orig_whole = TREE_OPERAND (t, 0);
6064 78828415 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6065 : lval,
6066 : non_constant_p, overflow_p,
6067 : jump_target);
6068 78828415 : if (*non_constant_p)
6069 : return t;
6070 58837158 : if (*jump_target)
6071 : return NULL_TREE;
6072 58837152 : if (INDIRECT_REF_P (whole)
6073 58837152 : && integer_zerop (TREE_OPERAND (whole, 0)))
6074 : {
6075 177 : if (!ctx->quiet)
6076 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6077 177 : *non_constant_p = true;
6078 177 : return t;
6079 : }
6080 :
6081 58836975 : if (TREE_CODE (whole) == PTRMEM_CST)
6082 1506 : whole = cplus_expand_constant (whole);
6083 58836975 : if (whole == orig_whole)
6084 : return t;
6085 38715168 : if (lval == vc_discard)
6086 : return t;
6087 38715144 : if (lval)
6088 14258142 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6089 : whole, part, NULL_TREE);
6090 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6091 : CONSTRUCTOR. */
6092 24457002 : if (TREE_CODE (whole) != CONSTRUCTOR)
6093 : {
6094 0 : if (!ctx->quiet)
6095 0 : error ("%qE is not a constant expression", orig_whole);
6096 0 : *non_constant_p = true;
6097 0 : return t;
6098 : }
6099 24455510 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6100 24458676 : && DECL_MUTABLE_P (part))
6101 : {
6102 144 : if (!ctx->quiet)
6103 16 : error ("mutable %qD is not usable in a constant expression", part);
6104 144 : *non_constant_p = true;
6105 144 : return t;
6106 : }
6107 :
6108 24456858 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6109 36680123 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6110 : {
6111 : /* Use name match for PMF fields, as a variant will have a
6112 : different FIELD_DECL with a different type. */
6113 36581773 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6114 : : field == part)
6115 : {
6116 24358508 : if (value == void_node)
6117 6 : goto uninit;
6118 24358502 : if (value)
6119 : {
6120 24358484 : STRIP_ANY_LOCATION_WRAPPER (value);
6121 24358484 : return value;
6122 : }
6123 : else
6124 : /* We're in the middle of initializing it. */
6125 : break;
6126 : }
6127 : }
6128 98368 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6129 : {
6130 109 : if (CONSTRUCTOR_NELTS (whole) > 0)
6131 : {
6132 : /* DR 1188 says we don't have to deal with this. */
6133 94 : if (!ctx->quiet)
6134 : {
6135 20 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6136 20 : if (cep->value == NULL_TREE)
6137 9 : error ("accessing uninitialized member %qD", part);
6138 : else
6139 11 : error ("accessing %qD member instead of active %qD member "
6140 : "in constant expression", part, cep->index);
6141 : }
6142 94 : *non_constant_p = true;
6143 94 : return t;
6144 : }
6145 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6146 : {
6147 : /* Value-initialized union, check if looking at the first member. */
6148 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6149 12 : if (first != part)
6150 : {
6151 9 : if (!ctx->quiet)
6152 3 : error ("accessing %qD member instead of initialized %qD "
6153 : "member in constant expression", part, first);
6154 9 : *non_constant_p = true;
6155 9 : return t;
6156 : }
6157 : }
6158 : }
6159 :
6160 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6161 : classes never get represented; throw together a value now. */
6162 98265 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6163 89660 : return build_constructor (TREE_TYPE (t), NULL);
6164 :
6165 8605 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6166 :
6167 8605 : if (CONSTRUCTOR_NO_CLEARING (whole))
6168 : {
6169 101 : uninit:
6170 : /* 'whole' is part of the aggregate initializer we're currently
6171 : building; if there's no initializer for this member yet, that's an
6172 : error. */
6173 107 : if (!ctx->quiet)
6174 23 : error ("accessing uninitialized member %qD", part);
6175 107 : *non_constant_p = true;
6176 107 : return t;
6177 : }
6178 :
6179 : /* If there's no explicit init for this field, it's value-initialized. */
6180 :
6181 8504 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6182 : {
6183 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6184 : and copy the flags. */
6185 7989 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6186 7989 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6187 7989 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6188 7989 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6189 7989 : return value;
6190 : }
6191 :
6192 515 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6193 515 : return cxx_eval_constant_expression (ctx, value,
6194 : lval,
6195 : non_constant_p, overflow_p,
6196 515 : jump_target);
6197 : }
6198 :
6199 : /* Subroutine of cxx_eval_constant_expression.
6200 : Attempt to reduce a field access of a value of class type that is
6201 : expressed as a BIT_FIELD_REF. */
6202 :
6203 : static tree
6204 15766 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6205 : value_cat lval,
6206 : bool *non_constant_p, bool *overflow_p,
6207 : tree *jump_target)
6208 : {
6209 15766 : tree orig_whole = TREE_OPERAND (t, 0);
6210 15766 : tree retval, fldval, utype, mask;
6211 15766 : bool fld_seen = false;
6212 15766 : HOST_WIDE_INT istart, isize;
6213 15766 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6214 : lval,
6215 : non_constant_p, overflow_p,
6216 : jump_target);
6217 15766 : tree start, field, value;
6218 15766 : unsigned HOST_WIDE_INT i;
6219 :
6220 15766 : if (*jump_target)
6221 : return NULL_TREE;
6222 15766 : if (whole == orig_whole)
6223 : return t;
6224 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6225 : CONSTRUCTOR. */
6226 15574 : if (!*non_constant_p
6227 15574 : && TREE_CODE (whole) != VECTOR_CST
6228 1495 : && TREE_CODE (whole) != CONSTRUCTOR)
6229 : {
6230 0 : if (!ctx->quiet)
6231 0 : error ("%qE is not a constant expression", orig_whole);
6232 0 : *non_constant_p = true;
6233 : }
6234 15574 : if (*non_constant_p)
6235 : return t;
6236 :
6237 15574 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6238 : {
6239 14079 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6240 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6241 : return r;
6242 0 : if (!ctx->quiet)
6243 0 : error ("%qE is not a constant expression", orig_whole);
6244 0 : *non_constant_p = true;
6245 0 : return t;
6246 : }
6247 :
6248 1495 : start = TREE_OPERAND (t, 2);
6249 1495 : istart = tree_to_shwi (start);
6250 1495 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6251 1495 : utype = TREE_TYPE (t);
6252 1495 : if (!TYPE_UNSIGNED (utype))
6253 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6254 1495 : retval = build_int_cst (utype, 0);
6255 20930 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6256 : {
6257 19435 : tree bitpos = bit_position (field);
6258 19435 : STRIP_ANY_LOCATION_WRAPPER (value);
6259 19435 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6260 : return value;
6261 19435 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6262 19435 : && TREE_CODE (value) == INTEGER_CST
6263 19435 : && tree_fits_shwi_p (bitpos)
6264 38870 : && tree_fits_shwi_p (DECL_SIZE (field)))
6265 : {
6266 19435 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6267 19435 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6268 19435 : HOST_WIDE_INT shift;
6269 19435 : if (bit >= istart && bit + sz <= istart + isize)
6270 : {
6271 4485 : fldval = fold_convert (utype, value);
6272 4485 : mask = build_int_cst_type (utype, -1);
6273 4485 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6274 : size_int (TYPE_PRECISION (utype) - sz));
6275 4485 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6276 : size_int (TYPE_PRECISION (utype) - sz));
6277 4485 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6278 4485 : shift = bit - istart;
6279 4485 : if (BYTES_BIG_ENDIAN)
6280 : shift = TYPE_PRECISION (utype) - shift - sz;
6281 4485 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6282 : size_int (shift));
6283 4485 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6284 4485 : fld_seen = true;
6285 : }
6286 : }
6287 : }
6288 1495 : if (fld_seen)
6289 1495 : return fold_convert (TREE_TYPE (t), retval);
6290 0 : gcc_unreachable ();
6291 : return error_mark_node;
6292 : }
6293 :
6294 : /* Helper for cxx_eval_bit_cast.
6295 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6296 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6297 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6298 : data members of reference type. */
6299 :
6300 : static bool
6301 5538 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6302 : tree orig_type)
6303 : {
6304 6061 : if (TREE_CODE (type) == UNION_TYPE)
6305 : {
6306 63 : if (!ctx->quiet)
6307 : {
6308 21 : if (type == orig_type)
6309 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6310 : "a union type", "__builtin_bit_cast", type);
6311 : else
6312 15 : error_at (loc, "%qs is not a constant expression because %qT "
6313 : "contains a union type", "__builtin_bit_cast",
6314 : orig_type);
6315 : }
6316 63 : return true;
6317 : }
6318 : if (TREE_CODE (type) == POINTER_TYPE)
6319 : {
6320 57 : if (!ctx->quiet)
6321 : {
6322 18 : if (type == orig_type)
6323 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6324 : "a pointer type", "__builtin_bit_cast", type);
6325 : else
6326 12 : error_at (loc, "%qs is not a constant expression because %qT "
6327 : "contains a pointer type", "__builtin_bit_cast",
6328 : orig_type);
6329 : }
6330 57 : return true;
6331 : }
6332 : if (TREE_CODE (type) == REFERENCE_TYPE)
6333 : {
6334 0 : if (!ctx->quiet)
6335 : {
6336 0 : if (type == orig_type)
6337 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6338 : "a reference type", "__builtin_bit_cast", type);
6339 : else
6340 0 : error_at (loc, "%qs is not a constant expression because %qT "
6341 : "contains a reference type", "__builtin_bit_cast",
6342 : orig_type);
6343 : }
6344 0 : return true;
6345 : }
6346 1887 : if (TYPE_PTRMEM_P (type))
6347 : {
6348 36 : if (!ctx->quiet)
6349 : {
6350 12 : if (type == orig_type)
6351 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6352 : "a pointer to member type", "__builtin_bit_cast",
6353 : type);
6354 : else
6355 0 : error_at (loc, "%qs is not a constant expression because %qT "
6356 : "contains a pointer to member type",
6357 : "__builtin_bit_cast", orig_type);
6358 : }
6359 36 : return true;
6360 : }
6361 5905 : if (TYPE_VOLATILE (type))
6362 : {
6363 0 : if (!ctx->quiet)
6364 : {
6365 0 : if (type == orig_type)
6366 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6367 : "volatile", "__builtin_bit_cast", type);
6368 : else
6369 0 : error_at (loc, "%qs is not a constant expression because %qT "
6370 : "contains a volatile subobject",
6371 : "__builtin_bit_cast", orig_type);
6372 : }
6373 0 : return true;
6374 : }
6375 5905 : if (TREE_CODE (type) == RECORD_TYPE)
6376 58406 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6377 56627 : if (TREE_CODE (field) == FIELD_DECL
6378 56627 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6379 : return true;
6380 5815 : if (TREE_CODE (type) == ARRAY_TYPE)
6381 523 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6382 : return false;
6383 : }
6384 :
6385 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6386 : std::byte members of CONSTRUCTOR (recursively) if they contain
6387 : some indeterminate bits (as set in MASK), remove the ctor elts,
6388 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6389 : bits in MASK. */
6390 :
6391 : static void
6392 707 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6393 : {
6394 707 : if (TREE_CODE (t) != CONSTRUCTOR)
6395 : return;
6396 :
6397 : unsigned i, j = 0;
6398 : tree index, value;
6399 2284 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6400 : {
6401 1577 : tree type = TREE_TYPE (value);
6402 1577 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6403 2715 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6404 : {
6405 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6406 : {
6407 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6408 135 : gcc_assert (fldsz != 0);
6409 135 : HOST_WIDE_INT pos = int_byte_position (index);
6410 135 : HOST_WIDE_INT bpos
6411 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6412 135 : bpos %= BITS_PER_UNIT;
6413 135 : HOST_WIDE_INT end
6414 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6415 135 : gcc_assert (end == 1 || end == 2);
6416 135 : unsigned char *p = mask + pos;
6417 135 : unsigned char mask_save[2];
6418 135 : mask_save[0] = mask[pos];
6419 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6420 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6421 : sorry_at (loc, "PDP11 bit-field handling unsupported"
6422 : " in %qs", "__builtin_bit_cast");
6423 135 : else if (BYTES_BIG_ENDIAN)
6424 : {
6425 : /* Big endian. */
6426 : if (bpos + fldsz <= BITS_PER_UNIT)
6427 : *p &= ~(((1 << fldsz) - 1)
6428 : << (BITS_PER_UNIT - bpos - fldsz));
6429 : else
6430 : {
6431 : gcc_assert (bpos);
6432 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6433 : p++;
6434 : fldsz -= BITS_PER_UNIT - bpos;
6435 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6436 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6437 : }
6438 : }
6439 : else
6440 : {
6441 : /* Little endian. */
6442 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6443 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6444 : else
6445 : {
6446 0 : gcc_assert (bpos);
6447 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6448 0 : p++;
6449 0 : fldsz -= BITS_PER_UNIT - bpos;
6450 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6451 0 : *p &= ~((1 << fldsz) - 1);
6452 : }
6453 : }
6454 135 : if (mask_save[0] != mask[pos]
6455 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6456 : {
6457 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6458 36 : continue;
6459 : }
6460 : }
6461 : }
6462 1307 : else if (is_byte_access_type_not_plain_char (type))
6463 : {
6464 228 : HOST_WIDE_INT pos;
6465 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6466 132 : pos = tree_to_shwi (index);
6467 : else
6468 96 : pos = int_byte_position (index);
6469 228 : if (mask[pos])
6470 : {
6471 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6472 48 : mask[pos] = 0;
6473 48 : continue;
6474 : }
6475 : }
6476 1493 : if (TREE_CODE (value) == CONSTRUCTOR)
6477 : {
6478 368 : HOST_WIDE_INT pos;
6479 368 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6480 144 : pos = tree_to_shwi (index)
6481 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6482 : else
6483 296 : pos = int_byte_position (index);
6484 368 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6485 : }
6486 1493 : if (i != j)
6487 : {
6488 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6489 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6490 : }
6491 1493 : ++j;
6492 : }
6493 1414 : if (CONSTRUCTOR_NELTS (t) != j)
6494 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6495 : }
6496 :
6497 : /* Subroutine of cxx_eval_constant_expression.
6498 : Attempt to evaluate a BIT_CAST_EXPR. */
6499 :
6500 : static tree
6501 1125 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6502 : bool *overflow_p, tree *jump_target)
6503 : {
6504 1125 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6505 1125 : TREE_TYPE (t))
6506 3652 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6507 1044 : EXPR_LOCATION (t)),
6508 1044 : TREE_TYPE (TREE_OPERAND (t, 0)),
6509 1044 : TREE_TYPE (TREE_OPERAND (t, 0))))
6510 : {
6511 156 : *non_constant_p = true;
6512 156 : return t;
6513 : }
6514 :
6515 969 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6516 : non_constant_p, overflow_p,
6517 : jump_target);
6518 969 : if (*non_constant_p)
6519 : return t;
6520 708 : if (*jump_target)
6521 : return NULL_TREE;
6522 :
6523 708 : location_t loc = EXPR_LOCATION (t);
6524 708 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6525 : {
6526 : if (!ctx->quiet)
6527 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6528 : "__builtin_bit_cast");
6529 : *non_constant_p = true;
6530 : return t;
6531 : }
6532 :
6533 708 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6534 : {
6535 0 : if (!ctx->quiet)
6536 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6537 : "type is too large", "__builtin_bit_cast");
6538 0 : *non_constant_p = true;
6539 0 : return t;
6540 : }
6541 :
6542 708 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6543 708 : if (len < 0 || (int) len != len)
6544 : {
6545 0 : if (!ctx->quiet)
6546 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6547 : "type is too large", "__builtin_bit_cast");
6548 0 : *non_constant_p = true;
6549 0 : return t;
6550 : }
6551 :
6552 708 : unsigned char buf[64];
6553 708 : unsigned char *ptr, *mask;
6554 708 : size_t alen = (size_t) len * 2;
6555 708 : if (alen <= sizeof (buf))
6556 : ptr = buf;
6557 : else
6558 3 : ptr = XNEWVEC (unsigned char, alen);
6559 708 : mask = ptr + (size_t) len;
6560 : /* At the beginning consider everything indeterminate. */
6561 708 : memset (mask, ~0, (size_t) len);
6562 :
6563 708 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6564 : {
6565 0 : if (!ctx->quiet)
6566 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6567 : "argument cannot be encoded", "__builtin_bit_cast");
6568 0 : *non_constant_p = true;
6569 0 : if (ptr != buf)
6570 0 : XDELETE (ptr);
6571 0 : return t;
6572 : }
6573 :
6574 708 : tree r = NULL_TREE;
6575 708 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6576 : {
6577 369 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6578 369 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6579 : {
6580 46 : gcc_assert (len == 1);
6581 46 : if (mask[0])
6582 : {
6583 24 : memset (mask, 0, len);
6584 24 : r = build_constructor (TREE_TYPE (r), NULL);
6585 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6586 : }
6587 : }
6588 : }
6589 339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6590 : {
6591 339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6592 339 : if (r != NULL_TREE)
6593 : {
6594 339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6595 339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6596 339 : if (CHECKING_P)
6597 : {
6598 339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6599 : non_constant_p, overflow_p,
6600 : jump_target);
6601 339 : gcc_checking_assert (e == r && !*jump_target);
6602 : r = e;
6603 : }
6604 : }
6605 : }
6606 :
6607 708 : if (r != NULL_TREE)
6608 : {
6609 5935 : for (int i = 0; i < len; i++)
6610 5317 : if (mask[i])
6611 : {
6612 90 : if (!ctx->quiet)
6613 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6614 : "__builtin_bit_cast", i);
6615 90 : *non_constant_p = true;
6616 90 : r = t;
6617 90 : break;
6618 : }
6619 708 : if (ptr != buf)
6620 3 : XDELETE (ptr);
6621 708 : return r;
6622 : }
6623 :
6624 0 : if (!ctx->quiet)
6625 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6626 : "argument cannot be interpreted", "__builtin_bit_cast");
6627 0 : *non_constant_p = true;
6628 0 : if (ptr != buf)
6629 0 : XDELETE (ptr);
6630 : return t;
6631 : }
6632 :
6633 : /* Subroutine of cxx_eval_constant_expression.
6634 : Evaluate a short-circuited logical expression T in the context
6635 : of a given constexpr CALL. BAILOUT_VALUE is the value for
6636 : early return. CONTINUE_VALUE is used here purely for
6637 : sanity check purposes. */
6638 :
6639 : static tree
6640 14188803 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6641 : tree bailout_value, tree continue_value,
6642 : bool *non_constant_p, bool *overflow_p,
6643 : tree *jump_target)
6644 : {
6645 14188803 : tree r;
6646 14188803 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6647 : vc_prvalue, non_constant_p,
6648 : overflow_p, jump_target);
6649 14188802 : if (*jump_target)
6650 : return NULL_TREE;
6651 14188793 : VERIFY_CONSTANT (lhs);
6652 10158857 : if (tree_int_cst_equal (lhs, bailout_value))
6653 : return lhs;
6654 8675393 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6655 8675393 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6656 : vc_prvalue, non_constant_p,
6657 : overflow_p, jump_target);
6658 8675393 : if (*jump_target)
6659 : return NULL_TREE;
6660 8675393 : VERIFY_CONSTANT (r);
6661 : return r;
6662 : }
6663 :
6664 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6665 : CONSTRUCTOR elements to initialize (part of) an object containing that
6666 : field. Return a pointer to the constructor_elt corresponding to the
6667 : initialization of the field. */
6668 :
6669 : static constructor_elt *
6670 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6671 : {
6672 0 : tree aggr = TREE_OPERAND (ref, 0);
6673 0 : tree field = TREE_OPERAND (ref, 1);
6674 0 : HOST_WIDE_INT i;
6675 0 : constructor_elt *ce;
6676 :
6677 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6678 :
6679 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6680 : {
6681 0 : constructor_elt *base_ce
6682 0 : = base_field_constructor_elt (v, aggr);
6683 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6684 : }
6685 :
6686 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6687 0 : if (ce->index == field)
6688 0 : return ce;
6689 :
6690 0 : gcc_unreachable ();
6691 : return NULL;
6692 : }
6693 :
6694 : /* Some of the expressions fed to the constexpr mechanism are calls to
6695 : constructors, which have type void. In that case, return the type being
6696 : initialized by the constructor. */
6697 :
6698 : static tree
6699 543483987 : initialized_type (tree t)
6700 : {
6701 544717471 : if (TYPE_P (t))
6702 : return t;
6703 544717014 : tree type = TREE_TYPE (t);
6704 544717014 : if (TREE_CODE (t) == CALL_EXPR)
6705 : {
6706 : /* A constructor call has void type, so we need to look deeper. */
6707 67884240 : tree fn = get_function_named_in_call (t);
6708 67882198 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6709 135648065 : && DECL_CXX_CONSTRUCTOR_P (fn))
6710 3783150 : type = DECL_CONTEXT (fn);
6711 : }
6712 476832774 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6713 1233484 : return initialized_type (TREE_OPERAND (t, 1));
6714 475599290 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6715 776208 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6716 543483530 : return cv_unqualified (type);
6717 : }
6718 :
6719 : /* We're about to initialize element INDEX of an array or class from VALUE.
6720 : Set up NEW_CTX appropriately by adjusting .object to refer to the
6721 : subobject and creating a new CONSTRUCTOR if the element is itself
6722 : a class or array. */
6723 :
6724 : static void
6725 2909705 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6726 : tree index, tree &value)
6727 : {
6728 2909705 : new_ctx = *ctx;
6729 :
6730 2909705 : if (index && TREE_CODE (index) != INTEGER_CST
6731 2642841 : && TREE_CODE (index) != FIELD_DECL
6732 3 : && TREE_CODE (index) != RANGE_EXPR)
6733 : /* This won't have an element in the new CONSTRUCTOR. */
6734 : return;
6735 :
6736 2909705 : tree type = initialized_type (value);
6737 2909705 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6738 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6739 : return;
6740 :
6741 1504463 : tree ctxtype = NULL_TREE;
6742 1504463 : if (ctx->ctor)
6743 1504446 : ctxtype = TREE_TYPE (ctx->ctor);
6744 17 : else if (ctx->object)
6745 14 : ctxtype = TREE_TYPE (ctx->object);
6746 : else
6747 : {
6748 : /* This can happen if the enclosing object is also an empty subobject
6749 : (c++/125315). */
6750 3 : gcc_checking_assert (is_empty_field (index));
6751 : return;
6752 : }
6753 :
6754 1504460 : if (VECTOR_TYPE_P (type)
6755 2950 : && VECTOR_TYPE_P (ctxtype)
6756 922 : && index == NULL_TREE)
6757 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6758 : vector is constructed from smaller vectors, doesn't get its own
6759 : CONSTRUCTOR either. */
6760 : return;
6761 :
6762 : /* The sub-aggregate initializer might contain a placeholder;
6763 : update object to refer to the subobject and ctor to refer to
6764 : the (newly created) sub-initializer. */
6765 1503538 : if (ctx->object)
6766 : {
6767 1364745 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6768 : /* There's no well-defined subobject for this index. */
6769 3 : new_ctx.object = NULL_TREE;
6770 : else
6771 1364742 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6772 : }
6773 :
6774 1503538 : if (is_empty_field (index)
6775 1503538 : && TREE_CODE (ctxtype) != UNION_TYPE)
6776 : /* Leave ctor null for an empty subobject of a non-union class, they aren't
6777 : represented in the result of evaluation. */
6778 1414760 : new_ctx.ctor = NULL_TREE;
6779 : else
6780 : {
6781 88778 : tree elt = build_constructor (type, NULL);
6782 88778 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6783 88778 : new_ctx.ctor = elt;
6784 : }
6785 :
6786 1503538 : if (TREE_CODE (value) == TARGET_EXPR)
6787 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6788 63450 : value = TARGET_EXPR_INITIAL (value);
6789 : }
6790 :
6791 : /* We're about to process an initializer for a class or array TYPE. Make
6792 : sure that CTX is set up appropriately. */
6793 :
6794 : static void
6795 2416273 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6796 : {
6797 : /* We don't bother building a ctor for an empty base subobject. */
6798 2416273 : if (is_empty_class (type))
6799 : return;
6800 :
6801 : /* We're in the middle of an initializer that might involve placeholders;
6802 : our caller should have created a CONSTRUCTOR for us to put the
6803 : initializer into. We will either return that constructor or T. */
6804 1007865 : gcc_assert (ctx->ctor);
6805 1007865 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6806 : (type, TREE_TYPE (ctx->ctor)));
6807 : /* We used to check that ctx->ctor was empty, but that isn't the case when
6808 : the object is zero-initialized before calling the constructor. */
6809 1007865 : if (ctx->object)
6810 : {
6811 850003 : tree otype = TREE_TYPE (ctx->object);
6812 850003 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6813 : /* Handle flexible array members. */
6814 : || (TREE_CODE (otype) == ARRAY_TYPE
6815 : && TYPE_DOMAIN (otype) == NULL_TREE
6816 : && TREE_CODE (type) == ARRAY_TYPE
6817 : && (same_type_ignoring_top_level_qualifiers_p
6818 : (TREE_TYPE (type), TREE_TYPE (otype)))));
6819 : }
6820 1007865 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6821 : || ctx->global->get_value (ctx->object) == ctx->ctor);
6822 : }
6823 :
6824 : /* Subroutine of cxx_eval_constant_expression.
6825 : The expression tree T denotes a C-style array or a C-style
6826 : aggregate. Reduce it to a constant expression. */
6827 :
6828 : static tree
6829 2415608 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6830 : value_cat lval,
6831 : bool *non_constant_p, bool *overflow_p,
6832 : tree *jump_target)
6833 : {
6834 2415608 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6835 2415608 : bool changed = false;
6836 2415608 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6837 2415608 : tree type = TREE_TYPE (t);
6838 :
6839 2415608 : constexpr_ctx new_ctx;
6840 2415608 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6841 : {
6842 : /* We don't really need the ctx->ctor business for a PMF or
6843 : vector, but it's simpler to use the same code. */
6844 32140 : new_ctx = *ctx;
6845 32140 : new_ctx.ctor = build_constructor (type, NULL);
6846 32140 : new_ctx.object = NULL_TREE;
6847 32140 : ctx = &new_ctx;
6848 2415608 : };
6849 2415608 : verify_ctor_sanity (ctx, type);
6850 2415608 : vec<constructor_elt, va_gc> **p = nullptr;
6851 2415608 : if (ctx->ctor)
6852 : {
6853 2415591 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6854 4828155 : vec_alloc (*p, vec_safe_length (v));
6855 2415591 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6856 16050 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6857 : }
6858 :
6859 2415608 : unsigned i;
6860 2415608 : tree index, value;
6861 2415608 : bool constant_p = true;
6862 2415608 : bool side_effects_p = false;
6863 4971624 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6864 : {
6865 2903507 : tree orig_value = value;
6866 2903507 : init_subob_ctx (ctx, new_ctx, index, value);
6867 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6868 2903507 : bool no_slot = new_ctx.ctor == NULL_TREE;
6869 2903507 : int pos_hint = -1;
6870 2903507 : if (!ctx->ctor)
6871 : /* The enclosing object could be an empty subobject so we have no
6872 : CONSTRUCTOR (c++/125336). */
6873 17 : gcc_checking_assert (is_empty_class (type));
6874 2903490 : else if (new_ctx.ctor != ctx->ctor && !no_slot)
6875 : {
6876 : /* If we built a new CONSTRUCTOR, attach it now so that other
6877 : initializers can refer to it. */
6878 82717 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6879 82717 : cep->value = new_ctx.ctor;
6880 82717 : pos_hint = cep - (*p)->begin();
6881 82717 : }
6882 2820773 : else if (TREE_CODE (type) == UNION_TYPE)
6883 : /* Otherwise if we're constructing a non-aggregate union member, set
6884 : the active union member now so that we can later detect and diagnose
6885 : if its initializer attempts to activate another member. */
6886 1095 : get_or_insert_ctor_field (ctx->ctor, index);
6887 2903507 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6888 : lval,
6889 : non_constant_p, overflow_p,
6890 : jump_target);
6891 2903507 : if (*jump_target)
6892 : return NULL_TREE;
6893 : /* Don't VERIFY_CONSTANT here. */
6894 2903501 : if (ctx->quiet && *non_constant_p)
6895 : break;
6896 2556016 : if (elt != orig_value)
6897 678554 : changed = true;
6898 :
6899 2556016 : if (!TREE_CONSTANT (elt))
6900 560602 : constant_p = false;
6901 2556016 : if (TREE_SIDE_EFFECTS (elt))
6902 191 : side_effects_p = true;
6903 2556016 : if (index && TREE_CODE (index) == COMPONENT_REF)
6904 : {
6905 : /* This is an initialization of a vfield inside a base
6906 : subaggregate that we already initialized; push this
6907 : initialization into the previous initialization. */
6908 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6909 0 : inner->value = elt;
6910 0 : changed = true;
6911 0 : }
6912 2556016 : else if (no_slot)
6913 : /* This is an initializer for an empty field; now that we've
6914 : checked that it's constant, we can ignore it. */
6915 : changed = true;
6916 1141756 : else if (ctx->ctor)
6917 : {
6918 1141756 : if (TREE_CODE (type) == UNION_TYPE
6919 1141756 : && (*p)->last().index != index)
6920 : /* The initializer erroneously changed the active union member that
6921 : we're initializing. */
6922 7 : gcc_assert (*non_constant_p);
6923 : else
6924 : {
6925 : /* The initializer might have mutated the underlying CONSTRUCTOR,
6926 : so recompute the location of the target constructer_elt. */
6927 1141749 : constructor_elt *cep
6928 1141749 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6929 1141749 : cep->value = elt;
6930 : }
6931 :
6932 : /* Adding or replacing an element might change the ctor's flags. */
6933 1141756 : TREE_CONSTANT (ctx->ctor) = constant_p;
6934 1141756 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6935 : }
6936 : }
6937 2415602 : if (*non_constant_p)
6938 : return t;
6939 2068066 : if (!changed)
6940 : {
6941 187582 : if (VECTOR_TYPE_P (type))
6942 14357 : t = fold (t);
6943 187582 : return t;
6944 : }
6945 1880484 : t = ctx->ctor;
6946 1880484 : if (!t)
6947 8 : t = build_constructor (type, NULL);
6948 : /* We're done building this CONSTRUCTOR, so now we can interpret an
6949 : element without an explicit initializer as value-initialized. */
6950 1880484 : CONSTRUCTOR_NO_CLEARING (t) = false;
6951 1880484 : TREE_CONSTANT (t) = constant_p;
6952 1880484 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6953 1880484 : if (VECTOR_TYPE_P (type))
6954 4832 : t = fold (t);
6955 : return t;
6956 : }
6957 :
6958 : /* Subroutine of cxx_eval_constant_expression.
6959 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6960 : initialization of a non-static data member of array type. Reduce it to a
6961 : CONSTRUCTOR.
6962 :
6963 : Note that apart from value-initialization (when VALUE_INIT is true),
6964 : this is only intended to support value-initialization and the
6965 : initializations done by defaulted constructors for classes with
6966 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6967 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6968 : for the copy/move constructor. */
6969 :
6970 : static tree
6971 665 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6972 : bool value_init, value_cat lval,
6973 : bool *non_constant_p, bool *overflow_p,
6974 : tree *jump_target)
6975 : {
6976 665 : tree elttype = TREE_TYPE (atype);
6977 665 : verify_ctor_sanity (ctx, atype);
6978 665 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6979 665 : bool pre_init = false;
6980 665 : unsigned HOST_WIDE_INT i;
6981 665 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6982 :
6983 665 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6984 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6985 0 : non_constant_p, overflow_p, jump_target);
6986 :
6987 : /* We already checked access when building the VEC_INIT_EXPR. */
6988 665 : deferring_access_check_sentinel acs (dk_deferred);
6989 :
6990 : /* For the default constructor, build up a call to the default
6991 : constructor of the element type. We only need to handle class types
6992 : here, as for a constructor to be constexpr, all members must be
6993 : initialized, which for a defaulted default constructor means they must
6994 : be of a class type with a constexpr default constructor. */
6995 665 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6996 : /* We only do this at the lowest level. */;
6997 616 : else if (value_init)
6998 : {
6999 174 : init = build_value_init (elttype, complain);
7000 174 : pre_init = true;
7001 : }
7002 442 : else if (!init)
7003 : {
7004 332 : releasing_vec argvec;
7005 332 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7006 : &argvec, elttype, LOOKUP_NORMAL,
7007 : complain);
7008 332 : init = build_aggr_init_expr (elttype, init);
7009 332 : pre_init = true;
7010 332 : }
7011 :
7012 665 : bool zeroed_out = false;
7013 665 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
7014 : {
7015 : /* We're initializing an array object that had been zero-initialized
7016 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
7017 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
7018 : initializers we append to it. */
7019 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
7020 156 : zeroed_out = true;
7021 156 : vec_safe_truncate (*p, 0);
7022 : }
7023 :
7024 665 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
7025 : overflow_p, jump_target);
7026 665 : if (*jump_target)
7027 : return NULL_TREE;
7028 665 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
7029 6427 : for (i = 0; i < max; ++i)
7030 : {
7031 6198 : tree idx = build_int_cst (size_type_node, i);
7032 6198 : tree eltinit;
7033 6198 : bool reuse = false;
7034 6198 : constexpr_ctx new_ctx;
7035 6655 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
7036 6198 : bool no_slot = new_ctx.ctor == NULL_TREE;
7037 6198 : if (new_ctx.ctor != ctx->ctor && !no_slot)
7038 : {
7039 6061 : if (zeroed_out)
7040 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
7041 6061 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
7042 : }
7043 6198 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7044 : {
7045 : /* A multidimensional array; recurse. */
7046 181 : if (value_init || init == NULL_TREE)
7047 : {
7048 171 : eltinit = NULL_TREE;
7049 171 : reuse = i == 0;
7050 : }
7051 : else
7052 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7053 181 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
7054 : value_init, lval, non_constant_p,
7055 : overflow_p, jump_target);
7056 : }
7057 6017 : else if (pre_init)
7058 : {
7059 : /* Initializing an element using value or default initialization
7060 : we just pre-built above. */
7061 5741 : if (init == void_node)
7062 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
7063 3 : return ctx->ctor;
7064 5738 : eltinit = init;
7065 5738 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7066 : /* Clarify what object is being initialized (118285). */
7067 5602 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7068 5738 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7069 : non_constant_p, overflow_p,
7070 : jump_target);
7071 5738 : reuse = i == 0;
7072 : }
7073 : else
7074 : {
7075 : /* Copying an element. */
7076 276 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7077 276 : if (!lvalue_p (init))
7078 232 : eltinit = move (eltinit);
7079 276 : eltinit = (perform_implicit_conversion_flags
7080 276 : (elttype, eltinit, complain,
7081 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7082 269 : if (CLASS_TYPE_P (elttype)
7083 269 : && new_ctx.object
7084 522 : && !error_operand_p (eltinit))
7085 : /* Clarify what object is being initialized (118285). */
7086 244 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7087 276 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7088 : non_constant_p, overflow_p,
7089 : jump_target);
7090 : }
7091 6195 : if (*jump_target)
7092 : return NULL_TREE;
7093 6195 : if (*non_constant_p)
7094 : break;
7095 6070 : if (no_slot)
7096 : {
7097 : /* This is an initializer for an empty subobject; now that we've
7098 : checked that it's constant, we can ignore it. */
7099 0 : gcc_checking_assert (i == 0);
7100 : break;
7101 : }
7102 6070 : else if (new_ctx.ctor != ctx->ctor)
7103 : {
7104 : /* We appended this element above; update the value. */
7105 5940 : gcc_assert ((*p)->last().index == idx);
7106 5940 : (*p)->last().value = eltinit;
7107 : }
7108 : else
7109 130 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7110 : /* Reuse the result of cxx_eval_constant_expression call
7111 : from the first iteration to all others if it is a constant
7112 : initializer that doesn't require relocations. */
7113 6070 : if (reuse
7114 6070 : && max > 1
7115 6070 : && (eltinit == NULL_TREE
7116 461 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7117 461 : == null_pointer_node)))
7118 : {
7119 308 : if (new_ctx.ctor != ctx->ctor)
7120 184 : eltinit = new_ctx.ctor;
7121 308 : tree range = build2 (RANGE_EXPR, size_type_node,
7122 : build_int_cst (size_type_node, 1),
7123 308 : build_int_cst (size_type_node, max - 1));
7124 308 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7125 308 : break;
7126 : }
7127 5762 : else if (i == 0)
7128 226 : vec_safe_reserve (*p, max);
7129 : }
7130 :
7131 662 : if (!*non_constant_p)
7132 : {
7133 537 : init = ctx->ctor;
7134 537 : CONSTRUCTOR_NO_CLEARING (init) = false;
7135 : }
7136 662 : return init;
7137 : }
7138 :
7139 : static tree
7140 556 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7141 : value_cat lval,
7142 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7143 : {
7144 556 : tree atype = TREE_TYPE (t);
7145 556 : tree init = VEC_INIT_EXPR_INIT (t);
7146 556 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7147 556 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7148 : ;
7149 83 : else if (CONSTRUCTOR_NELTS (init) == 0
7150 83 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7151 : {
7152 : /* Handle {} as value-init. */
7153 : init = NULL_TREE;
7154 : value_init = true;
7155 : }
7156 : else
7157 : {
7158 : /* This is a more complicated case, like needing to loop over trailing
7159 : elements; call build_vec_init and evaluate the result. */
7160 72 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7161 72 : constexpr_ctx new_ctx = *ctx;
7162 72 : if (!ctx->object)
7163 : {
7164 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7165 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7166 57 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7167 57 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7168 57 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7169 57 : ctx->global->put_value (new_ctx.object, ctor);
7170 57 : ctx = &new_ctx;
7171 : }
7172 72 : init = expand_vec_init_expr (ctx->object, t, complain);
7173 72 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7174 : overflow_p, jump_target);
7175 : }
7176 484 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7177 : lval, non_constant_p, overflow_p, jump_target);
7178 484 : if (*non_constant_p)
7179 : return t;
7180 : else
7181 382 : return r;
7182 : }
7183 :
7184 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7185 : where the desired type is an array of unknown bounds because the variable
7186 : has had its bounds deduced since the wrapping expression was created. */
7187 :
7188 : static bool
7189 344396180 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7190 : {
7191 344396180 : while (TREE_CODE (type1) == ARRAY_TYPE
7192 54418 : && TREE_CODE (type2) == ARRAY_TYPE
7193 344396184 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7194 : {
7195 2 : type1 = TREE_TYPE (type1);
7196 2 : type2 = TREE_TYPE (type2);
7197 : }
7198 344396180 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7199 : }
7200 :
7201 : /* Try to determine the currently active union member for an expression
7202 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7203 : otherwise return NULL_TREE. */
7204 :
7205 : static tree
7206 80 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7207 : {
7208 80 : constexpr_ctx new_ctx = *ctx;
7209 80 : new_ctx.quiet = true;
7210 80 : bool non_constant_p = false, overflow_p = false;
7211 80 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7212 : &non_constant_p,
7213 : &overflow_p, jump_target);
7214 80 : if (*jump_target)
7215 : return NULL_TREE;
7216 80 : if (TREE_CODE (ctor) == CONSTRUCTOR
7217 32 : && CONSTRUCTOR_NELTS (ctor) == 1
7218 20 : && CONSTRUCTOR_ELT (ctor, 0)->index
7219 100 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7220 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7221 : return NULL_TREE;
7222 : }
7223 :
7224 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7225 :
7226 : static tree
7227 12018959 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7228 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7229 : tree *jump_target)
7230 : {
7231 20301062 : tree optype = TREE_TYPE (op);
7232 20301062 : unsigned HOST_WIDE_INT const_nunits;
7233 20301062 : if (off == 0 && similar_type_p (optype, type))
7234 : return op;
7235 12015276 : else if (cxx_dialect >= cxx26
7236 6730821 : && VAR_P (op)
7237 4284432 : && DECL_VTABLE_OR_VTT_P (op)
7238 14653 : && same_type_ignoring_top_level_qualifiers_p (type,
7239 : ptrdiff_type_node)
7240 12016599 : && POINTER_TYPE_P (strip_array_types (optype)))
7241 : {
7242 : /* We often read some virtual table elements using ptrdiff_t rather
7243 : than pointer type. */
7244 1323 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7245 : strip_array_types (optype),
7246 : op, off, empty_base,
7247 : jump_target))
7248 1323 : return fold_convert (type, ret);
7249 : }
7250 12013953 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7251 12013953 : && similar_type_p (type, TREE_TYPE (optype)))
7252 : {
7253 : /* *(foo *)&complexfoo => __real__ complexfoo */
7254 0 : if (off == 0)
7255 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7256 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7257 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7258 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7259 : }
7260 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7261 12013953 : else if (VECTOR_TYPE_P (optype)
7262 0 : && similar_type_p (type, TREE_TYPE (optype))
7263 12013953 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7264 : {
7265 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7266 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7267 0 : if (off < max_offset && off % part_width == 0)
7268 : {
7269 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7270 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7271 0 : TYPE_SIZE (type), index);
7272 : }
7273 : }
7274 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7275 12013953 : else if (TREE_CODE (optype) == ARRAY_TYPE
7276 8282103 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7277 20296056 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7278 : {
7279 8282103 : tree type_domain = TYPE_DOMAIN (optype);
7280 8282103 : tree min_val = size_zero_node;
7281 8282103 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7282 8281964 : min_val = TYPE_MIN_VALUE (type_domain);
7283 8282103 : unsigned HOST_WIDE_INT el_sz
7284 8282103 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7285 8282103 : unsigned HOST_WIDE_INT idx = off / el_sz;
7286 8282103 : unsigned HOST_WIDE_INT rem = off % el_sz;
7287 8282103 : if (tree_fits_uhwi_p (min_val))
7288 : {
7289 8282103 : tree index = size_int (idx + tree_to_uhwi (min_val));
7290 8282103 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7291 : NULL_TREE, NULL_TREE);
7292 8282103 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7293 8282103 : empty_base, jump_target);
7294 : }
7295 : }
7296 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7297 3731850 : else if (TREE_CODE (optype) == RECORD_TYPE
7298 3731850 : || TREE_CODE (optype) == UNION_TYPE)
7299 : {
7300 3728428 : if (TREE_CODE (optype) == UNION_TYPE)
7301 : /* For unions prefer the currently active member. */
7302 80 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7303 : {
7304 20 : unsigned HOST_WIDE_INT el_sz
7305 20 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7306 20 : if (off < el_sz)
7307 : {
7308 20 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7309 : op, field, NULL_TREE);
7310 20 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7311 : off, empty_base,
7312 : jump_target))
7313 : return ret;
7314 : }
7315 : }
7316 :
7317 : /* Handle conversion to "as base" type. */
7318 3728414 : if (CLASS_TYPE_P (optype)
7319 7456664 : && CLASSTYPE_AS_BASE (optype) == type)
7320 : return op;
7321 :
7322 : /* Handle conversion to an empty base class, which is represented with a
7323 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7324 : which is likely to be a waste of time (109678). */
7325 3723129 : if (is_empty_class (type)
7326 3570771 : && CLASS_TYPE_P (optype)
7327 7293894 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
7328 : {
7329 1857501 : if (empty_base)
7330 1857501 : *empty_base = true;
7331 1857501 : return op;
7332 : }
7333 :
7334 1865628 : for (tree field = TYPE_FIELDS (optype);
7335 25246579 : field; field = DECL_CHAIN (field))
7336 25228591 : if (TREE_CODE (field) == FIELD_DECL
7337 1873904 : && TREE_TYPE (field) != error_mark_node
7338 27102495 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
7339 : {
7340 1873900 : tree pos = byte_position (field);
7341 1873900 : if (!tree_fits_uhwi_p (pos))
7342 0 : continue;
7343 1873900 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
7344 1873900 : unsigned HOST_WIDE_INT el_sz;
7345 1873900 : if (DECL_FIELD_IS_BASE (field)
7346 417654 : && CLASS_TYPE_P (optype)
7347 2291554 : && CLASSTYPE_VBASECLASSES (optype))
7348 7670 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
7349 : else
7350 1866230 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7351 1873900 : if (upos <= off && off < upos + el_sz)
7352 : {
7353 1865525 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7354 : op, field, NULL_TREE);
7355 1865525 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7356 : off - upos,
7357 : empty_base,
7358 : jump_target))
7359 : return ret;
7360 : }
7361 : }
7362 : }
7363 :
7364 : return NULL_TREE;
7365 : }
7366 :
7367 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7368 : match. We want to be less strict for simple *& folding; if we have a
7369 : non-const temporary that we access through a const pointer, that should
7370 : work. We handle this here rather than change fold_indirect_ref_1
7371 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7372 : don't really make sense outside of constant expression evaluation. Also
7373 : we want to allow folding to COMPONENT_REF, which could cause trouble
7374 : with TBAA in fold_indirect_ref_1. */
7375 :
7376 : static tree
7377 141197680 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7378 : tree op0, bool *empty_base, tree *jump_target)
7379 : {
7380 141197680 : tree sub = op0;
7381 141197680 : tree subtype;
7382 :
7383 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7384 290793074 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7385 378556538 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7386 : {
7387 94197588 : if (TREE_CODE (sub) == NOP_EXPR
7388 94197588 : && REINTERPRET_CAST_P (sub))
7389 : return NULL_TREE;
7390 94197588 : sub = TREE_OPERAND (sub, 0);
7391 : }
7392 :
7393 141197680 : subtype = TREE_TYPE (sub);
7394 141197680 : if (!INDIRECT_TYPE_P (subtype))
7395 : return NULL_TREE;
7396 :
7397 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7398 : the innermost component into the offset until it would make the
7399 : offset positive, so that cxx_fold_indirect_ref_1 can identify
7400 : more folding opportunities. */
7401 151349722 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7402 10152091 : if (cxx_dialect >= cxx26)
7403 : {
7404 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7405 : to access virtual base members. */
7406 5642127 : tree nobj = obj;
7407 5642127 : while (TREE_CODE (nobj) == COMPONENT_REF
7408 5663957 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7409 21830 : nobj = TREE_OPERAND (nobj, 0);
7410 5642127 : if (nobj != obj
7411 17285 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7412 5659412 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7413 2505 : while (obj != nobj)
7414 : {
7415 1407 : tree field = TREE_OPERAND (obj, 1);
7416 1407 : tree pos = byte_position (field);
7417 1407 : off = int_const_binop (PLUS_EXPR, off, pos);
7418 1407 : obj = TREE_OPERAND (obj, 0);
7419 : }
7420 : }
7421 12021790 : while (TREE_CODE (obj) == COMPONENT_REF
7422 : /* We need to preserve union member accesses so that we can
7423 : later properly diagnose accessing the wrong member. */
7424 4846756 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7425 16690363 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7426 : {
7427 1989219 : tree field = TREE_OPERAND (obj, 1);
7428 1989219 : tree pos = byte_position (field);
7429 1989219 : if (integer_zerop (off) && integer_nonzerop (pos))
7430 : /* If the offset is already 0, keep going as long as the
7431 : component is at position 0. */
7432 : break;
7433 1869699 : off = int_const_binop (PLUS_EXPR, off, pos);
7434 1869699 : obj = TREE_OPERAND (obj, 0);
7435 : }
7436 10152091 : };
7437 :
7438 141197631 : if (TREE_CODE (sub) == ADDR_EXPR)
7439 : {
7440 60787667 : tree op = TREE_OPERAND (sub, 0);
7441 60787667 : tree optype = TREE_TYPE (op);
7442 :
7443 : /* *&CONST_DECL -> to the value of the const decl. */
7444 60787667 : if (TREE_CODE (op) == CONST_DECL)
7445 0 : return DECL_INITIAL (op);
7446 : /* *&p => p; make sure to handle *&"str"[cst] here. */
7447 60787667 : if (similar_type_p (optype, type))
7448 : {
7449 58036807 : tree fop = fold_read_from_constant_string (op);
7450 58036807 : if (fop)
7451 : return fop;
7452 : else
7453 57842458 : return op;
7454 : }
7455 : else
7456 : {
7457 2750860 : tree off = integer_zero_node;
7458 2750860 : canonicalize_obj_off (op, off);
7459 2750860 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7460 : tree_to_uhwi (off), empty_base,
7461 : jump_target);
7462 : }
7463 : }
7464 80409964 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7465 80409964 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7466 : {
7467 7797208 : tree op00 = TREE_OPERAND (sub, 0);
7468 7797208 : tree off = TREE_OPERAND (sub, 1);
7469 :
7470 7797208 : STRIP_NOPS (op00);
7471 7797208 : if (TREE_CODE (op00) == ADDR_EXPR)
7472 : {
7473 7401231 : tree obj = TREE_OPERAND (op00, 0);
7474 7401231 : canonicalize_obj_off (obj, off);
7475 7401231 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7476 : tree_to_uhwi (off), empty_base,
7477 : jump_target);
7478 : }
7479 : }
7480 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7481 72612756 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7482 72612756 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7483 : {
7484 0 : tree type_domain;
7485 0 : tree min_val = size_zero_node;
7486 0 : tree newsub
7487 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7488 : jump_target);
7489 0 : if (*jump_target)
7490 : return NULL_TREE;
7491 0 : if (newsub)
7492 : sub = newsub;
7493 : else
7494 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7495 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7496 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7497 0 : min_val = TYPE_MIN_VALUE (type_domain);
7498 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7499 0 : NULL_TREE);
7500 : }
7501 :
7502 : return NULL_TREE;
7503 : }
7504 :
7505 : static tree
7506 74893256 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7507 : value_cat lval,
7508 : bool *non_constant_p, bool *overflow_p,
7509 : tree *jump_target)
7510 : {
7511 74893256 : tree orig_op0 = TREE_OPERAND (t, 0);
7512 74893256 : bool empty_base = false;
7513 :
7514 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7515 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7516 :
7517 74893256 : if (TREE_CODE (t) == MEM_REF
7518 74893256 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7519 : {
7520 9 : gcc_assert (ctx->quiet);
7521 9 : *non_constant_p = true;
7522 9 : return t;
7523 : }
7524 :
7525 : /* First try to simplify it directly. */
7526 74893247 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7527 : orig_op0, &empty_base, jump_target);
7528 74893247 : if (*jump_target)
7529 : return NULL_TREE;
7530 74893247 : if (!r)
7531 : {
7532 : /* If that didn't work, evaluate the operand first. */
7533 72360068 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7534 : vc_prvalue, non_constant_p,
7535 : overflow_p, jump_target);
7536 72360068 : if (*jump_target)
7537 : return NULL_TREE;
7538 : /* Don't VERIFY_CONSTANT here. */
7539 72360058 : if (*non_constant_p)
7540 : return t;
7541 :
7542 56042992 : if (!lval && integer_zerop (op0))
7543 : {
7544 70 : if (!ctx->quiet)
7545 12 : error ("dereferencing a null pointer");
7546 70 : *non_constant_p = true;
7547 70 : return t;
7548 : }
7549 :
7550 56042922 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7551 : &empty_base, jump_target);
7552 56042922 : if (*jump_target)
7553 : return NULL_TREE;
7554 56042922 : if (r == NULL_TREE)
7555 : {
7556 : /* We couldn't fold to a constant value. Make sure it's not
7557 : something we should have been able to fold. */
7558 652170 : tree sub = op0;
7559 652170 : STRIP_NOPS (sub);
7560 652170 : if (TREE_CODE (sub) == ADDR_EXPR)
7561 : {
7562 1677 : gcc_assert (!similar_type_p
7563 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7564 : /* DR 1188 says we don't have to deal with this. */
7565 1677 : if (!ctx->quiet)
7566 : {
7567 9 : auto_diagnostic_group d;
7568 15 : error_at (cp_expr_loc_or_input_loc (t),
7569 : "accessing value of %qT object through a %qT "
7570 : "glvalue in a constant expression",
7571 9 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7572 9 : tree ob = build_fold_indirect_ref (sub);
7573 9 : if (DECL_P (ob))
7574 : {
7575 9 : if (DECL_ARTIFICIAL (ob))
7576 2 : inform (DECL_SOURCE_LOCATION (ob),
7577 2 : "%qT object created here", TREE_TYPE (ob));
7578 : else
7579 7 : inform (DECL_SOURCE_LOCATION (ob),
7580 : "%q#D declared here", ob);
7581 : }
7582 9 : }
7583 1677 : *non_constant_p = true;
7584 1677 : return t;
7585 : }
7586 :
7587 650493 : if (lval == vc_glvalue && op0 != orig_op0)
7588 77349 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7589 573144 : if (!lval)
7590 472346 : VERIFY_CONSTANT (t);
7591 573144 : return t;
7592 : }
7593 : }
7594 :
7595 57923931 : r = cxx_eval_constant_expression (ctx, r,
7596 : lval, non_constant_p, overflow_p,
7597 : jump_target);
7598 57923930 : if (*jump_target)
7599 : return NULL_TREE;
7600 57923930 : if (*non_constant_p)
7601 : return t;
7602 :
7603 : /* If we're pulling out the value of an empty base, just return an empty
7604 : CONSTRUCTOR. */
7605 51645094 : if (empty_base && !lval)
7606 : {
7607 17014 : r = build_constructor (TREE_TYPE (t), NULL);
7608 17014 : TREE_CONSTANT (r) = true;
7609 : }
7610 :
7611 : return r;
7612 : }
7613 :
7614 : /* Complain about R, a DECL that is accessed outside its lifetime. */
7615 :
7616 : static void
7617 37 : outside_lifetime_error (location_t loc, tree r)
7618 : {
7619 37 : auto_diagnostic_group d;
7620 37 : if (DECL_NAME (r) == heap_deleted_identifier)
7621 : {
7622 : /* Provide a more accurate message for deleted variables. */
7623 6 : error_at (loc, "use of allocated storage after deallocation "
7624 : "in a constant expression");
7625 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7626 : }
7627 : else
7628 : {
7629 31 : error_at (loc, "accessing %qE outside its lifetime", r);
7630 31 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7631 : }
7632 37 : }
7633 :
7634 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7635 : FUNDEF_P is true if we're checking a constexpr function body.
7636 : Shared between potential_constant_expression and
7637 : cxx_eval_constant_expression. */
7638 :
7639 : static void
7640 350 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7641 : {
7642 350 : auto_diagnostic_group d;
7643 350 : tree type = TREE_TYPE (r);
7644 350 : if (DECL_NAME (r) == heap_uninit_identifier
7645 350 : || DECL_NAME (r) == heap_identifier
7646 347 : || DECL_NAME (r) == heap_vec_uninit_identifier
7647 697 : || DECL_NAME (r) == heap_vec_identifier)
7648 : {
7649 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7650 : "storage is not usable in a constant expression"))
7651 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7652 3 : return;
7653 : }
7654 347 : if (DECL_NAME (r) == heap_deleted_identifier)
7655 : {
7656 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7657 : "deallocation in a constant expression"))
7658 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7659 0 : return;
7660 : }
7661 347 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7662 : "a constant expression", r))
7663 : return;
7664 : /* Avoid error cascade. */
7665 344 : if (DECL_INITIAL (r) == error_mark_node)
7666 : return;
7667 330 : if (DECL_DECLARED_CONSTEXPR_P (r))
7668 3 : inform (DECL_SOURCE_LOCATION (r),
7669 : "%qD used in its own initializer", r);
7670 327 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7671 : {
7672 225 : if (!CP_TYPE_CONST_P (type))
7673 196 : inform (DECL_SOURCE_LOCATION (r),
7674 : "%q#D is not const", r);
7675 29 : else if (CP_TYPE_VOLATILE_P (type))
7676 0 : inform (DECL_SOURCE_LOCATION (r),
7677 : "%q#D is volatile", r);
7678 29 : else if (!DECL_INITIAL (r)
7679 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7680 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7681 29 : inform (DECL_SOURCE_LOCATION (r),
7682 : "%qD was not initialized with a constant "
7683 : "expression", r);
7684 : else
7685 0 : gcc_unreachable ();
7686 : }
7687 102 : else if (TYPE_REF_P (type))
7688 9 : inform (DECL_SOURCE_LOCATION (r),
7689 : "%qD was not initialized with a constant "
7690 : "expression", r);
7691 : else
7692 : {
7693 93 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7694 93 : inform (DECL_SOURCE_LOCATION (r),
7695 : "%qD was not declared %<constexpr%>", r);
7696 : else
7697 0 : inform (DECL_SOURCE_LOCATION (r),
7698 : "%qD does not have integral or enumeration type",
7699 : r);
7700 : }
7701 350 : }
7702 :
7703 : /* Subroutine of cxx_eval_constant_expression.
7704 : Like cxx_eval_unary_expression, except for trinary expressions. */
7705 :
7706 : static tree
7707 14887 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7708 : value_cat lval,
7709 : bool *non_constant_p, bool *overflow_p,
7710 : tree *jump_target)
7711 : {
7712 14887 : int i;
7713 14887 : tree args[3];
7714 14887 : tree val;
7715 :
7716 58078 : for (i = 0; i < 3; i++)
7717 : {
7718 43681 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7719 : lval,
7720 : non_constant_p, overflow_p,
7721 : jump_target);
7722 43681 : if (*jump_target)
7723 : return NULL_TREE;
7724 43681 : VERIFY_CONSTANT (args[i]);
7725 : }
7726 :
7727 14397 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7728 : args[0], args[1], args[2]);
7729 14397 : if (val == NULL_TREE)
7730 : return t;
7731 14397 : VERIFY_CONSTANT (val);
7732 : return val;
7733 : }
7734 :
7735 : /* True if T was declared in a function declared to be constexpr, and
7736 : therefore potentially constant in C++14. */
7737 :
7738 : bool
7739 84679348 : var_in_constexpr_fn (tree t)
7740 : {
7741 84679348 : tree ctx = DECL_CONTEXT (t);
7742 84679348 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7743 161302965 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7744 : }
7745 :
7746 : /* True if a function might be constexpr: either a function that was
7747 : declared constexpr, or a C++17 lambda op(). */
7748 :
7749 : bool
7750 603473516 : maybe_constexpr_fn (tree t)
7751 : {
7752 603473516 : return (DECL_DECLARED_CONSTEXPR_P (t)
7753 184092849 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7754 779866514 : || (flag_implicit_constexpr
7755 94 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7756 : }
7757 :
7758 : /* True if T was declared in a function that might be constexpr: either a
7759 : function that was declared constexpr, or a C++17 lambda op(). */
7760 :
7761 : bool
7762 60360558 : var_in_maybe_constexpr_fn (tree t)
7763 : {
7764 120720501 : return (DECL_FUNCTION_SCOPE_P (t)
7765 110007651 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7766 : }
7767 :
7768 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7769 : build_over_call we implement trivial copy of a class with tail padding using
7770 : assignment of character arrays, which is valid in normal code, but not in
7771 : constexpr evaluation. We don't need to worry about clobbering tail padding
7772 : in constexpr evaluation, so strip the type punning. */
7773 :
7774 : static void
7775 78581746 : maybe_simplify_trivial_copy (tree &target, tree &init)
7776 : {
7777 78581746 : if (TREE_CODE (target) == MEM_REF
7778 35765 : && TREE_CODE (init) == MEM_REF
7779 35601 : && TREE_TYPE (target) == TREE_TYPE (init)
7780 35601 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7781 78617347 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7782 : {
7783 35601 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7784 35601 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7785 : }
7786 78581746 : }
7787 :
7788 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7789 : of constant type. This does not check for 'mutable', so the
7790 : caller is expected to be mindful of that. */
7791 :
7792 : static bool
7793 419 : cref_has_const_field (tree ref)
7794 : {
7795 488 : while (TREE_CODE (ref) == COMPONENT_REF)
7796 : {
7797 479 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7798 : return true;
7799 69 : ref = TREE_OPERAND (ref, 0);
7800 : }
7801 : return false;
7802 : }
7803 :
7804 : /* Return true if we are modifying something that is const during constant
7805 : expression evaluation. CODE is the code of the statement, OBJ is the
7806 : object in question, MUTABLE_P is true if one of the subobjects were
7807 : declared mutable. */
7808 :
7809 : static bool
7810 80453081 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7811 : {
7812 : /* If this is initialization, there's no problem. */
7813 80453081 : if (code != MODIFY_EXPR)
7814 : return false;
7815 :
7816 : /* [basic.type.qualifier] "A const object is an object of type
7817 : const T or a non-mutable subobject of a const object." */
7818 24946080 : if (mutable_p)
7819 : return false;
7820 :
7821 24945344 : if (TREE_READONLY (obj))
7822 : return true;
7823 :
7824 24892096 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7825 : {
7826 : /* Although a COMPONENT_REF may have a const type, we should
7827 : only consider it modifying a const object when any of the
7828 : field components is const. This can happen when using
7829 : constructs such as const_cast<const T &>(m), making something
7830 : const even though it wasn't declared const. */
7831 31490 : if (TREE_CODE (obj) == COMPONENT_REF)
7832 419 : return cref_has_const_field (obj);
7833 : else
7834 : return true;
7835 : }
7836 :
7837 : return false;
7838 : }
7839 :
7840 : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7841 :
7842 : static tree
7843 78581746 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7844 : value_cat lval,
7845 : bool *non_constant_p, bool *overflow_p,
7846 : tree *jump_target)
7847 : {
7848 78581746 : constexpr_ctx new_ctx = *ctx;
7849 :
7850 78581746 : tree init = TREE_OPERAND (t, 1);
7851 :
7852 : /* First we figure out where we're storing to. */
7853 78581746 : tree target = TREE_OPERAND (t, 0);
7854 :
7855 78581746 : maybe_simplify_trivial_copy (target, init);
7856 :
7857 78581746 : tree type = TREE_TYPE (target);
7858 78581746 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7859 57628331 : if (preeval && !TREE_CLOBBER_P (init))
7860 : {
7861 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7862 56963617 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7863 28795732 : && TREE_CODE (init) == CALL_EXPR
7864 4193724 : && CALL_EXPR_FN (init) == NULL_TREE
7865 56963625 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7866 8 : return void_node;
7867 :
7868 : /* Evaluate the value to be stored without knowing what object it will be
7869 : stored in, so that any side-effects happen first. */
7870 56963609 : if (!SCALAR_TYPE_P (type))
7871 153848 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7872 56963609 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7873 : non_constant_p, overflow_p,
7874 : jump_target);
7875 56963607 : if (*jump_target)
7876 : return NULL_TREE;
7877 56963385 : if (*non_constant_p)
7878 : return t;
7879 : }
7880 :
7881 70875122 : bool evaluated = false;
7882 70875122 : if (lval == vc_glvalue)
7883 : {
7884 : /* If we want to return a reference to the target, we need to evaluate it
7885 : as a whole; otherwise, only evaluate the innermost piece to avoid
7886 : building up unnecessary *_REFs. */
7887 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7888 : non_constant_p, overflow_p,
7889 : jump_target);
7890 0 : evaluated = true;
7891 0 : if (*jump_target)
7892 : return NULL_TREE;
7893 0 : if (*non_constant_p)
7894 : return t;
7895 : }
7896 :
7897 : /* Find the underlying variable. */
7898 70875122 : releasing_vec refs;
7899 70875122 : tree object = NULL_TREE;
7900 : /* If we're modifying a const object, save it. */
7901 70875122 : tree const_object_being_modified = NULL_TREE;
7902 70875122 : bool mutable_p = false;
7903 : /* If we see a union, we can't ignore clobbers. */
7904 70875122 : int seen_union = 0;
7905 239717783 : for (tree probe = target; object == NULL_TREE; )
7906 : {
7907 168842999 : switch (TREE_CODE (probe))
7908 : {
7909 27092992 : case BIT_FIELD_REF:
7910 27092992 : case COMPONENT_REF:
7911 27092992 : case ARRAY_REF:
7912 27092992 : {
7913 27092992 : tree ob = TREE_OPERAND (probe, 0);
7914 27092992 : tree elt = TREE_OPERAND (probe, 1);
7915 27092992 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7916 : mutable_p = true;
7917 27092992 : if (TREE_CODE (probe) == ARRAY_REF)
7918 : {
7919 4996983 : elt = eval_and_check_array_index (ctx, probe, false,
7920 : non_constant_p, overflow_p,
7921 : jump_target);
7922 4996983 : if (*jump_target)
7923 69 : return NULL_TREE;
7924 4996983 : if (*non_constant_p)
7925 : return t;
7926 : }
7927 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7928 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7929 : the array isn't const. Instead, check "a" in the next iteration;
7930 : that will detect modifying "const int a[10]". */
7931 22096009 : else if (evaluated
7932 9578297 : && modifying_const_object_p (TREE_CODE (t), probe,
7933 : mutable_p)
7934 22096623 : && const_object_being_modified == NULL_TREE)
7935 : const_object_being_modified = probe;
7936 :
7937 : /* Track named member accesses for unions to validate modifications
7938 : that change active member. */
7939 27092923 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7940 12517712 : vec_safe_push (refs, probe);
7941 : else
7942 14575211 : vec_safe_push (refs, NULL_TREE);
7943 :
7944 27092923 : vec_safe_push (refs, elt);
7945 27092923 : vec_safe_push (refs, TREE_TYPE (probe));
7946 27092923 : probe = ob;
7947 27092923 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
7948 502201 : ++seen_union;
7949 : }
7950 27092923 : break;
7951 :
7952 16 : case REALPART_EXPR:
7953 16 : gcc_assert (refs->is_empty ());
7954 16 : vec_safe_push (refs, NULL_TREE);
7955 16 : vec_safe_push (refs, probe);
7956 16 : vec_safe_push (refs, TREE_TYPE (probe));
7957 16 : probe = TREE_OPERAND (probe, 0);
7958 16 : break;
7959 :
7960 17 : case IMAGPART_EXPR:
7961 17 : gcc_assert (refs->is_empty ());
7962 17 : vec_safe_push (refs, NULL_TREE);
7963 17 : vec_safe_push (refs, probe);
7964 17 : vec_safe_push (refs, TREE_TYPE (probe));
7965 17 : probe = TREE_OPERAND (probe, 0);
7966 17 : break;
7967 :
7968 141749974 : default:
7969 141749974 : if (evaluated)
7970 : object = probe;
7971 : else
7972 : {
7973 70875190 : tree pvar = tree_strip_any_location_wrapper (probe);
7974 70875190 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7975 : {
7976 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7977 : active union member. */
7978 101 : probe = DECL_VALUE_EXPR (pvar);
7979 101 : break;
7980 : }
7981 70875089 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7982 : non_constant_p, overflow_p,
7983 : jump_target);
7984 70875089 : evaluated = true;
7985 70875089 : if (*jump_target)
7986 : return NULL_TREE;
7987 70875089 : if (*non_constant_p)
7988 : return t;
7989 : }
7990 : break;
7991 : }
7992 : }
7993 :
7994 70874784 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7995 70874784 : && const_object_being_modified == NULL_TREE)
7996 70874784 : const_object_being_modified = object;
7997 :
7998 70874784 : if (DECL_P (object)
7999 70872905 : && TREE_CLOBBER_P (init)
8000 71550876 : && DECL_NAME (object) == heap_deleted_identifier)
8001 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
8002 : message later when operator delete is called. */
8003 15 : return void_node;
8004 :
8005 : /* And then find/build up our initializer for the path to the subobject
8006 : we're initializing. */
8007 70874769 : tree *valp;
8008 70874769 : if (DECL_P (object))
8009 70872890 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8010 : else
8011 1879 : valp = NULL;
8012 70874769 : if (!valp)
8013 : {
8014 : /* A constant-expression cannot modify objects from outside the
8015 : constant-expression. */
8016 7210 : if (!ctx->quiet)
8017 : {
8018 29 : auto_diagnostic_group d;
8019 29 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
8020 : {
8021 0 : error ("modification of allocated storage after deallocation "
8022 : "is not a constant expression");
8023 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
8024 : }
8025 29 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
8026 : {
8027 14 : if (TREE_CLOBBER_P (init))
8028 6 : error ("destroying %qE outside its lifetime", object);
8029 : else
8030 8 : error ("modification of %qE outside its lifetime "
8031 : "is not a constant expression", object);
8032 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
8033 : }
8034 : else
8035 : {
8036 15 : if (TREE_CLOBBER_P (init))
8037 6 : error ("destroying %qE from outside current evaluation "
8038 : "is not a constant expression", object);
8039 : else
8040 9 : error ("modification of %qE from outside current evaluation "
8041 : "is not a constant expression", object);
8042 : }
8043 29 : }
8044 7210 : *non_constant_p = true;
8045 7210 : return t;
8046 : }
8047 :
8048 : /* Handle explicit end-of-lifetime. */
8049 70867559 : if (TREE_CLOBBER_P (init))
8050 : {
8051 676027 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
8052 676027 : && refs->is_empty ())
8053 : {
8054 215878 : ctx->global->destroy_value (object);
8055 215878 : return void_node;
8056 : }
8057 :
8058 448821 : if (!seen_union && !*valp
8059 463525 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
8060 3338 : return void_node;
8061 :
8062 : /* Ending the lifetime of a const object is OK. */
8063 : const_object_being_modified = NULL_TREE;
8064 : }
8065 :
8066 70648343 : type = TREE_TYPE (object);
8067 70648343 : bool no_zero_init = true;
8068 70648343 : bool zero_padding_bits = false;
8069 :
8070 141296686 : auto_vec<tree *> ctors;
8071 141296686 : releasing_vec indexes;
8072 141296686 : auto_vec<int> index_pos_hints;
8073 70648343 : bool activated_union_member_p = false;
8074 70648343 : bool empty_base = false;
8075 97540585 : while (!refs->is_empty ())
8076 : {
8077 27075102 : if (*valp == NULL_TREE)
8078 : {
8079 2121657 : *valp = build_constructor (type, NULL);
8080 2121657 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8081 2121657 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8082 : }
8083 24953445 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8084 24953445 : TREE_CODE (*valp) == STRING_CST)
8085 : {
8086 : /* An array was initialized with a string constant, and now
8087 : we're writing into one of its elements. Explode the
8088 : single initialization into a set of element
8089 : initializations. */
8090 10982 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8091 :
8092 10982 : tree string = *valp;
8093 10982 : tree elt_type = TREE_TYPE (type);
8094 10982 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8095 10982 : / TYPE_PRECISION (char_type_node));
8096 10982 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8097 10982 : tree ary_ctor = build_constructor (type, NULL);
8098 :
8099 10982 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8100 22118 : for (unsigned ix = 0; ix != num_elts; ix++)
8101 : {
8102 11136 : constructor_elt elt =
8103 : {
8104 11136 : build_int_cst (size_type_node, ix),
8105 11136 : extract_string_elt (string, chars_per_elt, ix)
8106 11136 : };
8107 11136 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8108 : }
8109 :
8110 10982 : *valp = ary_ctor;
8111 : }
8112 :
8113 27075102 : enum tree_code code = TREE_CODE (type);
8114 27075102 : tree reftype = refs->pop();
8115 27075102 : tree index = refs->pop();
8116 27075102 : bool is_access_expr = refs->pop() != NULL_TREE;
8117 :
8118 27075102 : if (code == COMPLEX_TYPE)
8119 : {
8120 33 : if (TREE_CODE (*valp) == COMPLEX_CST)
8121 29 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8122 29 : TREE_IMAGPART (*valp));
8123 4 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8124 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8125 6 : && CONSTRUCTOR_NO_CLEARING (*valp))
8126 : {
8127 2 : tree r = build_constructor (reftype, NULL);
8128 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8129 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8130 : }
8131 33 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8132 33 : ctors.safe_push (valp);
8133 33 : vec_safe_push (indexes, index);
8134 33 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8135 33 : gcc_checking_assert (refs->is_empty ());
8136 : type = reftype;
8137 180050 : break;
8138 : }
8139 :
8140 : /* If the value of object is already zero-initialized, any new ctors for
8141 : subobjects will also be zero-initialized. Similarly with zeroing of
8142 : padding bits. */
8143 27075069 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8144 27075069 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8145 :
8146 27075069 : if (code == RECORD_TYPE && is_empty_field (index))
8147 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8148 : have no data and might have an offset lower than previously declared
8149 : fields, which confuses the middle-end. The code below will notice
8150 : that we don't have a CONSTRUCTOR for our inner target and just
8151 : return init. */
8152 : {
8153 : empty_base = true;
8154 : break;
8155 : }
8156 :
8157 : /* If a union is zero-initialized, its first non-static named data member
8158 : is zero-initialized (and therefore active). */
8159 26895052 : if (code == UNION_TYPE
8160 26895052 : && !no_zero_init
8161 26895052 : && CONSTRUCTOR_NELTS (*valp) == 0)
8162 2409 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8163 2409 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8164 :
8165 : /* Check for implicit change of active member for a union. */
8166 :
8167 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8168 : now allow initializing an array element to activate the array. */
8169 26943010 : auto only_array_refs = [](const releasing_vec &refs)
8170 : {
8171 47963 : for (unsigned i = 1; i < refs->length(); i += 3)
8172 35 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8173 : return false;
8174 : return true;
8175 : };
8176 :
8177 26895052 : if (code == UNION_TYPE
8178 500689 : && (CONSTRUCTOR_NELTS (*valp) == 0
8179 328058 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8180 : /* An INIT_EXPR of the last member in an access chain is always OK,
8181 : but still check implicit change of members earlier on; see
8182 : cpp2a/constexpr-union6.C. */
8183 27077470 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8184 : {
8185 134490 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8186 134490 : tree inner = strip_array_types (reftype);
8187 :
8188 134490 : if (has_active_member && cxx_dialect < cxx20)
8189 : {
8190 58 : if (!ctx->quiet)
8191 19 : error_at (cp_expr_loc_or_input_loc (t),
8192 : "change of the active member of a union "
8193 : "from %qD to %qD is not a constant expression "
8194 : "before C++20",
8195 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8196 : index);
8197 58 : *non_constant_p = true;
8198 : }
8199 134432 : else if (!is_access_expr
8200 41362 : || (TREE_CLOBBER_P (init)
8201 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8202 175794 : || (TREE_CODE (t) == MODIFY_EXPR
8203 41350 : && CLASS_TYPE_P (inner)
8204 17 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8205 : {
8206 : /* Diagnose changing active union member after initialization
8207 : without a valid member access expression, as described in
8208 : [class.union.general] p5. */
8209 93079 : if (!ctx->quiet)
8210 : {
8211 33 : auto_diagnostic_group d;
8212 33 : if (has_active_member)
8213 24 : error_at (cp_expr_loc_or_input_loc (t),
8214 : "accessing %qD member instead of initialized "
8215 : "%qD member in constant expression",
8216 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8217 : else
8218 9 : error_at (cp_expr_loc_or_input_loc (t),
8219 : "accessing uninitialized member %qD",
8220 : index);
8221 33 : if (is_access_expr)
8222 3 : inform (DECL_SOURCE_LOCATION (index),
8223 : "%qD does not implicitly begin its lifetime "
8224 : "because %qT does not have a non-deleted "
8225 : "trivial default constructor, use "
8226 : "%<std::construct_at%> instead",
8227 : index, inner);
8228 : else
8229 30 : inform (DECL_SOURCE_LOCATION (index),
8230 : "initializing %qD requires a member access "
8231 : "expression as the left operand of the assignment",
8232 : index);
8233 33 : }
8234 93079 : *non_constant_p = true;
8235 : }
8236 41353 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8237 : {
8238 : /* Diagnose changing the active union member while the union
8239 : is in the process of being initialized. */
8240 18 : if (!ctx->quiet)
8241 6 : error_at (cp_expr_loc_or_input_loc (t),
8242 : "change of the active member of a union "
8243 : "from %qD to %qD during initialization",
8244 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8245 : index);
8246 18 : *non_constant_p = true;
8247 : }
8248 : no_zero_init = true;
8249 : }
8250 :
8251 : /* Ending the lifetime of the active union member means the union no
8252 : longer has an active member. */
8253 500689 : if (code == UNION_TYPE && refs->is_empty ()
8254 88117 : && TREE_CLOBBER_P (init)
8255 26901853 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8256 : {
8257 1838 : vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
8258 2810 : return void_node;
8259 : }
8260 :
8261 26893214 : ctors.safe_push (valp);
8262 26893214 : vec_safe_push (indexes, index);
8263 :
8264 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8265 : uninitialized. */
8266 25201158 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8267 981881 : && CONSTRUCTOR_NO_CLEARING (*valp)
8268 27769735 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8269 26893214 : constructor_elt *cep
8270 26893214 : = get_or_insert_ctor_field (*valp, index, pos);
8271 26893214 : if (cep == nullptr)
8272 972 : return void_node;
8273 26892242 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8274 :
8275 26892242 : if (code == UNION_TYPE)
8276 : {
8277 498851 : activated_union_member_p = true;
8278 498851 : --seen_union;
8279 : }
8280 :
8281 26892242 : valp = &cep->value;
8282 26892242 : type = reftype;
8283 : }
8284 :
8285 : /* Change an "as-base" clobber to the real type;
8286 : we don't need to worry about padding in constexpr. */
8287 70645533 : tree itype = initialized_type (init);
8288 70645533 : if (IS_FAKE_BASE_TYPE (itype))
8289 4894 : itype = TYPE_CONTEXT (itype);
8290 :
8291 : /* For initialization of an empty base, the original target will be
8292 : *(base*)this, evaluation of which resolves to the object
8293 : argument, which has the derived type rather than the base type. */
8294 141111049 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8295 70465516 : (itype, type)))
8296 : {
8297 12530 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8298 : empty_base = true;
8299 : }
8300 :
8301 : /* Detect modifying a constant object in constexpr evaluation.
8302 : We have found a const object that is being modified. Figure out
8303 : if we need to issue an error. Consider
8304 :
8305 : struct A {
8306 : int n;
8307 : constexpr A() : n(1) { n = 2; } // #1
8308 : };
8309 : struct B {
8310 : const A a;
8311 : constexpr B() { a.n = 3; } // #2
8312 : };
8313 : constexpr B b{};
8314 :
8315 : #1 is OK, since we're modifying an object under construction, but
8316 : #2 is wrong, since "a" is const and has been fully constructed.
8317 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8318 : which means that the object is read-only. For the example above, the
8319 : *ctors stack at the point of #2 will look like:
8320 :
8321 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
8322 : ctors[1] = {.n=2} TREE_READONLY = 1
8323 :
8324 : and we're modifying "b.a", so we search the stack and see if the
8325 : constructor for "b.a" has already run. */
8326 70645533 : if (const_object_being_modified)
8327 : {
8328 83322 : bool fail = false;
8329 83322 : tree const_objtype
8330 83322 : = strip_array_types (TREE_TYPE (const_object_being_modified));
8331 83322 : if (!CLASS_TYPE_P (const_objtype))
8332 : fail = true;
8333 : else
8334 : {
8335 : /* [class.ctor]p5 "A constructor can be invoked for a const,
8336 : volatile, or const volatile object. const and volatile
8337 : semantics are not applied on an object under construction.
8338 : They come into effect when the constructor for the most
8339 : derived object ends." */
8340 250165 : for (tree *elt : ctors)
8341 83731 : if (same_type_ignoring_top_level_qualifiers_p
8342 83731 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
8343 : {
8344 83217 : fail = TREE_READONLY (*elt);
8345 83217 : break;
8346 : }
8347 : }
8348 83217 : if (fail)
8349 : {
8350 198 : if (!ctx->quiet)
8351 63 : modifying_const_object_error (t, const_object_being_modified);
8352 198 : *non_constant_p = true;
8353 198 : return t;
8354 : }
8355 : }
8356 :
8357 70645335 : if (!preeval)
8358 : {
8359 : /* We're handling an INIT_EXPR of class type, so the value of the
8360 : initializer can depend on the object it's initializing. */
8361 :
8362 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
8363 : wants to modify it. */
8364 20944236 : if (*valp == NULL_TREE)
8365 : {
8366 20259052 : *valp = build_constructor (type, NULL);
8367 20259052 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8368 20259052 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8369 : }
8370 20944236 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8371 20944236 : new_ctx.object = target;
8372 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8373 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8374 : expansion of those trees uses ctx instead. */
8375 20944236 : if (TREE_CODE (init) == TARGET_EXPR)
8376 2502795 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8377 2502795 : init = tinit;
8378 20944236 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8379 : non_constant_p, overflow_p,
8380 : jump_target);
8381 20944236 : if (*jump_target)
8382 : return NULL_TREE;
8383 : /* The hash table might have moved since the get earlier, and the
8384 : initializer might have mutated the underlying CONSTRUCTORs, so we must
8385 : recompute VALP. */
8386 20944136 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8387 23529777 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8388 : {
8389 2585641 : ctors[i] = valp;
8390 2585641 : constructor_elt *cep
8391 2585641 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8392 2585641 : valp = &cep->value;
8393 : }
8394 : }
8395 :
8396 70645235 : if (*non_constant_p)
8397 : return t;
8398 :
8399 : /* Don't share a CONSTRUCTOR that might be changed later. */
8400 69127919 : init = unshare_constructor (init);
8401 :
8402 69127919 : gcc_checking_assert (!*valp
8403 : || *valp == void_node
8404 : || (same_type_ignoring_top_level_qualifiers_p
8405 : (TREE_TYPE (*valp), type)));
8406 69127919 : if (empty_base)
8407 : {
8408 : /* Just evaluate the initializer and return, since there's no actual data
8409 : to store, and we didn't build a CONSTRUCTOR. */
8410 185371 : if (!*valp)
8411 : {
8412 : /* But do make sure we have something in *valp. */
8413 0 : *valp = build_constructor (type, nullptr);
8414 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8415 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8416 : }
8417 : }
8418 68942548 : else if (TREE_CLOBBER_P (init))
8419 : {
8420 454001 : if (AGGREGATE_TYPE_P (type))
8421 : {
8422 299764 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
8423 299721 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8424 : else
8425 43 : *valp = build_constructor (type, nullptr);
8426 299764 : TREE_CONSTANT (*valp) = true;
8427 299764 : TREE_SIDE_EFFECTS (*valp) = false;
8428 299764 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8429 299764 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8430 : }
8431 : else
8432 154237 : *valp = void_node;
8433 : }
8434 68488547 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8435 19473988 : && TREE_CODE (init) == CONSTRUCTOR)
8436 : {
8437 : /* An outer ctx->ctor might be pointing to *valp, so replace
8438 : its contents. */
8439 3403647 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8440 3403647 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8441 3403647 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8442 10210941 : CONSTRUCTOR_NO_CLEARING (*valp)
8443 3403647 : = CONSTRUCTOR_NO_CLEARING (init);
8444 10210941 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8445 3403647 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8446 : }
8447 : else
8448 65084900 : *valp = init;
8449 :
8450 : /* After initialization, 'const' semantics apply to the value of the
8451 : object. Make a note of this fact by marking the CONSTRUCTOR
8452 : TREE_READONLY. */
8453 69127919 : if (TREE_CODE (t) == INIT_EXPR
8454 50077308 : && !empty_base
8455 49891937 : && TREE_CODE (*valp) == CONSTRUCTOR
8456 72392195 : && TYPE_READONLY (type))
8457 : {
8458 28676 : tree target_type = TREE_TYPE (target);
8459 28676 : if (IS_FAKE_BASE_TYPE (target_type))
8460 0 : target_type = TYPE_CONTEXT (target_type);
8461 28676 : if (INDIRECT_REF_P (target)
8462 43739 : && (is_this_parameter
8463 15063 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8464 : /* We've just initialized '*this' (perhaps via the target
8465 : constructor of a delegating constructor). Leave it up to the
8466 : caller that set 'this' to set TREE_READONLY appropriately. */
8467 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8468 : (target_type, type) || empty_base);
8469 : else
8470 28653 : TREE_READONLY (*valp) = true;
8471 : }
8472 :
8473 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8474 : CONSTRUCTORs, if any. */
8475 69127919 : bool c = TREE_CONSTANT (init);
8476 69127919 : bool s = TREE_SIDE_EFFECTS (init);
8477 69127919 : if (!indexes->is_empty ())
8478 : {
8479 15261517 : tree last = indexes->last ();
8480 15261517 : if (TREE_CODE (last) == REALPART_EXPR
8481 15261517 : || TREE_CODE (last) == IMAGPART_EXPR)
8482 : {
8483 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8484 : possible. */
8485 33 : tree *cexpr = ctors.last ();
8486 33 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8487 33 : TREE_OPERAND (*cexpr, 0),
8488 33 : TREE_OPERAND (*cexpr, 1)))
8489 31 : *cexpr = c;
8490 : else
8491 : {
8492 4 : TREE_CONSTANT (*cexpr)
8493 2 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8494 2 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8495 4 : TREE_SIDE_EFFECTS (*cexpr)
8496 4 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8497 2 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8498 : }
8499 33 : c = TREE_CONSTANT (*cexpr);
8500 33 : s = TREE_SIDE_EFFECTS (*cexpr);
8501 : }
8502 : }
8503 69127919 : if (!c || s || activated_union_member_p)
8504 42166459 : for (tree *elt : ctors)
8505 : {
8506 8138260 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8507 0 : continue;
8508 8138260 : if (!c)
8509 6446223 : TREE_CONSTANT (*elt) = false;
8510 8138260 : if (s)
8511 0 : TREE_SIDE_EFFECTS (*elt) = true;
8512 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8513 : this union. */
8514 8138260 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8515 405514 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8516 : }
8517 :
8518 69127919 : if (lval)
8519 : return target;
8520 : else
8521 421264 : return init;
8522 70875122 : }
8523 :
8524 : /* Evaluate a ++ or -- expression. */
8525 :
8526 : static tree
8527 7299047 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8528 : value_cat lval,
8529 : bool *non_constant_p, bool *overflow_p,
8530 : tree *jump_target)
8531 : {
8532 7299047 : enum tree_code code = TREE_CODE (t);
8533 7299047 : tree type = TREE_TYPE (t);
8534 7299047 : tree op = TREE_OPERAND (t, 0);
8535 7299047 : tree offset = TREE_OPERAND (t, 1);
8536 7299047 : gcc_assert (TREE_CONSTANT (offset));
8537 :
8538 : /* OFFSET is constant, but perhaps not constant enough. We need to
8539 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8540 7299047 : offset = fold_simple (offset);
8541 :
8542 : /* The operand as an lvalue. */
8543 7299047 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8544 : non_constant_p, overflow_p,
8545 : jump_target);
8546 7299047 : if (*jump_target)
8547 : return NULL_TREE;
8548 :
8549 : /* The operand as an rvalue. */
8550 7299047 : tree val
8551 7299047 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8552 : non_constant_p, overflow_p,
8553 : jump_target);
8554 7299047 : if (*jump_target)
8555 : return NULL_TREE;
8556 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8557 : a local array in a constexpr function. */
8558 7299047 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8559 2602040 : if (!ptr)
8560 2602040 : VERIFY_CONSTANT (val);
8561 :
8562 : /* The modified value. */
8563 7246157 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8564 7246157 : tree mod;
8565 7246157 : if (INDIRECT_TYPE_P (type))
8566 : {
8567 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8568 4697007 : offset = convert_to_ptrofftype (offset);
8569 4697007 : if (!inc)
8570 61454 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8571 4697007 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8572 : }
8573 2549150 : else if (c_promoting_integer_type_p (type)
8574 39523 : && !TYPE_UNSIGNED (type)
8575 2549275 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8576 : {
8577 125 : offset = fold_convert (integer_type_node, offset);
8578 125 : mod = fold_convert (integer_type_node, val);
8579 155 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8580 : mod, offset);
8581 125 : mod = fold_convert (type, t);
8582 125 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8583 9 : TREE_OVERFLOW (mod) = false;
8584 : }
8585 : else
8586 2793612 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8587 7246157 : if (!ptr)
8588 2549150 : VERIFY_CONSTANT (mod);
8589 :
8590 : /* Storing the modified value. */
8591 11801384 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8592 : MODIFY_EXPR, type, op, mod);
8593 7246157 : mod = cxx_eval_constant_expression (ctx, store, lval,
8594 : non_constant_p, overflow_p,
8595 : jump_target);
8596 7246157 : ggc_free (store);
8597 7246157 : if (*jump_target)
8598 : return NULL_TREE;
8599 7246157 : if (*non_constant_p)
8600 : return t;
8601 :
8602 : /* And the value of the expression. */
8603 7201252 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8604 : /* Prefix ops are lvalues, but the caller might want an rvalue;
8605 : lval has already been taken into account in the store above. */
8606 : return mod;
8607 : else
8608 : /* Postfix ops are rvalues. */
8609 357872 : return val;
8610 : }
8611 :
8612 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8613 : STMT matches *jump_target. If we're looking for a case label and we see
8614 : the default label, note it in ctx->css_state. */
8615 :
8616 : static bool
8617 498810 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8618 : {
8619 498810 : switch (TREE_CODE (*jump_target))
8620 : {
8621 45 : case LABEL_DECL:
8622 45 : if (TREE_CODE (stmt) == LABEL_EXPR
8623 45 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8624 : return true;
8625 : break;
8626 :
8627 496848 : case INTEGER_CST:
8628 496848 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8629 : {
8630 496821 : gcc_assert (ctx->css_state != NULL);
8631 496821 : if (!CASE_LOW (stmt))
8632 : {
8633 : /* default: should appear just once in a SWITCH_EXPR
8634 : body (excluding nested SWITCH_EXPR). */
8635 74056 : gcc_assert (*ctx->css_state != css_default_seen);
8636 : /* When evaluating SWITCH_EXPR body for the second time,
8637 : return true for the default: label. */
8638 74056 : if (*ctx->css_state == css_default_processing)
8639 : return true;
8640 37040 : *ctx->css_state = css_default_seen;
8641 : }
8642 422765 : else if (CASE_HIGH (stmt))
8643 : {
8644 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8645 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8646 : return true;
8647 : }
8648 422745 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8649 : return true;
8650 : }
8651 : break;
8652 :
8653 : case BREAK_STMT:
8654 : case CONTINUE_STMT:
8655 : /* These two are handled directly in cxx_eval_loop_expr by testing
8656 : breaks (jump_target) or continues (jump_target). */
8657 : break;
8658 :
8659 : case VAR_DECL:
8660 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8661 : and other places by testing throws (jump_target). */
8662 : break;
8663 :
8664 0 : default:
8665 0 : gcc_unreachable ();
8666 : }
8667 : return false;
8668 : }
8669 :
8670 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8671 : semantics, for switch, break, continue, and return. */
8672 :
8673 : static tree
8674 36907413 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8675 : bool *non_constant_p, bool *overflow_p,
8676 : tree *jump_target)
8677 : {
8678 : /* In a statement-expression we want to return the last value.
8679 : For empty statement expression return void_node. */
8680 36907413 : tree r = void_node;
8681 95675479 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8682 : {
8683 77957380 : tree stmt = *i;
8684 :
8685 : /* We've found a continue, so skip everything until we reach
8686 : the label its jumping to. */
8687 77957380 : if (continues (jump_target))
8688 : {
8689 1962 : if (label_matches (ctx, jump_target, stmt))
8690 : /* Found it. */
8691 9 : *jump_target = NULL_TREE;
8692 : else
8693 1953 : continue;
8694 : }
8695 77955427 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8696 10228730 : continue;
8697 :
8698 67726697 : value_cat lval = vc_discard;
8699 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8700 96241810 : if (tsi_one_before_end_p (i)
8701 28516331 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8702 : lval = vc_prvalue;
8703 :
8704 67726697 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8705 : non_constant_p, overflow_p,
8706 : jump_target);
8707 67726695 : if (*non_constant_p)
8708 : break;
8709 59417914 : if (returns (jump_target)
8710 48624284 : || breaks (jump_target)
8711 58768066 : || throws (jump_target))
8712 : break;
8713 : }
8714 36907411 : return r;
8715 : }
8716 :
8717 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8718 : semantics; continue semantics are covered by cxx_eval_statement_list. */
8719 :
8720 : static tree
8721 3706302 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8722 : bool *non_constant_p, bool *overflow_p,
8723 : tree *jump_target)
8724 : {
8725 3706302 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8726 3706302 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8727 3706302 : unsigned cond_cleanup_depth = 0;
8728 3706302 : int count = 0;
8729 3706302 : switch (TREE_CODE (t))
8730 : {
8731 2948 : case LOOP_EXPR:
8732 2948 : body = LOOP_EXPR_BODY (t);
8733 2948 : break;
8734 2846927 : case DO_STMT:
8735 2846927 : body = DO_BODY (t);
8736 2846927 : cond = DO_COND (t);
8737 2846927 : break;
8738 308833 : case WHILE_STMT:
8739 308833 : body = WHILE_BODY (t);
8740 308833 : cond = WHILE_COND (t);
8741 308833 : cond_prep = WHILE_COND_PREP (t);
8742 308833 : cond_cleanup = WHILE_COND_CLEANUP (t);
8743 308833 : count = -1;
8744 308833 : break;
8745 547594 : case FOR_STMT:
8746 547594 : if (FOR_INIT_STMT (t))
8747 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8748 : non_constant_p, overflow_p, jump_target);
8749 547594 : if (*non_constant_p)
8750 : return NULL_TREE;
8751 547594 : body = FOR_BODY (t);
8752 547594 : cond = FOR_COND (t);
8753 547594 : expr = FOR_EXPR (t);
8754 547594 : cond_prep = FOR_COND_PREP (t);
8755 547594 : cond_cleanup = FOR_COND_CLEANUP (t);
8756 547594 : count = -1;
8757 547594 : break;
8758 0 : default:
8759 0 : gcc_unreachable ();
8760 : }
8761 3706302 : if (cond_prep)
8762 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8763 22017231 : auto cleanup_cond = [&] {
8764 : /* Clean up the condition variable after each iteration. */
8765 18310929 : if (cond_cleanup_depth && !*non_constant_p)
8766 : {
8767 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8768 105 : tree s = BIND_EXPR_BODY (cond_prep);
8769 105 : unsigned i;
8770 210 : for (i = cond_cleanup_depth; i; --i)
8771 : {
8772 105 : tree_stmt_iterator iter = tsi_last (s);
8773 105 : s = tsi_stmt (iter);
8774 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8775 105 : s = CLEANUP_BODY (s);
8776 : }
8777 105 : tree c;
8778 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8779 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8780 : overflow_p, jump_target);
8781 105 : }
8782 18310929 : if (cond_prep)
8783 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8784 222 : decl; decl = DECL_CHAIN (decl))
8785 111 : destroy_value_checked (ctx, decl, non_constant_p);
8786 3706302 : };
8787 15495868 : do
8788 : {
8789 15495868 : if (count != -1)
8790 : {
8791 14639441 : if (body)
8792 14639441 : cxx_eval_constant_expression (ctx, body, vc_discard,
8793 : non_constant_p, overflow_p,
8794 : jump_target);
8795 14639441 : if (breaks (jump_target))
8796 : {
8797 34814 : *jump_target = NULL_TREE;
8798 34814 : break;
8799 : }
8800 :
8801 14604627 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8802 570 : *jump_target = NULL_TREE;
8803 :
8804 14604627 : if (expr)
8805 4638651 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8806 : non_constant_p, overflow_p,
8807 : jump_target);
8808 14604627 : cleanup_cond ();
8809 : }
8810 :
8811 15461054 : if (cond_prep)
8812 : {
8813 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8814 222 : decl; decl = DECL_CHAIN (decl))
8815 111 : ctx->global->clear_value (decl);
8816 111 : if (cond_cleanup)
8817 : {
8818 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8819 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8820 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8821 : at the end of those STATEMENT_LISTs. */
8822 111 : cond_cleanup_depth = 0;
8823 111 : tree s = BIND_EXPR_BODY (cond_prep);
8824 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8825 222 : depth; --depth)
8826 : {
8827 111 : for (tree_stmt_iterator i = tsi_start (s);
8828 321 : !tsi_end_p (i); ++i)
8829 : {
8830 321 : tree stmt = *i;
8831 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8832 0 : continue;
8833 321 : if (tsi_one_before_end_p (i))
8834 : {
8835 : /* The last statement in the STATEMENT_LIST
8836 : has to be a CLEANUP_STMT (verified in
8837 : finish_loop_cond_prep). We want to
8838 : evaluate just its CLEANUP_BODY part but not
8839 : CLEANUP_EXPR part just yet. */
8840 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8841 : /* If the CLEANUP_STMT is not actually to be
8842 : evaluated, don't increment cond_cleanup_depth
8843 : so that we don't evaluate the CLEANUP_EXPR
8844 : for it later either. */
8845 105 : if (*jump_target)
8846 : {
8847 : depth = 1;
8848 : break;
8849 : }
8850 105 : ++cond_cleanup_depth;
8851 : /* If not in the innermost one, next iteration
8852 : will handle CLEANUP_BODY similarly. */
8853 105 : if (depth > 1)
8854 : {
8855 0 : s = CLEANUP_BODY (stmt);
8856 0 : break;
8857 : }
8858 : /* The innermost one can be evaluated normally. */
8859 105 : cxx_eval_constant_expression (ctx,
8860 105 : CLEANUP_BODY (stmt),
8861 : vc_discard,
8862 : non_constant_p,
8863 : overflow_p,
8864 : jump_target);
8865 105 : break;
8866 : }
8867 : /* And so should be evaluated statements which aren't
8868 : last in the STATEMENT_LIST. */
8869 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8870 : non_constant_p, overflow_p,
8871 : jump_target);
8872 216 : if (*non_constant_p
8873 210 : || returns (jump_target)
8874 210 : || breaks (jump_target)
8875 210 : || continues (jump_target)
8876 531 : || throws (jump_target))
8877 : {
8878 : depth = 1;
8879 : break;
8880 : }
8881 : }
8882 : }
8883 : }
8884 : else
8885 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8886 : vc_discard, non_constant_p,
8887 : overflow_p, jump_target);
8888 : }
8889 :
8890 15461054 : if (cond)
8891 : {
8892 15454560 : tree res
8893 15454560 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8894 : non_constant_p, overflow_p,
8895 : jump_target);
8896 15454560 : if (res)
8897 : {
8898 15405415 : if (verify_constant (res, ctx->quiet, non_constant_p,
8899 : overflow_p))
8900 : break;
8901 15277414 : if (integer_zerop (res))
8902 : break;
8903 : }
8904 : else
8905 49145 : gcc_assert (*jump_target);
8906 : }
8907 :
8908 11840327 : if (++count >= constexpr_loop_limit)
8909 : {
8910 27 : if (!ctx->quiet)
8911 9 : error_at (cp_expr_loc_or_input_loc (t),
8912 : "%<constexpr%> loop iteration count exceeds limit of %d "
8913 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8914 : constexpr_loop_limit);
8915 27 : *non_constant_p = true;
8916 27 : break;
8917 : }
8918 : }
8919 35470368 : while (!returns (jump_target)
8920 11789768 : && !breaks (jump_target)
8921 11789768 : && !continues (jump_target)
8922 11789861 : && (!switches (jump_target) || count == 0)
8923 11789738 : && !throws (jump_target)
8924 23680751 : && !*non_constant_p);
8925 :
8926 3706302 : cleanup_cond ();
8927 :
8928 3706302 : return NULL_TREE;
8929 : }
8930 :
8931 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8932 : semantics. */
8933 :
8934 : static tree
8935 49002 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8936 : bool *non_constant_p, bool *overflow_p,
8937 : tree *jump_target)
8938 : {
8939 49002 : tree cond
8940 49002 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8941 49002 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8942 : non_constant_p, overflow_p,
8943 : jump_target);
8944 49002 : if (*jump_target)
8945 : return NULL_TREE;
8946 49002 : VERIFY_CONSTANT (cond);
8947 48759 : if (TREE_CODE (cond) != INTEGER_CST)
8948 : {
8949 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8950 : switch condition even if it's constant enough for other things
8951 : (c++/113545). */
8952 0 : gcc_checking_assert (ctx->quiet);
8953 0 : *non_constant_p = true;
8954 0 : return t;
8955 : }
8956 :
8957 48759 : *jump_target = cond;
8958 :
8959 48759 : tree body
8960 48759 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8961 48759 : constexpr_ctx new_ctx = *ctx;
8962 48759 : constexpr_switch_state css = css_default_not_seen;
8963 48759 : new_ctx.css_state = &css;
8964 48759 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8965 : non_constant_p, overflow_p, jump_target);
8966 85814 : if (switches (jump_target) && css == css_default_seen)
8967 : {
8968 : /* If the SWITCH_EXPR body has default: label, process it once again,
8969 : this time instructing label_matches to return true for default:
8970 : label on switches (jump_target). */
8971 37016 : css = css_default_processing;
8972 37016 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8973 : non_constant_p, overflow_p, jump_target);
8974 : }
8975 48759 : if (breaks (jump_target) || switches (jump_target))
8976 21416 : *jump_target = NULL_TREE;
8977 : return NULL_TREE;
8978 : }
8979 :
8980 : /* Find the object of TYPE under initialization in CTX. */
8981 :
8982 : static tree
8983 16666 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8984 : {
8985 16666 : if (!ctx)
8986 : return NULL_TREE;
8987 :
8988 : /* Prefer the outermost matching object, but don't cross
8989 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8990 16291 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8991 553 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8992 : return outer_ob;
8993 :
8994 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8995 : can is a minor optimization. */
8996 16113 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8997 300 : return ctx->ctor;
8998 :
8999 15813 : if (!ctx->object)
9000 : return NULL_TREE;
9001 :
9002 : /* Since an object cannot have a field of its own type, we can search outward
9003 : from ctx->object to find the unique containing object of TYPE. */
9004 : tree ob = ctx->object;
9005 15804 : while (ob)
9006 : {
9007 15804 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
9008 : break;
9009 219 : if (handled_component_p (ob))
9010 217 : ob = TREE_OPERAND (ob, 0);
9011 : else
9012 : ob = NULL_TREE;
9013 : }
9014 :
9015 : return ob;
9016 : }
9017 :
9018 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
9019 : true, we're checking a constexpr function body. */
9020 :
9021 : static void
9022 35 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
9023 : {
9024 35 : auto_diagnostic_group d;
9025 35 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
9026 : "constant expression"))
9027 35 : inform (loc, "only unevaluated inline assembly is allowed in a "
9028 : "%<constexpr%> function in C++20");
9029 35 : }
9030 :
9031 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
9032 : context; maybe complain about that. */
9033 :
9034 : static void
9035 86237116 : maybe_warn_about_constant_value (location_t loc, tree decl)
9036 : {
9037 86237116 : static bool explained = false;
9038 86237116 : auto_diagnostic_group d;
9039 86237116 : if (cxx_dialect >= cxx17
9040 86074744 : && warn_interference_size
9041 86074744 : && !OPTION_SET_P (param_destruct_interfere_size)
9042 86074744 : && DECL_CONTEXT (decl) == std_node
9043 17559087 : && DECL_NAME (decl)
9044 17559081 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
9045 16 : && (LOCATION_FILE (input_location) != main_input_filename
9046 10 : || module_exporting_p ())
9047 86237119 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
9048 86237125 : && !explained)
9049 : {
9050 6 : explained = true;
9051 6 : inform (loc, "its value can vary between compiler versions or "
9052 : "with different %<-mtune%> or %<-mcpu%> flags");
9053 6 : inform (loc, "if this use is part of a public ABI, change it to "
9054 : "instead use a constant variable you define");
9055 6 : inform (loc, "the default value for the current CPU tuning "
9056 : "is %d bytes", param_destruct_interfere_size);
9057 6 : inform (loc, "you can stabilize this value with %<--param "
9058 : "hardware_destructive_interference_size=%d%>, or disable "
9059 : "this warning with %<-Wno-interference-size%>",
9060 : param_destruct_interfere_size);
9061 : }
9062 86237116 : }
9063 :
9064 : /* For element type ELT_TYPE, return the appropriate type of the heap object
9065 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
9066 : in bytes. If COOKIE_SIZE is NULL, return array type
9067 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
9068 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
9069 : where N is computed such that the size of the struct fits into FULL_SIZE.
9070 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
9071 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
9072 : will be also 0 and so it is not possible to determine the actual array
9073 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
9074 : expression evaluation of subexpressions of ARG_SIZE. */
9075 :
9076 : static tree
9077 72765 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9078 : tree cookie_size, tree full_size, tree arg_size,
9079 : bool *non_constant_p, bool *overflow_p,
9080 : tree *jump_target)
9081 : {
9082 72765 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9083 72765 : gcc_assert (tree_fits_uhwi_p (full_size));
9084 72765 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9085 72765 : if (arg_size)
9086 : {
9087 9 : STRIP_NOPS (arg_size);
9088 9 : if (cookie_size)
9089 : {
9090 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9091 : arg_size = NULL_TREE;
9092 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9093 0 : && tree_int_cst_equal (cookie_size,
9094 0 : TREE_OPERAND (arg_size, 0)))
9095 : {
9096 0 : arg_size = TREE_OPERAND (arg_size, 1);
9097 0 : STRIP_NOPS (arg_size);
9098 : }
9099 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9100 0 : && tree_int_cst_equal (cookie_size,
9101 0 : TREE_OPERAND (arg_size, 1)))
9102 : {
9103 0 : arg_size = TREE_OPERAND (arg_size, 0);
9104 0 : STRIP_NOPS (arg_size);
9105 : }
9106 : else
9107 : arg_size = NULL_TREE;
9108 : }
9109 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9110 : {
9111 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9112 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9113 9 : if (integer_zerop (op0))
9114 9 : arg_size
9115 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9116 : non_constant_p, overflow_p,
9117 : jump_target);
9118 0 : else if (integer_zerop (op1))
9119 0 : arg_size
9120 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9121 : non_constant_p, overflow_p,
9122 : jump_target);
9123 : else
9124 : arg_size = NULL_TREE;
9125 9 : if (*jump_target)
9126 : return NULL_TREE;
9127 : }
9128 : else
9129 : arg_size = NULL_TREE;
9130 : }
9131 :
9132 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9133 72765 : if (!arg_size)
9134 : {
9135 72756 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9136 72756 : gcc_assert (fsz >= csz);
9137 72756 : fsz -= csz;
9138 72756 : if (esz)
9139 72756 : fsz /= esz;
9140 : }
9141 72765 : tree itype2 = build_index_type (size_int (fsz - 1));
9142 72765 : if (!cookie_size)
9143 72756 : return build_cplus_array_type (elt_type, itype2);
9144 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9145 : }
9146 :
9147 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9148 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9149 : the expression which needed the cleanup threw. If both threw, diagnose
9150 : it and return NULL, otherwise return R. If only the cleanup threw, set
9151 : *JUMP_TARGET to the exception object from the cleanup. */
9152 :
9153 : static tree
9154 1067360 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9155 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9156 : {
9157 1067362 : if (!throws (&jmp_target))
9158 : return r;
9159 14 : if (throws (jump_target))
9160 : {
9161 : /* [except.throw]/9 - If the exception handling mechanism
9162 : handling an uncaught exception directly invokes a function
9163 : that exits via an exception, the function std::terminate is
9164 : invoked. */
9165 12 : if (!ctx->quiet)
9166 : {
9167 4 : auto_diagnostic_group d;
9168 4 : diagnose_std_terminate (loc, ctx, *jump_target);
9169 4 : inform (loc, "destructor exited with an exception");
9170 4 : }
9171 12 : *non_constant_p = true;
9172 12 : *jump_target = NULL_TREE;
9173 12 : return NULL_TREE;
9174 : }
9175 2 : *jump_target = jmp_target;
9176 2 : return r;
9177 : }
9178 :
9179 : /* Attempt to reduce the expression T to a constant value.
9180 : On failure, issue diagnostic and return error_mark_node. */
9181 : /* FIXME unify with c_fully_fold */
9182 : /* FIXME overflow_p is too global */
9183 :
9184 : tree
9185 2357786238 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9186 : value_cat lval,
9187 : bool *non_constant_p, bool *overflow_p,
9188 : tree *jump_target)
9189 : {
9190 2357786238 : if (*jump_target)
9191 : {
9192 : /* If we are jumping, ignore all statements/expressions except those
9193 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9194 1602077 : switch (TREE_CODE (t))
9195 : {
9196 : case BIND_EXPR:
9197 : case STATEMENT_LIST:
9198 : case LOOP_EXPR:
9199 : case COND_EXPR:
9200 : case IF_STMT:
9201 : case DO_STMT:
9202 : case WHILE_STMT:
9203 : case FOR_STMT:
9204 : break;
9205 496848 : case LABEL_EXPR:
9206 496848 : case CASE_LABEL_EXPR:
9207 496848 : if (label_matches (ctx, jump_target, t))
9208 : /* Found it. */
9209 48720 : *jump_target = NULL_TREE;
9210 496848 : return NULL_TREE;
9211 : default:
9212 : return NULL_TREE;
9213 : }
9214 : }
9215 2356427141 : if (error_operand_p (t))
9216 : {
9217 138 : *non_constant_p = true;
9218 138 : return t;
9219 : }
9220 :
9221 : /* Change the input location to the currently processed expression for
9222 : better error messages when a subexpression has no location. */
9223 2356427003 : location_t loc = cp_expr_loc_or_input_loc (t);
9224 4712848593 : iloc_sentinel sentinel (loc);
9225 :
9226 2356427003 : STRIP_ANY_LOCATION_WRAPPER (t);
9227 :
9228 2356427003 : if (CONSTANT_CLASS_P (t))
9229 : {
9230 401258098 : if (TREE_OVERFLOW (t))
9231 : {
9232 62 : if (!ctx->quiet)
9233 15 : permerror (input_location, "overflow in constant expression");
9234 62 : if (!flag_permissive || ctx->quiet)
9235 59 : *overflow_p = true;
9236 : }
9237 :
9238 401258098 : if (TREE_CODE (t) == INTEGER_CST
9239 384880547 : && TYPE_PTR_P (TREE_TYPE (t))
9240 : /* INTEGER_CST with pointer-to-method type is only used
9241 : for a virtual method in a pointer to member function.
9242 : Don't reject those. */
9243 1627892 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9244 402885713 : && !integer_zerop (t))
9245 : {
9246 5 : if (!ctx->quiet)
9247 0 : error ("value %qE of type %qT is not a constant expression",
9248 0 : t, TREE_TYPE (t));
9249 5 : *non_constant_p = true;
9250 : }
9251 :
9252 401258098 : return t;
9253 : }
9254 :
9255 : /* Avoid excessively long constexpr evaluations. */
9256 1955168905 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9257 : {
9258 9 : if (!ctx->quiet)
9259 3 : error_at (loc,
9260 : "%<constexpr%> evaluation operation count exceeds limit of "
9261 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9262 : constexpr_ops_limit);
9263 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9264 9 : *non_constant_p = true;
9265 9 : return t;
9266 : }
9267 :
9268 1955168896 : constexpr_ctx new_ctx;
9269 1955168896 : tree r = t;
9270 :
9271 1955168896 : tree_code tcode = TREE_CODE (t);
9272 1955168896 : switch (tcode)
9273 : {
9274 41429300 : case RESULT_DECL:
9275 41429300 : if (lval)
9276 : return t;
9277 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9278 : through an invisible reference, or in named return value
9279 : optimization. */
9280 43106 : if (tree v = ctx->global->get_value (t))
9281 : return v;
9282 : else
9283 : {
9284 3 : if (!ctx->quiet)
9285 0 : error ("%qE is not a constant expression", t);
9286 3 : *non_constant_p = true;
9287 : }
9288 3 : break;
9289 :
9290 248834582 : case VAR_DECL:
9291 248834582 : if (DECL_HAS_VALUE_EXPR_P (t))
9292 : {
9293 4860802 : if (is_normal_capture_proxy (t)
9294 4860802 : && current_function_decl == DECL_CONTEXT (t))
9295 : {
9296 : /* Function parms aren't constexpr within the function
9297 : definition, so don't try to look at the closure. But if the
9298 : captured variable is constant, try to evaluate it directly. */
9299 334855 : r = DECL_CAPTURED_VARIABLE (t);
9300 : }
9301 : else
9302 4525947 : r = DECL_VALUE_EXPR (t);
9303 :
9304 4860802 : tree type = TREE_TYPE (t);
9305 4860802 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
9306 : {
9307 : /* Adjust r to match the reference-ness of t. */
9308 152449 : if (TYPE_REF_P (type))
9309 150148 : r = build_address (r);
9310 : else
9311 2301 : r = convert_from_reference (r);
9312 : }
9313 4860802 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
9314 4860802 : overflow_p, jump_target);
9315 : }
9316 : /* fall through */
9317 254053093 : case CONST_DECL:
9318 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
9319 : CONST_DECL for aggregate constants. */
9320 254053093 : if (lval)
9321 : return t;
9322 186532401 : else if (t == ctx->object)
9323 964559 : return ctx->ctor;
9324 185567842 : if (VAR_P (t))
9325 : {
9326 175488529 : if (tree v = ctx->global->get_value (t))
9327 : {
9328 : r = v;
9329 : break;
9330 : }
9331 123746915 : if (ctx->global->is_outside_lifetime (t))
9332 : {
9333 107 : if (!ctx->quiet)
9334 31 : outside_lifetime_error (loc, t);
9335 107 : *non_constant_p = true;
9336 107 : break;
9337 : }
9338 : }
9339 133826121 : if (ctx->manifestly_const_eval == mce_true)
9340 86237116 : maybe_warn_about_constant_value (loc, t);
9341 133826121 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
9342 133826121 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9343 : {
9344 : /* If the class is empty, we aren't actually loading anything. */
9345 139497 : r = build_constructor (TREE_TYPE (t), NULL);
9346 139497 : TREE_CONSTANT (r) = true;
9347 : }
9348 133686624 : else if (ctx->strict)
9349 133356999 : r = decl_really_constant_value (t, /*unshare_p=*/false);
9350 : else
9351 329625 : r = decl_constant_value (t, /*unshare_p=*/false);
9352 133823424 : if (TREE_CODE (r) == TARGET_EXPR
9353 133823424 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9354 0 : r = TARGET_EXPR_INITIAL (r);
9355 133823424 : if (DECL_P (r)
9356 : /* P2280 allows references to unknown. */
9357 134090983 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
9358 : {
9359 28704801 : if (!ctx->quiet)
9360 183 : non_const_var_error (loc, r, /*fundef_p*/false);
9361 28704801 : *non_constant_p = true;
9362 : }
9363 : break;
9364 :
9365 : case DEBUG_BEGIN_STMT:
9366 : /* ??? It might be nice to retain this information somehow, so
9367 : as to be able to step into a constexpr function call. */
9368 : /* Fall through. */
9369 :
9370 : case FUNCTION_DECL:
9371 : case TEMPLATE_DECL:
9372 : case LABEL_DECL:
9373 : case LABEL_EXPR:
9374 : case CASE_LABEL_EXPR:
9375 : case PREDICT_EXPR:
9376 : case OMP_DECLARE_MAPPER:
9377 : case REFLECT_EXPR:
9378 : return t;
9379 :
9380 224813448 : case PARM_DECL:
9381 224813448 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9382 : {
9383 : /* glvalue use. */
9384 15522060 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9385 144008 : if (tree v = ctx->global->get_value (t))
9386 1574990384 : r = v;
9387 : }
9388 209291388 : else if (tree v = ctx->global->get_value (t))
9389 : {
9390 119184723 : r = v;
9391 119184723 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9392 5375 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9393 : non_constant_p, overflow_p,
9394 : jump_target);
9395 119184723 : if (*jump_target)
9396 : return NULL_TREE;
9397 : }
9398 90106665 : else if (lval)
9399 : /* Defer in case this is only used for its type. */;
9400 90106665 : else if (ctx->global->is_outside_lifetime (t))
9401 : {
9402 18 : if (!ctx->quiet)
9403 6 : outside_lifetime_error (loc, t);
9404 18 : *non_constant_p = true;
9405 18 : break;
9406 : }
9407 90106647 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9408 90106647 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9409 : {
9410 : /* If the class is empty, we aren't actually loading anything. */
9411 25344 : r = build_constructor (TREE_TYPE (t), NULL);
9412 25344 : TREE_CONSTANT (r) = true;
9413 : }
9414 90081303 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9415 : /* P2280 allows references to unknown... */;
9416 89821303 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9417 : /* ...as well as the this pointer. */;
9418 : else
9419 : {
9420 89396201 : if (!ctx->quiet)
9421 196 : error ("%qE is not a constant expression", t);
9422 89396201 : *non_constant_p = true;
9423 : }
9424 : break;
9425 :
9426 156084973 : case CALL_EXPR:
9427 156084973 : case AGGR_INIT_EXPR:
9428 156084973 : r = cxx_eval_call_expression (ctx, t, lval,
9429 : non_constant_p, overflow_p, jump_target);
9430 156084973 : break;
9431 :
9432 15603468 : case DECL_EXPR:
9433 15603468 : {
9434 15603468 : r = DECL_EXPR_DECL (t);
9435 15603468 : if (TREE_CODE (r) == USING_DECL)
9436 : {
9437 9025 : r = void_node;
9438 9025 : break;
9439 : }
9440 :
9441 15594443 : if (VAR_P (r)
9442 15594443 : && (TREE_STATIC (r)
9443 15462140 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9444 : /* Allow __FUNCTION__ etc. */
9445 132303 : && !DECL_ARTIFICIAL (r)
9446 15595221 : && !decl_constant_var_p (r))
9447 : {
9448 7 : if (!ctx->quiet)
9449 : {
9450 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9451 1 : error_at (loc, "control passes through definition of %qD "
9452 : "with thread storage duration", r);
9453 : else
9454 1 : error_at (loc, "control passes through definition of %qD "
9455 : "with static storage duration", r);
9456 : }
9457 7 : *non_constant_p = true;
9458 7 : break;
9459 : }
9460 :
9461 : /* make_rtl_for_nonlocal_decl could have deferred emission of
9462 : a local static var, but if it appears in a statement expression
9463 : which is constant expression evaluated to e.g. just the address
9464 : of the variable, its DECL_EXPR will never be seen during
9465 : gimple lowering's record_vars_into as the statement expression
9466 : will not be in the IL at all. */
9467 15594436 : if (VAR_P (r)
9468 15594436 : && TREE_STATIC (r)
9469 132296 : && !DECL_REALLY_EXTERN (r)
9470 132274 : && DECL_FUNCTION_SCOPE_P (r)
9471 132274 : && !var_in_maybe_constexpr_fn (r)
9472 15594439 : && decl_constant_var_p (r))
9473 : {
9474 3 : varpool_node *node = varpool_node::get (r);
9475 3 : if (node == NULL || !node->definition)
9476 3 : rest_of_decl_compilation (r, 0, at_eof);
9477 : }
9478 :
9479 31018948 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9480 29421858 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9481 : {
9482 1768120 : new_ctx = *ctx;
9483 1768120 : new_ctx.object = r;
9484 1768120 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9485 1768120 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9486 1768120 : ctx->global->put_value (r, new_ctx.ctor);
9487 1768120 : ctx = &new_ctx;
9488 : }
9489 :
9490 15594436 : if (tree init = DECL_INITIAL (r))
9491 : {
9492 9234666 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9493 : non_constant_p, overflow_p,
9494 : jump_target);
9495 9234666 : if (*jump_target)
9496 : return NULL_TREE;
9497 : /* Don't share a CONSTRUCTOR that might be changed. */
9498 9234666 : init = unshare_constructor (init);
9499 : /* Remember that a constant object's constructor has already
9500 : run. */
9501 18469332 : if (CLASS_TYPE_P (TREE_TYPE (r))
9502 9574870 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9503 36053 : TREE_READONLY (init) = true;
9504 9234666 : ctx->global->put_value (r, init);
9505 : }
9506 6359770 : else if (ctx == &new_ctx)
9507 : /* We gave it a CONSTRUCTOR above. */;
9508 : else
9509 4967175 : ctx->global->put_value (r, NULL_TREE);
9510 : }
9511 : break;
9512 :
9513 20038832 : case TARGET_EXPR:
9514 20038832 : {
9515 20038832 : tree type = TREE_TYPE (t);
9516 :
9517 20038832 : if (!literal_type_p (type))
9518 : {
9519 5157 : if (!ctx->quiet)
9520 : {
9521 0 : auto_diagnostic_group d;
9522 0 : error ("temporary of non-literal type %qT in a "
9523 : "constant expression", type);
9524 0 : explain_non_literal_class (type);
9525 0 : }
9526 5157 : *non_constant_p = true;
9527 8039940 : break;
9528 : }
9529 20033675 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9530 : /* Avoid evaluating a TARGET_EXPR more than once. */
9531 20033675 : tree slot = TARGET_EXPR_SLOT (t);
9532 20033675 : if (tree v = ctx->global->get_value (slot))
9533 : {
9534 1985 : if (lval)
9535 8881377 : return slot;
9536 : r = v;
9537 : break;
9538 : }
9539 20031690 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9540 : {
9541 : /* We're being expanded without an explicit target, so start
9542 : initializing a new object; expansion with an explicit target
9543 : strips the TARGET_EXPR before we get here. */
9544 14942214 : new_ctx = *ctx;
9545 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9546 : any PLACEHOLDER_EXPR within the initializer that refers to the
9547 : former object under construction. */
9548 14942214 : new_ctx.parent = ctx;
9549 14942214 : new_ctx.ctor = build_constructor (type, NULL);
9550 14942214 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9551 14942214 : new_ctx.object = slot;
9552 14942214 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9553 14942214 : ctx = &new_ctx;
9554 : }
9555 :
9556 : /* If the initializer is complex, evaluate it to initialize slot. */
9557 20031690 : bool is_complex = target_expr_needs_replace (t);
9558 20031690 : if (is_complex)
9559 : /* In case no initialization actually happens, clear out any
9560 : void_node from a previous evaluation. */
9561 473 : ctx->global->put_value (slot, NULL_TREE);
9562 :
9563 : /* Pass vc_prvalue because this indicates
9564 : initialization of a temporary. */
9565 20031690 : r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t),
9566 : vc_prvalue, non_constant_p,
9567 : overflow_p, jump_target);
9568 20031690 : if (*non_constant_p)
9569 : break;
9570 11997932 : if (ctx->save_exprs)
9571 7497444 : ctx->save_exprs->safe_push (slot);
9572 11997932 : if (*jump_target)
9573 : {
9574 437 : if (!is_complex
9575 437 : && !(AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9576 : /* If TARGET_EXPR_INITIAL throws exception and slot's value
9577 : has not been changed yet, CLEANUP_POINT_EXPR handling
9578 : could see there void_node from a previous evaluation
9579 : and complain. */
9580 6 : ctx->global->put_value (slot, NULL_TREE);
9581 437 : return NULL_TREE;
9582 : }
9583 11997495 : if (!is_complex)
9584 : {
9585 11997092 : r = unshare_constructor (r);
9586 : /* Adjust the type of the result to the type of the temporary. */
9587 11997092 : r = adjust_temp_type (type, r);
9588 11997092 : ctx->global->put_value (slot, r);
9589 : }
9590 11997495 : if (TARGET_EXPR_CLEANUP (t)
9591 11997495 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9592 : {
9593 1199059 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9594 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9595 : them. */
9596 1199059 : if (CLEANUP_EH_ONLY (t))
9597 1388 : ctx->global->cleanups->safe_push (NULL_TREE);
9598 : }
9599 11997495 : if (lval)
9600 : return slot;
9601 3117515 : if (is_complex)
9602 0 : r = ctx->global->get_value (slot);
9603 : }
9604 3117515 : break;
9605 :
9606 78581746 : case INIT_EXPR:
9607 78581746 : case MODIFY_EXPR:
9608 78581746 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9609 78581746 : r = cxx_eval_store_expression (ctx, t, lval,
9610 : non_constant_p, overflow_p, jump_target);
9611 78581746 : break;
9612 :
9613 0 : case SCOPE_REF:
9614 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9615 : lval,
9616 : non_constant_p, overflow_p,
9617 : jump_target);
9618 0 : break;
9619 :
9620 47545443 : case RETURN_EXPR:
9621 47545443 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9622 47313055 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9623 : lval,
9624 : non_constant_p, overflow_p,
9625 : jump_target);
9626 47545441 : if (!throws (jump_target))
9627 47545203 : *jump_target = t;
9628 : break;
9629 53855 : case BREAK_STMT:
9630 53855 : case CONTINUE_STMT:
9631 53855 : *jump_target = t;
9632 53855 : break;
9633 :
9634 610776 : case SAVE_EXPR:
9635 : /* Avoid evaluating a SAVE_EXPR more than once. */
9636 610776 : if (tree v = ctx->global->get_value (t))
9637 : r = v;
9638 : else
9639 : {
9640 598600 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9641 : vc_prvalue, non_constant_p,
9642 : overflow_p, jump_target);
9643 598600 : if (*non_constant_p || *jump_target)
9644 : break;
9645 9826 : ctx->global->put_value (t, r);
9646 9826 : if (ctx->save_exprs)
9647 6324 : ctx->save_exprs->safe_push (t);
9648 : }
9649 : break;
9650 :
9651 48100283 : case NON_LVALUE_EXPR:
9652 48100283 : case EXPR_STMT:
9653 48100283 : case EH_SPEC_BLOCK:
9654 48100283 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9655 : lval,
9656 : non_constant_p, overflow_p,
9657 : jump_target);
9658 48100283 : break;
9659 :
9660 3962 : case TRY_BLOCK:
9661 3962 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9662 : non_constant_p, overflow_p,
9663 : jump_target);
9664 3962 : if (!*non_constant_p && throws (jump_target))
9665 3206 : if (tree h = TRY_HANDLERS (t))
9666 : {
9667 3206 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9668 3206 : if (TREE_CODE (h) == STATEMENT_LIST)
9669 : {
9670 923 : for (tree stmt : tsi_range (h))
9671 889 : if (TREE_CODE (stmt) == HANDLER
9672 889 : && handler_match_for_exception_type (stmt, type))
9673 : {
9674 : h = stmt;
9675 : break;
9676 : }
9677 449 : if (TREE_CODE (h) == STATEMENT_LIST)
9678 : h = NULL_TREE;
9679 : }
9680 2757 : else if (TREE_CODE (h) != HANDLER
9681 2757 : || !handler_match_for_exception_type (h, type))
9682 : h = NULL_TREE;
9683 : if (h)
9684 : {
9685 3172 : gcc_assert (VAR_P (*jump_target));
9686 3172 : ctx->global->caught_exceptions.safe_push (*jump_target);
9687 3172 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9688 3172 : *jump_target = NULL_TREE;
9689 3172 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9690 : vc_discard, non_constant_p,
9691 : overflow_p, jump_target);
9692 : }
9693 : }
9694 : break;
9695 :
9696 69028230 : case CLEANUP_POINT_EXPR:
9697 69028230 : {
9698 69028230 : auto_vec<tree, 2> cleanups;
9699 69028230 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9700 69028230 : ctx->global->cleanups = &cleanups;
9701 :
9702 69028230 : auto_vec<tree, 10> save_exprs;
9703 69028230 : constexpr_ctx new_ctx = *ctx;
9704 69028230 : new_ctx.save_exprs = &save_exprs;
9705 :
9706 69028230 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9707 : lval,
9708 : non_constant_p, overflow_p,
9709 : jump_target);
9710 :
9711 69028229 : ctx->global->cleanups = prev_cleanups;
9712 69028229 : unsigned int i;
9713 69028229 : tree cleanup, jmp_target = NULL_TREE;
9714 69028229 : bool eh = throws (jump_target);
9715 : /* Evaluate the cleanups. */
9716 139168541 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9717 1112083 : if (cleanup == NULL_TREE)
9718 : {
9719 : /* NULL_TREE cleanup is a marker that before it is
9720 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9721 : if the body didn't throw. */
9722 1365 : if (!eh)
9723 1363 : --i;
9724 : }
9725 : else
9726 1110718 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9727 : non_constant_p, overflow_p,
9728 : &jmp_target);
9729 :
9730 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9731 : full-expression. */
9732 214588455 : for (tree save_expr : save_exprs)
9733 7503768 : destroy_value_checked (ctx, save_expr, non_constant_p);
9734 69028229 : if (throws (&jmp_target))
9735 0 : *jump_target = jmp_target;
9736 69028229 : }
9737 69028229 : break;
9738 :
9739 43388404 : case MUST_NOT_THROW_EXPR:
9740 43388404 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9741 : lval,
9742 : non_constant_p, overflow_p,
9743 : jump_target);
9744 43388403 : if (throws (jump_target))
9745 : {
9746 : /* [except.handle]/7 - If the search for a handler exits the
9747 : function body of a function with a non-throwing exception
9748 : specification, the function std::terminate is invoked. */
9749 54 : if (!ctx->quiet)
9750 : {
9751 18 : auto_diagnostic_group d;
9752 18 : diagnose_std_terminate (loc, ctx, *jump_target);
9753 18 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9754 14 : && ctx->call
9755 32 : && ctx->call->fundef)
9756 14 : inform (loc, "uncaught exception exited from %<noexcept%> "
9757 : "function %qD",
9758 : ctx->call->fundef->decl);
9759 4 : else if (MUST_NOT_THROW_THROW_P (t))
9760 2 : inform (loc, "destructor exited with an exception after "
9761 : "initializing the exception object");
9762 2 : else if (MUST_NOT_THROW_CATCH_P (t))
9763 2 : inform (loc, "constructor exited with another exception while "
9764 : "entering handler");
9765 18 : }
9766 54 : *non_constant_p = true;
9767 54 : *jump_target = NULL_TREE;
9768 54 : r = NULL_TREE;
9769 : }
9770 : break;
9771 :
9772 0 : case TRY_CATCH_EXPR:
9773 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9774 : {
9775 0 : r = void_node;
9776 0 : break;
9777 : }
9778 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9779 : non_constant_p, overflow_p,
9780 : jump_target);
9781 0 : if (!*non_constant_p && throws (jump_target))
9782 : {
9783 0 : tree jmp_target = NULL_TREE;
9784 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9785 : non_constant_p, overflow_p,
9786 : &jmp_target);
9787 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9788 : jmp_target);
9789 : }
9790 : break;
9791 :
9792 671 : case TRY_FINALLY_EXPR:
9793 671 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9794 : non_constant_p, overflow_p,
9795 : jump_target);
9796 671 : if (!*non_constant_p)
9797 : {
9798 656 : tree jmp_target = NULL_TREE;
9799 : /* Also evaluate the cleanup. */
9800 656 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9801 656 : && throws (jump_target))
9802 0 : cxx_eval_constant_expression (ctx,
9803 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9804 : 1), vc_discard,
9805 : non_constant_p, overflow_p,
9806 : &jmp_target);
9807 : else
9808 656 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9809 : non_constant_p, overflow_p,
9810 : &jmp_target);
9811 656 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9812 : jmp_target);
9813 : }
9814 : break;
9815 :
9816 0 : case EH_ELSE_EXPR:
9817 : /* Evaluate any cleanup that applies to non-EH exits. */
9818 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9819 : non_constant_p, overflow_p,
9820 : jump_target);
9821 :
9822 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9823 0 : break;
9824 :
9825 2524729 : case CLEANUP_STMT:
9826 2524729 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9827 : non_constant_p, overflow_p,
9828 : jump_target);
9829 2524728 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9830 : {
9831 1066704 : iloc_sentinel ils (loc);
9832 1066704 : tree jmp_target = NULL_TREE;
9833 : /* Also evaluate the cleanup. */
9834 1066704 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9835 : non_constant_p, overflow_p,
9836 : &jmp_target);
9837 1066704 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9838 : jmp_target);
9839 1066704 : }
9840 : break;
9841 :
9842 : /* These differ from cxx_eval_unary_expression in that this doesn't
9843 : check for a constant operand or result; an address can be
9844 : constant without its operand being, and vice versa. */
9845 74893256 : case MEM_REF:
9846 74893256 : case INDIRECT_REF:
9847 74893256 : r = cxx_eval_indirect_ref (ctx, t, lval,
9848 : non_constant_p, overflow_p,
9849 : jump_target);
9850 74893256 : break;
9851 :
9852 75249899 : case ADDR_EXPR:
9853 75249899 : {
9854 75249899 : tree oldop = TREE_OPERAND (t, 0);
9855 75249899 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9856 : non_constant_p, overflow_p,
9857 : jump_target);
9858 75249899 : if (*jump_target)
9859 : return NULL_TREE;
9860 : /* Don't VERIFY_CONSTANT here. */
9861 75249894 : if (*non_constant_p)
9862 : return t;
9863 60189513 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9864 : /* This function does more aggressive folding than fold itself. */
9865 60189513 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9866 60189513 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9867 : {
9868 42457343 : ggc_free (r);
9869 42457343 : return t;
9870 : }
9871 : break;
9872 : }
9873 :
9874 479733 : case REALPART_EXPR:
9875 479733 : case IMAGPART_EXPR:
9876 479733 : if (lval)
9877 : {
9878 614 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9879 : non_constant_p, overflow_p,
9880 : jump_target);
9881 614 : if (*jump_target)
9882 : return NULL_TREE;
9883 614 : if (r == error_mark_node)
9884 : ;
9885 614 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9886 : r = t;
9887 : else
9888 568 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9889 : break;
9890 : }
9891 : /* FALLTHRU */
9892 22513644 : case CONJ_EXPR:
9893 22513644 : case FIX_TRUNC_EXPR:
9894 22513644 : case FLOAT_EXPR:
9895 22513644 : case NEGATE_EXPR:
9896 22513644 : case ABS_EXPR:
9897 22513644 : case ABSU_EXPR:
9898 22513644 : case BIT_NOT_EXPR:
9899 22513644 : case TRUTH_NOT_EXPR:
9900 22513644 : case FIXED_CONVERT_EXPR:
9901 22513644 : case VEC_DUPLICATE_EXPR:
9902 22513644 : r = cxx_eval_unary_expression (ctx, t, lval,
9903 : non_constant_p, overflow_p,
9904 : jump_target);
9905 22513644 : break;
9906 :
9907 8665357 : case SIZEOF_EXPR:
9908 8665357 : r = fold_sizeof_expr (t);
9909 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9910 : which could lead to an infinite recursion. */
9911 8665357 : if (TREE_CODE (r) != SIZEOF_EXPR)
9912 8665357 : r = cxx_eval_constant_expression (ctx, r, lval,
9913 : non_constant_p, overflow_p,
9914 : jump_target);
9915 : else
9916 : {
9917 0 : *non_constant_p = true;
9918 0 : gcc_assert (ctx->quiet);
9919 : }
9920 :
9921 : break;
9922 :
9923 9471267 : case COMPOUND_EXPR:
9924 9471267 : {
9925 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9926 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9927 : introduced by build_call_a. */
9928 9471267 : tree op0 = TREE_OPERAND (t, 0);
9929 9471267 : tree op1 = TREE_OPERAND (t, 1);
9930 9471267 : STRIP_NOPS (op1);
9931 3146499 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9932 11444611 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9933 4124769 : r = cxx_eval_constant_expression (ctx, op0,
9934 : lval, non_constant_p, overflow_p,
9935 : jump_target);
9936 : else
9937 : {
9938 : /* Check that the LHS is constant and then discard it. */
9939 5346498 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9940 : non_constant_p, overflow_p,
9941 : jump_target);
9942 5346498 : if (*jump_target)
9943 : return NULL_TREE;
9944 5340209 : if (*non_constant_p)
9945 : return t;
9946 5096620 : op1 = TREE_OPERAND (t, 1);
9947 5096620 : r = cxx_eval_constant_expression (ctx, op1,
9948 : lval, non_constant_p, overflow_p,
9949 : jump_target);
9950 : }
9951 : }
9952 : break;
9953 :
9954 93212473 : case POINTER_PLUS_EXPR:
9955 93212473 : case POINTER_DIFF_EXPR:
9956 93212473 : case PLUS_EXPR:
9957 93212473 : case MINUS_EXPR:
9958 93212473 : case MULT_EXPR:
9959 93212473 : case TRUNC_DIV_EXPR:
9960 93212473 : case CEIL_DIV_EXPR:
9961 93212473 : case FLOOR_DIV_EXPR:
9962 93212473 : case ROUND_DIV_EXPR:
9963 93212473 : case TRUNC_MOD_EXPR:
9964 93212473 : case CEIL_MOD_EXPR:
9965 93212473 : case ROUND_MOD_EXPR:
9966 93212473 : case RDIV_EXPR:
9967 93212473 : case EXACT_DIV_EXPR:
9968 93212473 : case MIN_EXPR:
9969 93212473 : case MAX_EXPR:
9970 93212473 : case LSHIFT_EXPR:
9971 93212473 : case RSHIFT_EXPR:
9972 93212473 : case LROTATE_EXPR:
9973 93212473 : case RROTATE_EXPR:
9974 93212473 : case BIT_IOR_EXPR:
9975 93212473 : case BIT_XOR_EXPR:
9976 93212473 : case BIT_AND_EXPR:
9977 93212473 : case TRUTH_XOR_EXPR:
9978 93212473 : case LT_EXPR:
9979 93212473 : case LE_EXPR:
9980 93212473 : case GT_EXPR:
9981 93212473 : case GE_EXPR:
9982 93212473 : case EQ_EXPR:
9983 93212473 : case NE_EXPR:
9984 93212473 : case SPACESHIP_EXPR:
9985 93212473 : case UNORDERED_EXPR:
9986 93212473 : case ORDERED_EXPR:
9987 93212473 : case UNLT_EXPR:
9988 93212473 : case UNLE_EXPR:
9989 93212473 : case UNGT_EXPR:
9990 93212473 : case UNGE_EXPR:
9991 93212473 : case UNEQ_EXPR:
9992 93212473 : case LTGT_EXPR:
9993 93212473 : case RANGE_EXPR:
9994 93212473 : case COMPLEX_EXPR:
9995 93212473 : r = cxx_eval_binary_expression (ctx, t, lval,
9996 : non_constant_p, overflow_p,
9997 : jump_target);
9998 93212473 : break;
9999 :
10000 : /* fold can introduce non-IF versions of these; still treat them as
10001 : short-circuiting. */
10002 11793423 : case TRUTH_AND_EXPR:
10003 11793423 : case TRUTH_ANDIF_EXPR:
10004 11793423 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
10005 : boolean_true_node,
10006 : non_constant_p, overflow_p,
10007 : jump_target);
10008 11793423 : break;
10009 :
10010 2395380 : case TRUTH_OR_EXPR:
10011 2395380 : case TRUTH_ORIF_EXPR:
10012 2395380 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
10013 : boolean_false_node,
10014 : non_constant_p, overflow_p,
10015 : jump_target);
10016 2395380 : break;
10017 :
10018 12228363 : case ARRAY_REF:
10019 12228363 : r = cxx_eval_array_reference (ctx, t, lval,
10020 : non_constant_p, overflow_p,
10021 : jump_target);
10022 12228363 : break;
10023 :
10024 78828421 : case COMPONENT_REF:
10025 78828421 : if (is_overloaded_fn (t))
10026 : {
10027 : /* We can only get here in checking mode via
10028 : build_non_dependent_expr, because any expression that
10029 : calls or takes the address of the function will have
10030 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
10031 6 : gcc_checking_assert (ctx->quiet || errorcount);
10032 6 : *non_constant_p = true;
10033 6 : return t;
10034 : }
10035 78828415 : r = cxx_eval_component_reference (ctx, t, lval,
10036 : non_constant_p, overflow_p,
10037 : jump_target);
10038 78828415 : break;
10039 :
10040 15766 : case BIT_FIELD_REF:
10041 15766 : r = cxx_eval_bit_field_ref (ctx, t, lval,
10042 : non_constant_p, overflow_p,
10043 : jump_target);
10044 15766 : break;
10045 :
10046 28133681 : case COND_EXPR:
10047 28133681 : case IF_STMT:
10048 28133681 : if (*jump_target)
10049 : {
10050 151492 : tree orig_jump = *jump_target;
10051 151492 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
10052 302984 : ? TREE_OPERAND (t, 1) : void_node);
10053 : /* When jumping to a label, the label might be either in the
10054 : then or else blocks, so process then block first in skipping
10055 : mode first, and if we are still in the skipping mode at its end,
10056 : process the else block too. */
10057 151492 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10058 : overflow_p, jump_target);
10059 : /* It's possible that we found the label in the then block. But
10060 : it could have been followed by another jumping statement, e.g.
10061 : say we're looking for case 1:
10062 : if (cond)
10063 : {
10064 : // skipped statements
10065 : case 1:; // clears up *jump_target
10066 : return 1; // and sets it to a RETURN_EXPR
10067 : }
10068 : else { ... }
10069 : in which case we need not go looking to the else block.
10070 : (goto is not allowed in a constexpr function.) */
10071 151492 : if (*jump_target == orig_jump)
10072 : {
10073 151549 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
10074 151549 : ? TREE_OPERAND (t, 2) : void_node);
10075 151454 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10076 : overflow_p, jump_target);
10077 : }
10078 : break;
10079 : }
10080 27982189 : r = cxx_eval_conditional_expression (ctx, t, lval,
10081 : non_constant_p, overflow_p,
10082 : jump_target);
10083 27982189 : break;
10084 4582 : case VEC_COND_EXPR:
10085 4582 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10086 : overflow_p, jump_target);
10087 4582 : break;
10088 :
10089 19642027 : case CONSTRUCTOR:
10090 19642027 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10091 : {
10092 : /* Don't re-process a constant CONSTRUCTOR. */
10093 17226758 : verify_constructor_flags (t);
10094 17226758 : if (TREE_CONSTANT (t))
10095 : return t;
10096 : }
10097 2415269 : if (TREE_CLOBBER_P (t))
10098 : {
10099 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10100 : a clobber by itself isn't a constant-expression. */
10101 0 : gcc_assert (ctx->quiet);
10102 0 : *non_constant_p = true;
10103 0 : break;
10104 : }
10105 2415269 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10106 : non_constant_p, overflow_p, jump_target);
10107 2415269 : break;
10108 :
10109 556 : case VEC_INIT_EXPR:
10110 : /* We can get this in a defaulted constructor for a class with a
10111 : non-static data member of array type. Either the initializer will
10112 : be NULL, meaning default-initialization, or it will be an lvalue
10113 : or xvalue of the same type, meaning direct-initialization from the
10114 : corresponding member. */
10115 556 : r = cxx_eval_vec_init (ctx, t, lval,
10116 : non_constant_p, overflow_p, jump_target);
10117 556 : break;
10118 :
10119 14887 : case VEC_PERM_EXPR:
10120 14887 : r = cxx_eval_trinary_expression (ctx, t, lval,
10121 : non_constant_p, overflow_p,
10122 : jump_target);
10123 14887 : break;
10124 :
10125 4 : case PAREN_EXPR:
10126 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
10127 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10128 : constant expressions since it's unaffected by -fassociative-math. */
10129 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10130 : non_constant_p, overflow_p,
10131 : jump_target);
10132 4 : break;
10133 :
10134 345502611 : case NOP_EXPR:
10135 345502611 : if (REINTERPRET_CAST_P (t))
10136 : {
10137 73 : if (!ctx->quiet)
10138 6 : error_at (loc,
10139 : "%<reinterpret_cast%> is not a constant expression");
10140 73 : *non_constant_p = true;
10141 73 : return t;
10142 : }
10143 : /* FALLTHROUGH. */
10144 431258768 : case CONVERT_EXPR:
10145 431258768 : case VIEW_CONVERT_EXPR:
10146 431258768 : case UNARY_PLUS_EXPR:
10147 431258768 : {
10148 431258768 : tree oldop = TREE_OPERAND (t, 0);
10149 :
10150 431258768 : tree op = cxx_eval_constant_expression (ctx, oldop,
10151 431258768 : VOID_TYPE_P (TREE_TYPE (t))
10152 : ? vc_discard
10153 : : tcode == VIEW_CONVERT_EXPR
10154 403585479 : ? lval : vc_prvalue,
10155 : non_constant_p, overflow_p,
10156 : jump_target);
10157 431256070 : if (*jump_target)
10158 : return NULL_TREE;
10159 431252491 : if (*non_constant_p)
10160 : return t;
10161 349477465 : tree type = TREE_TYPE (t);
10162 :
10163 349477465 : if (VOID_TYPE_P (type))
10164 26815713 : return void_node;
10165 :
10166 322661752 : if (TREE_CODE (t) == CONVERT_EXPR
10167 44307916 : && ARITHMETIC_TYPE_P (type)
10168 4244982 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10169 322685245 : && ctx->strict)
10170 : {
10171 23398 : if (!ctx->quiet)
10172 54 : error_at (loc,
10173 : "conversion from pointer type %qT to arithmetic type "
10174 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10175 23398 : *non_constant_p = true;
10176 23398 : return t;
10177 : }
10178 :
10179 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10180 : type cannot be part of a core constant expression as a resolution to
10181 : DR 1312. */
10182 111379313 : if (TYPE_PTROB_P (type)
10183 108353675 : && TYPE_PTR_P (TREE_TYPE (op))
10184 68165009 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10185 : /* Inside a call to std::construct_at,
10186 : std::allocator<T>::{,de}allocate, or
10187 : std::source_location::current, we permit casting from void*
10188 : because that is compiler-generated code. */
10189 1663551 : && !is_std_construct_at (ctx->call)
10190 92927 : && !is_std_allocator_allocate (ctx->call)
10191 322681918 : && !is_std_source_location_current (ctx->call))
10192 : {
10193 : /* Likewise, don't error when casting from void* when OP is
10194 : &heap uninit and similar. */
10195 43405 : tree sop = tree_strip_nop_conversions (op);
10196 43405 : tree decl = NULL_TREE;
10197 43405 : if (TREE_CODE (sop) == ADDR_EXPR)
10198 33563 : decl = TREE_OPERAND (sop, 0);
10199 33563 : if (decl
10200 33563 : && VAR_P (decl)
10201 33066 : && DECL_ARTIFICIAL (decl)
10202 66265 : && (DECL_NAME (decl) == heap_identifier
10203 25032 : || DECL_NAME (decl) == heap_uninit_identifier
10204 3100 : || DECL_NAME (decl) == heap_vec_identifier
10205 1850 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
10206 : /* OK */;
10207 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10208 : cv void" to a pointer-to-object type T unless P is a null
10209 : pointer value or points to an object whose type is similar to
10210 : T. */
10211 11083 : else if (cxx_dialect > cxx23)
10212 : {
10213 10934 : if (integer_zerop (sop))
10214 29 : return build_int_cst (type, 0);
10215 10905 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10216 : NULL, jump_target);
10217 10905 : if (*jump_target)
10218 : return NULL_TREE;
10219 10905 : if (r)
10220 : {
10221 10864 : r = build1 (ADDR_EXPR, type, r);
10222 10864 : break;
10223 : }
10224 41 : if (!ctx->quiet)
10225 : {
10226 5 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10227 5 : auto_diagnostic_group d;
10228 5 : error_at (loc, "cast from %qT is not allowed in a "
10229 : "constant expression because "
10230 : "pointed-to type %qT is not similar to %qT",
10231 5 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10232 5 : TREE_TYPE (type));
10233 5 : tree obj = build_fold_indirect_ref (sop);
10234 5 : if (TREE_CODE (obj) == COMPONENT_REF)
10235 4 : obj = TREE_OPERAND (obj, 1);
10236 5 : if (DECL_P (obj))
10237 5 : inform (DECL_SOURCE_LOCATION (obj),
10238 : "pointed-to object declared here");
10239 5 : }
10240 41 : *non_constant_p = true;
10241 41 : return t;
10242 : }
10243 : else
10244 : {
10245 149 : if (!ctx->quiet)
10246 26 : error_at (loc, "cast from %qT is not allowed in a "
10247 : "constant expression before C++26",
10248 26 : TREE_TYPE (op));
10249 149 : *non_constant_p = true;
10250 149 : return t;
10251 : }
10252 : }
10253 :
10254 322627271 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
10255 : {
10256 988 : op = cplus_expand_constant (op);
10257 988 : if (TREE_CODE (op) == PTRMEM_CST)
10258 : {
10259 21 : if (!ctx->quiet)
10260 3 : error_at (loc, "%qE is not a constant expression when the "
10261 : "class %qT is still incomplete", op,
10262 3 : PTRMEM_CST_CLASS (op));
10263 21 : *non_constant_p = true;
10264 21 : return t;
10265 : }
10266 : }
10267 :
10268 322627250 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
10269 : {
10270 579 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
10271 579 : && !can_convert_qual (type, op))
10272 6 : op = cplus_expand_constant (op);
10273 579 : return cp_fold_convert (type, op);
10274 : }
10275 :
10276 322626671 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
10277 : {
10278 586858 : if (integer_zerop (op))
10279 : {
10280 532240 : if (TYPE_REF_P (type))
10281 : {
10282 35 : if (!ctx->quiet)
10283 4 : error_at (loc, "dereferencing a null pointer");
10284 35 : *non_constant_p = true;
10285 35 : return t;
10286 : }
10287 : }
10288 54618 : else if (TYPE_PTR_P (type)
10289 54618 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10290 : /* INTEGER_CST with pointer-to-method type is only used
10291 : for a virtual method in a pointer to member function.
10292 : Don't reject those. */
10293 : ;
10294 : else
10295 : {
10296 : /* This detects for example:
10297 : reinterpret_cast<void*>(sizeof 0)
10298 : */
10299 54615 : if (!ctx->quiet)
10300 15 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
10301 : "a constant expression",
10302 : type, op);
10303 54615 : *non_constant_p = true;
10304 54615 : return t;
10305 : }
10306 : }
10307 :
10308 322572021 : if (INDIRECT_TYPE_P (type)
10309 185766514 : && TREE_CODE (op) == NOP_EXPR
10310 83741425 : && TREE_TYPE (op) == ptr_type_node
10311 345794 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
10312 343530 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
10313 322820084 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10314 248063 : 0)) == heap_uninit_identifier
10315 176768 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10316 176768 : 0)) == heap_vec_uninit_identifier))
10317 : {
10318 72765 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
10319 72765 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
10320 72765 : tree elt_type = TREE_TYPE (type);
10321 72765 : tree cookie_size = NULL_TREE;
10322 72765 : tree arg_size = NULL_TREE;
10323 72765 : if (TREE_CODE (elt_type) == RECORD_TYPE
10324 72765 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
10325 : {
10326 9 : tree fld1 = TYPE_FIELDS (elt_type);
10327 9 : tree fld2 = DECL_CHAIN (fld1);
10328 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
10329 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
10330 : }
10331 72765 : DECL_NAME (var)
10332 72765 : = (DECL_NAME (var) == heap_uninit_identifier
10333 72765 : ? heap_identifier : heap_vec_identifier);
10334 : /* For zero sized elt_type, try to recover how many outer_nelts
10335 : it should have. */
10336 72765 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
10337 72756 : : integer_zerop (var_size))
10338 89 : && !int_size_in_bytes (elt_type)
10339 9 : && TREE_CODE (oldop) == CALL_EXPR
10340 72783 : && call_expr_nargs (oldop) >= 1)
10341 9 : if (tree fun = get_function_named_in_call (oldop))
10342 9 : if (cxx_replaceable_global_alloc_fn (fun)
10343 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
10344 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
10345 72765 : tree new_type
10346 72765 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
10347 : var_size, arg_size,
10348 : non_constant_p, overflow_p,
10349 : jump_target);
10350 72765 : if (*jump_target)
10351 : return NULL_TREE;
10352 72765 : TREE_TYPE (var) = new_type;
10353 72765 : TREE_TYPE (TREE_OPERAND (op, 0))
10354 145530 : = build_pointer_type (TREE_TYPE (var));
10355 : }
10356 :
10357 : /* This can happen for std::meta::info(^^int) where the cast has no
10358 : meaning. */
10359 322572021 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
10360 : {
10361 : r = op;
10362 : break;
10363 : }
10364 :
10365 322468643 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
10366 : /* We didn't fold at the top so we could check for ptr-int
10367 : conversion. */
10368 24419558 : return fold (t);
10369 :
10370 298049085 : tree sop;
10371 :
10372 : /* Handle an array's bounds having been deduced after we built
10373 : the wrapping expression. */
10374 298049085 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
10375 : r = op;
10376 125691901 : else if (sop = tree_strip_nop_conversions (op),
10377 172038996 : sop != op && (same_type_ignoring_tlq_and_bounds_p
10378 46347095 : (type, TREE_TYPE (sop))))
10379 : r = sop;
10380 106568505 : else if (tcode == UNARY_PLUS_EXPR)
10381 0 : r = fold_convert (TREE_TYPE (t), op);
10382 : else
10383 106568505 : r = fold_build1 (tcode, type, op);
10384 :
10385 : /* Conversion of an out-of-range value has implementation-defined
10386 : behavior; the language considers it different from arithmetic
10387 : overflow, which is undefined. */
10388 298049085 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10389 11925 : TREE_OVERFLOW (r) = false;
10390 : }
10391 : break;
10392 :
10393 8385 : case EXCESS_PRECISION_EXPR:
10394 8385 : {
10395 8385 : tree oldop = TREE_OPERAND (t, 0);
10396 :
10397 8385 : tree op = cxx_eval_constant_expression (ctx, oldop,
10398 : lval,
10399 : non_constant_p, overflow_p,
10400 : jump_target);
10401 8385 : if (*jump_target)
10402 : return NULL_TREE;
10403 8385 : if (*non_constant_p)
10404 : return t;
10405 8381 : r = fold_convert (TREE_TYPE (t), op);
10406 8381 : break;
10407 : }
10408 :
10409 39937 : case EMPTY_CLASS_EXPR:
10410 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10411 : it to an appropriate CONSTRUCTOR. */
10412 39937 : return build_constructor (TREE_TYPE (t), NULL);
10413 :
10414 36907413 : case STATEMENT_LIST:
10415 36907413 : new_ctx = *ctx;
10416 36907413 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10417 36907413 : return cxx_eval_statement_list (&new_ctx, t,
10418 36907411 : non_constant_p, overflow_p, jump_target);
10419 :
10420 24302172 : case BIND_EXPR:
10421 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10422 : map, so that when checking whether they're already destroyed later we
10423 : don't get confused by remnants of previous calls. */
10424 37604694 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10425 13302522 : ctx->global->clear_value (decl);
10426 24302172 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10427 : lval,
10428 : non_constant_p, overflow_p,
10429 : jump_target);
10430 37604692 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10431 13302521 : destroy_value_checked (ctx, decl, non_constant_p);
10432 : break;
10433 :
10434 7299047 : case PREINCREMENT_EXPR:
10435 7299047 : case POSTINCREMENT_EXPR:
10436 7299047 : case PREDECREMENT_EXPR:
10437 7299047 : case POSTDECREMENT_EXPR:
10438 7299047 : return cxx_eval_increment_expression (ctx, t,
10439 : lval, non_constant_p, overflow_p,
10440 7299047 : jump_target);
10441 :
10442 1400 : case THROW_EXPR:
10443 1400 : if (cxx_dialect >= cxx26)
10444 1073 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10445 : non_constant_p, overflow_p,
10446 1073 : jump_target);
10447 : /* FALLTHROUGH */
10448 335 : case LAMBDA_EXPR:
10449 335 : case NEW_EXPR:
10450 335 : case VEC_NEW_EXPR:
10451 335 : case DELETE_EXPR:
10452 335 : case VEC_DELETE_EXPR:
10453 335 : case MODOP_EXPR:
10454 : /* GCC internal stuff. */
10455 335 : case VA_ARG_EXPR:
10456 335 : case BASELINK:
10457 335 : case OFFSET_REF:
10458 335 : if (!ctx->quiet)
10459 86 : error_at (loc, "expression %qE is not a constant expression", t);
10460 335 : *non_constant_p = true;
10461 335 : break;
10462 :
10463 211807 : case OBJ_TYPE_REF:
10464 : /* Virtual function lookup. We don't need to do anything fancy. */
10465 211807 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10466 : lval, non_constant_p, overflow_p,
10467 211807 : jump_target);
10468 :
10469 16113 : case PLACEHOLDER_EXPR:
10470 : /* Use of the value or address of the current object. */
10471 16113 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10472 : {
10473 15885 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10474 : return ctor;
10475 : else
10476 15585 : return cxx_eval_constant_expression (ctx, ctor, lval,
10477 : non_constant_p, overflow_p,
10478 15585 : jump_target);
10479 : }
10480 : /* A placeholder without a referent. We can get here when
10481 : checking whether NSDMIs are noexcept, or in massage_init_elt;
10482 : just say it's non-constant for now. */
10483 228 : gcc_assert (ctx->quiet);
10484 228 : *non_constant_p = true;
10485 228 : break;
10486 :
10487 5998 : case EXIT_EXPR:
10488 5998 : {
10489 5998 : tree cond = TREE_OPERAND (t, 0);
10490 5998 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10491 : non_constant_p, overflow_p,
10492 : jump_target);
10493 5998 : if (*jump_target)
10494 : return NULL_TREE;
10495 5998 : VERIFY_CONSTANT (cond);
10496 5998 : if (integer_nonzerop (cond))
10497 2930 : *jump_target = t;
10498 : }
10499 : break;
10500 :
10501 21 : case GOTO_EXPR:
10502 21 : if (breaks (&TREE_OPERAND (t, 0))
10503 21 : || continues (&TREE_OPERAND (t, 0)))
10504 9 : *jump_target = TREE_OPERAND (t, 0);
10505 : else
10506 : {
10507 12 : if (!ctx->quiet)
10508 1 : error_at (loc, "%<goto%> is not a constant expression");
10509 12 : *non_constant_p = true;
10510 : }
10511 : break;
10512 :
10513 3706302 : case LOOP_EXPR:
10514 3706302 : case DO_STMT:
10515 3706302 : case WHILE_STMT:
10516 3706302 : case FOR_STMT:
10517 3706302 : cxx_eval_loop_expr (ctx, t,
10518 : non_constant_p, overflow_p, jump_target);
10519 3706302 : break;
10520 :
10521 49002 : case SWITCH_EXPR:
10522 49002 : case SWITCH_STMT:
10523 49002 : cxx_eval_switch_expr (ctx, t,
10524 : non_constant_p, overflow_p, jump_target);
10525 49002 : break;
10526 :
10527 132 : case REQUIRES_EXPR:
10528 : /* It's possible to get a requires-expression in a constant
10529 : expression. For example:
10530 :
10531 : template<typename T> concept bool C() {
10532 : return requires (T t) { t; };
10533 : }
10534 :
10535 : template<typename T> requires !C<T>() void f(T);
10536 :
10537 : Normalization leaves f with the associated constraint
10538 : '!requires (T t) { ... }' which is not transformed into
10539 : a constraint. */
10540 132 : if (!processing_template_decl)
10541 132 : return evaluate_requires_expr (t);
10542 : else
10543 0 : *non_constant_p = true;
10544 0 : return t;
10545 :
10546 1214946 : case ANNOTATE_EXPR:
10547 1214946 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10548 : lval,
10549 : non_constant_p, overflow_p,
10550 : jump_target);
10551 1214946 : break;
10552 :
10553 15208 : case USING_STMT:
10554 15208 : r = void_node;
10555 15208 : break;
10556 :
10557 30 : case ASSERTION_STMT:
10558 30 : case PRECONDITION_STMT:
10559 30 : case POSTCONDITION_STMT:
10560 30 : {
10561 30 : r = void_node;
10562 : /* Only record the first fail, and do not go further is the semantic
10563 : is 'ignore'. */
10564 30 : if (*non_constant_p || ctx->global->contract_statement
10565 60 : || contract_ignored_p (t))
10566 : break;
10567 :
10568 30 : tree cond = CONTRACT_CONDITION (t);
10569 30 : if (!potential_rvalue_constant_expression (cond))
10570 : {
10571 6 : ctx->global->contract_statement = t;
10572 6 : ctx->global->contract_condition_non_const = true;
10573 6 : break;
10574 : }
10575 :
10576 : /* We need to evaluate and stash the result of this here, since whether
10577 : it needs to be reported (and how) depends on whether the containing
10578 : expression is otherwise const. */
10579 24 : bool ctrct_non_const_p = false;
10580 24 : bool ctrct_overflow_p = false;
10581 24 : tree jmp_target = NULL_TREE;
10582 24 : constexpr_ctx new_ctx = *ctx;
10583 24 : new_ctx.quiet = true;
10584 : /* Avoid modification of existing values. */
10585 24 : modifiable_tracker ms (new_ctx.global);
10586 24 : tree eval =
10587 24 : cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
10588 : &ctrct_non_const_p,
10589 : &ctrct_overflow_p, &jmp_target);
10590 : /* Not a constant. */
10591 24 : if (ctrct_non_const_p)
10592 : {
10593 0 : ctx->global->contract_statement = t;
10594 0 : ctx->global->contract_condition_non_const = true;
10595 0 : break;
10596 : }
10597 : /* Constant, but check failed. */
10598 24 : if (integer_zerop (eval))
10599 24 : ctx->global->contract_statement = t;
10600 24 : }
10601 24 : break;
10602 :
10603 2391504 : case TEMPLATE_ID_EXPR:
10604 2391504 : {
10605 : /* We can evaluate template-id that refers to a concept only if
10606 : the template arguments are non-dependent. */
10607 2391504 : gcc_assert (concept_check_p (t));
10608 :
10609 2391504 : if (!value_dependent_expression_p (t)
10610 2391504 : && !uid_sensitive_constexpr_evaluation_p ())
10611 2391065 : r = evaluate_concept_check (t);
10612 : else
10613 439 : *non_constant_p = true;
10614 :
10615 : break;
10616 : }
10617 :
10618 51 : case ASM_EXPR:
10619 51 : if (!ctx->quiet)
10620 29 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10621 51 : *non_constant_p = true;
10622 51 : return t;
10623 :
10624 1125 : case BIT_CAST_EXPR:
10625 1125 : if (lval)
10626 : {
10627 0 : if (!ctx->quiet)
10628 0 : error_at (EXPR_LOCATION (t),
10629 : "address of a call to %qs is not a constant expression",
10630 : "__builtin_bit_cast");
10631 0 : *non_constant_p = true;
10632 0 : return t;
10633 : }
10634 1125 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10635 1125 : break;
10636 :
10637 0 : case OMP_PARALLEL:
10638 0 : case OMP_TASK:
10639 0 : case OMP_FOR:
10640 0 : case OMP_SIMD:
10641 0 : case OMP_DISTRIBUTE:
10642 0 : case OMP_TASKLOOP:
10643 0 : case OMP_LOOP:
10644 0 : case OMP_TEAMS:
10645 0 : case OMP_TARGET_DATA:
10646 0 : case OMP_TARGET:
10647 0 : case OMP_SECTIONS:
10648 0 : case OMP_ORDERED:
10649 0 : case OMP_CRITICAL:
10650 0 : case OMP_SINGLE:
10651 0 : case OMP_SCAN:
10652 0 : case OMP_SCOPE:
10653 0 : case OMP_SECTION:
10654 0 : case OMP_STRUCTURED_BLOCK:
10655 0 : case OMP_MASTER:
10656 0 : case OMP_MASKED:
10657 0 : case OMP_TASKGROUP:
10658 0 : case OMP_TARGET_UPDATE:
10659 0 : case OMP_TARGET_ENTER_DATA:
10660 0 : case OMP_TARGET_EXIT_DATA:
10661 0 : case OMP_ATOMIC:
10662 0 : case OMP_ATOMIC_READ:
10663 0 : case OMP_ATOMIC_CAPTURE_OLD:
10664 0 : case OMP_ATOMIC_CAPTURE_NEW:
10665 0 : case OMP_DEPOBJ:
10666 0 : case OACC_PARALLEL:
10667 0 : case OACC_KERNELS:
10668 0 : case OACC_SERIAL:
10669 0 : case OACC_DATA:
10670 0 : case OACC_HOST_DATA:
10671 0 : case OACC_LOOP:
10672 0 : case OACC_CACHE:
10673 0 : case OACC_DECLARE:
10674 0 : case OACC_ENTER_DATA:
10675 0 : case OACC_EXIT_DATA:
10676 0 : case OACC_UPDATE:
10677 0 : if (!ctx->quiet)
10678 0 : error_at (EXPR_LOCATION (t),
10679 : "statement is not a constant expression");
10680 0 : *non_constant_p = true;
10681 0 : break;
10682 :
10683 0 : default:
10684 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10685 : {
10686 : /* This function doesn't know how to deal with pre-genericize
10687 : statements; this can only happen with statement-expressions,
10688 : so for now just fail. */
10689 0 : if (!ctx->quiet)
10690 0 : error_at (EXPR_LOCATION (t),
10691 : "statement is not a constant expression");
10692 : }
10693 0 : else if (flag_checking)
10694 0 : internal_error ("unexpected expression %qE of kind %s", t,
10695 : get_tree_code_name (TREE_CODE (t)));
10696 0 : *non_constant_p = true;
10697 0 : break;
10698 : }
10699 :
10700 1574990384 : if (r == error_mark_node)
10701 11866359 : *non_constant_p = true;
10702 :
10703 105632851 : if (r == void_node && lval != vc_discard && !*jump_target
10704 1575004898 : && !VOID_TYPE_P (TREE_TYPE (t)))
10705 : {
10706 : /* For diagnostic quality we should have handled this sooner, where we
10707 : can be more specific about the out-of-lifetime object. But here we
10708 : can still be correct. */
10709 1 : gcc_checking_assert (false);
10710 : if (!ctx->quiet)
10711 : error_at (EXPR_LOCATION (t),
10712 : "%qE accesses an object outside its lifetime", t);
10713 : *non_constant_p = true;
10714 : }
10715 :
10716 1574990383 : if (*non_constant_p)
10717 327068497 : return t;
10718 : else
10719 : return r;
10720 : }
10721 :
10722 : /* P0859: A function is needed for constant evaluation if it is a constexpr
10723 : function that is named by an expression ([basic.def.odr]) that is
10724 : potentially constant evaluated.
10725 :
10726 : So we need to instantiate any constexpr functions mentioned by the
10727 : expression even if the definition isn't needed for evaluating the
10728 : expression. */
10729 :
10730 : static tree
10731 547722529 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10732 : {
10733 547722529 : if (TREE_CODE (*tp) == FUNCTION_DECL
10734 11416647 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10735 11290124 : && !DECL_INITIAL (*tp)
10736 2914834 : && !trivial_fn_p (*tp)
10737 2914687 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10738 547722529 : && !uid_sensitive_constexpr_evaluation_p ())
10739 : {
10740 2885723 : ++function_depth;
10741 2885723 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10742 2885696 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10743 : else
10744 27 : synthesize_method (*tp);
10745 2885723 : --function_depth;
10746 : }
10747 544836806 : else if (TREE_CODE (*tp) == CALL_EXPR
10748 533886457 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10749 : {
10750 11476676 : if (EXPR_HAS_LOCATION (*tp))
10751 11469946 : input_location = EXPR_LOCATION (*tp);
10752 : }
10753 :
10754 547722529 : if (!EXPR_P (*tp))
10755 310428604 : *walk_subtrees = 0;
10756 :
10757 547722529 : return NULL_TREE;
10758 : }
10759 :
10760 : static void
10761 263834087 : instantiate_constexpr_fns (tree t)
10762 : {
10763 263834087 : location_t loc = input_location;
10764 263834087 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10765 263834087 : input_location = loc;
10766 263834087 : }
10767 :
10768 : /* Look for heap variables in the expression *TP. */
10769 :
10770 : static tree
10771 336030 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10772 : {
10773 336030 : if (VAR_P (*tp)
10774 336030 : && (DECL_NAME (*tp) == heap_uninit_identifier
10775 6413 : || DECL_NAME (*tp) == heap_identifier
10776 2492 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10777 1834 : || DECL_NAME (*tp) == heap_vec_identifier
10778 717 : || DECL_NAME (*tp) == heap_deleted_identifier))
10779 : return *tp;
10780 :
10781 235172 : if (TYPE_P (*tp))
10782 166 : *walk_subtrees = 0;
10783 : return NULL_TREE;
10784 : }
10785 :
10786 : /* Find immediate function decls in *TP if any. */
10787 :
10788 : static tree
10789 569710264 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
10790 : {
10791 573362053 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10792 : return *tp;
10793 569704359 : if (TREE_CODE (*tp) == PTRMEM_CST
10794 28432 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10795 569732046 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10796 : return PTRMEM_CST_MEMBER (*tp);
10797 569704254 : if (REFLECT_EXPR_P (*tp))
10798 106156 : *walk_subtrees = 0;
10799 : return NULL_TREE;
10800 : }
10801 :
10802 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10803 : expression. Return a version of T that has TREE_CONSTANT cleared. */
10804 :
10805 : static tree
10806 143895 : mark_non_constant (tree t)
10807 : {
10808 143895 : gcc_checking_assert (TREE_CONSTANT (t));
10809 :
10810 : /* This isn't actually constant, so unset TREE_CONSTANT.
10811 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10812 : it to be set if it is invariant address, even when it is not
10813 : a valid C++ constant expression. Wrap it with a NOP_EXPR
10814 : instead. */
10815 143895 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10816 141418 : t = copy_node (t);
10817 2477 : else if (TREE_CODE (t) == CONSTRUCTOR)
10818 235 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10819 : else
10820 2242 : t = build_nop (TREE_TYPE (t), t);
10821 143895 : TREE_CONSTANT (t) = false;
10822 143895 : return t;
10823 : }
10824 :
10825 : /* If we have a successful constant evaluation, now check whether there is
10826 : a failed or non-constant contract that would invalidate this. */
10827 :
10828 : static bool
10829 501382033 : check_for_failed_contracts (constexpr_ctx *ctx)
10830 : {
10831 501382033 : constexpr_global_ctx *const global_ctx = ctx->global;
10832 501382033 : if (!flag_contracts || !global_ctx->contract_statement)
10833 : return false;
10834 :
10835 30 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
10836 30 : enum diagnostics::kind kind;
10837 30 : bool error = false;
10838 : /* [intro.compliance.general]/2.3.4. */
10839 : /* [basic.contract.eval]/8. */
10840 30 : if (ctx->manifestly_const_eval != mce_true)
10841 : {
10842 : /* When !MCE, silently return not constant. */
10843 : return true;
10844 : }
10845 27 : else if (contract_terminating_p (global_ctx->contract_statement))
10846 : {
10847 : kind = diagnostics::kind::error;
10848 : error = true;
10849 : }
10850 : else
10851 6 : kind = diagnostics::kind::warning;
10852 :
10853 : /* [basic.contract.eval]/7.3 */
10854 27 : if (global_ctx->contract_condition_non_const)
10855 : {
10856 6 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
10857 6 : return error;
10858 : }
10859 :
10860 : /* Otherwise, the evaluation was const, but determined to be false. */
10861 21 : emit_diagnostic (kind, loc, 0,
10862 : "contract predicate is false in constant expression");
10863 21 : return error;
10864 : }
10865 :
10866 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10867 : STRICT has the same sense as for constant_value_1: true if we only allow
10868 : conforming C++ constant expressions, or false if we want a constant value
10869 : even if it doesn't conform.
10870 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10871 : per P0595 even when ALLOW_NON_CONSTANT is true.
10872 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10873 : OBJECT must be non-NULL in that case. */
10874 :
10875 : static tree
10876 510610935 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10877 : bool strict = true,
10878 : mce_value manifestly_const_eval = mce_unknown,
10879 : bool constexpr_dtor = false,
10880 : tree object = NULL_TREE)
10881 : {
10882 510610935 : auto_timevar time (TV_CONSTEXPR);
10883 :
10884 510610935 : bool non_constant_p = false;
10885 510610935 : bool overflow_p = false;
10886 :
10887 510610935 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10888 : {
10889 0 : gcc_checking_assert (allow_non_constant);
10890 : return t;
10891 : }
10892 :
10893 510610935 : constexpr_global_ctx global_ctx;
10894 510610935 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10895 : allow_non_constant, strict,
10896 510610935 : !allow_non_constant ? mce_true : manifestly_const_eval };
10897 :
10898 : /* Turn off -frounding-math for manifestly constant evaluation. */
10899 510610935 : warning_sentinel rm (flag_rounding_math,
10900 510610935 : ctx.manifestly_const_eval == mce_true);
10901 510610935 : tree type = (object
10902 510610935 : ? cv_unqualified (TREE_TYPE (object))
10903 469928749 : : initialized_type (t));
10904 510610935 : tree r = t;
10905 510610935 : bool is_consteval = false;
10906 510610935 : if (VOID_TYPE_P (type))
10907 : {
10908 9222696 : if (!constexpr_dtor)
10909 : {
10910 9222696 : if (cxx_dialect < cxx20)
10911 : return t;
10912 : /* We could have a COMPOUND_EXPR here coming from
10913 : keep_unused_object_arg. */
10914 9211674 : tree x = extract_call_expr (t);
10915 9211674 : if (x == NULL_TREE || x == error_mark_node)
10916 : return t;
10917 : /* Calls to immediate functions returning void need to be
10918 : evaluated. */
10919 9211634 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10920 18423268 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10921 : return t;
10922 : else
10923 1125 : is_consteval = true;
10924 1125 : tree lam;
10925 1125 : if (manifestly_const_eval == mce_true
10926 1638 : && LAMBDA_FUNCTION_P (fndecl)
10927 507 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
10928 1632 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
10929 490 : global_ctx.consteval_block = fndecl;
10930 : }
10931 : }
10932 501388239 : else if (cxx_dialect >= cxx20
10933 493235188 : && (TREE_CODE (t) == CALL_EXPR
10934 428028312 : || TREE_CODE (t) == AGGR_INIT_EXPR
10935 423942146 : || TREE_CODE (t) == TARGET_EXPR))
10936 : {
10937 74345421 : tree x = t;
10938 74345421 : if (TREE_CODE (x) == TARGET_EXPR)
10939 5052379 : x = TARGET_EXPR_INITIAL (x);
10940 74345421 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10941 146287211 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10942 : is_consteval = true;
10943 : /* Don't try to evaluate a std::vector constructor taking an integer, it
10944 : will fail in the 'if (heap_var)' block below after doing all the work
10945 : (c++/113835). This will need adjustment if P3554 is accepted. Note
10946 : that evaluation of e.g. the vector default constructor can succeed, so
10947 : we don't shortcut all vector constructors. */
10948 143883580 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10949 10039797 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10950 74366668 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10951 : return t;
10952 : }
10953 501388012 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10954 : {
10955 : /* In C++14 an NSDMI can participate in aggregate initialization,
10956 : and can refer to the address of the object being initialized, so
10957 : we need to pass in the relevant VAR_DECL if we want to do the
10958 : evaluation in a single pass. The evaluation will dynamically
10959 : update ctx.values for the VAR_DECL. We use the same strategy
10960 : for C++11 constexpr constructors that refer to the object being
10961 : initialized. */
10962 39032271 : if (constexpr_dtor)
10963 : {
10964 198 : gcc_assert (object && VAR_P (object));
10965 198 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10966 198 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10967 198 : if (error_operand_p (DECL_INITIAL (object)))
10968 : return t;
10969 183 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10970 183 : TREE_READONLY (ctx.ctor) = false;
10971 : /* Temporarily force decl_really_constant_value to return false
10972 : for it, we want to use ctx.ctor for the current value instead. */
10973 183 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10974 : }
10975 : else
10976 : {
10977 39032073 : ctx.ctor = build_constructor (type, NULL);
10978 39032073 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10979 : }
10980 39032256 : if (!object)
10981 : {
10982 20274730 : if (TREE_CODE (t) == CALL_EXPR)
10983 : {
10984 : /* If T is calling a constructor to initialize an object, reframe
10985 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10986 : from outside the constant evaluation, which will fail even if
10987 : the value is actually constant (is_constant_evaluated3.C). */
10988 9222405 : tree fn = cp_get_callee_fndecl_nofold (t);
10989 18444714 : if (fn && DECL_CONSTRUCTOR_P (fn))
10990 : {
10991 3782460 : object = CALL_EXPR_ARG (t, 0);
10992 3782460 : object = build_fold_indirect_ref (object);
10993 3782460 : r = build_aggr_init_expr (type, r);
10994 : }
10995 : }
10996 11052325 : else if (TREE_CODE (t) == TARGET_EXPR)
10997 4963202 : object = TARGET_EXPR_SLOT (t);
10998 6089123 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10999 670342 : object = AGGR_INIT_EXPR_SLOT (t);
11000 : }
11001 39032256 : ctx.object = object;
11002 39032256 : if (object)
11003 28173530 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
11004 : (type, TREE_TYPE (object)));
11005 28173530 : if (object && DECL_P (object))
11006 24060721 : global_ctx.put_value (object, ctx.ctor);
11007 39032256 : if (TREE_CODE (r) == TARGET_EXPR)
11008 : /* Avoid creating another CONSTRUCTOR when we expand the
11009 : TARGET_EXPR. */
11010 5089072 : r = TARGET_EXPR_INITIAL (r);
11011 : }
11012 :
11013 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
11014 : constexpr evaluation to avoid unnecessary template instantiation, and is
11015 : always done with mce_unknown. But due to gaps in the restriction logic
11016 : we may still end up taking an evaluation path that in turn requires
11017 : manifestly constant evaluation, and such evaluation must not be
11018 : restricted since it likely has semantic consequences.
11019 : TODO: Remove/replace the mechanism in GCC 17. */
11020 501387997 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
11021 501387997 : if (ctx.manifestly_const_eval == mce_true)
11022 263834087 : uid_sensitive_constexpr_evaluation_value = false;
11023 :
11024 501387997 : auto_vec<tree, 16> cleanups;
11025 501387997 : global_ctx.cleanups = &cleanups;
11026 :
11027 501387997 : if (manifestly_const_eval == mce_true)
11028 263834087 : instantiate_constexpr_fns (r);
11029 501387997 : tree jmp_target = NULL_TREE;
11030 501387997 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
11031 : &non_constant_p, &overflow_p,
11032 : &jmp_target);
11033 501386423 : if (throws (&jmp_target) && !non_constant_p)
11034 : {
11035 1124 : if (!ctx.quiet)
11036 374 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11037 1124 : non_constant_p = true;
11038 1124 : jmp_target = NULL_TREE;
11039 1124 : r = t;
11040 : }
11041 501384175 : else if (!non_constant_p && jmp_target)
11042 : {
11043 24 : non_constant_p = true;
11044 24 : if (!ctx.quiet)
11045 : {
11046 3 : if (breaks (&jmp_target))
11047 3 : error ("%<break%> outside of a loop or %<switch%>");
11048 0 : else if (continues (&jmp_target))
11049 0 : error ("%<continue%> outside of a loop");
11050 0 : else if (returns (&jmp_target))
11051 0 : error ("%<return%> in a statement expression");
11052 : else
11053 0 : gcc_unreachable ();
11054 : }
11055 24 : r = t;
11056 : }
11057 :
11058 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
11059 : of statements, and the result ought to be stored in ctx.ctor. */
11060 501385299 : if (r == void_node && !constexpr_dtor && ctx.ctor)
11061 0 : r = ctx.ctor;
11062 :
11063 501385299 : unsigned int i;
11064 501385299 : tree cleanup;
11065 501385299 : jmp_target = NULL_TREE;
11066 : /* Evaluate the cleanups. */
11067 1002857576 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
11068 86978 : if (cleanup == NULL_TREE)
11069 : /* NULL_TREE cleanup is a marker that before it is
11070 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
11071 23 : --i;
11072 : else
11073 86955 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
11074 : &non_constant_p, &overflow_p,
11075 : &jmp_target);
11076 501385299 : if (throws (&jmp_target) && !non_constant_p)
11077 : {
11078 0 : if (!ctx.quiet)
11079 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11080 0 : non_constant_p = true;
11081 0 : r = t;
11082 : }
11083 :
11084 : /* Mutable logic is a bit tricky: we want to allow initialization of
11085 : constexpr variables with mutable members, but we can't copy those
11086 : members to another constexpr variable. */
11087 501385299 : if (!non_constant_p
11088 381379498 : && TREE_CODE (r) == CONSTRUCTOR
11089 516396665 : && CONSTRUCTOR_MUTABLE_POISON (r))
11090 : {
11091 90 : if (!allow_non_constant)
11092 6 : error ("%qE is not a constant expression because it refers to "
11093 : "mutable subobjects of %qT", t, type);
11094 90 : non_constant_p = true;
11095 : }
11096 :
11097 381379408 : if (!non_constant_p && cxx_dialect >= cxx20
11098 882764707 : && !global_ctx.heap_vars.is_empty ())
11099 : {
11100 111263 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11101 : NULL);
11102 111263 : unsigned int i;
11103 111263 : if (heap_var)
11104 : {
11105 100858 : if (!allow_non_constant && !non_constant_p)
11106 : {
11107 15 : auto_diagnostic_group d;
11108 15 : if (DECL_LANG_SPECIFIC (heap_var))
11109 4 : error ("%qE is not a constant expression because it refers to "
11110 : "exception object allocated with "
11111 : "%<__cxa_allocate_exception%>", t);
11112 : else
11113 11 : error ("%qE is not a constant expression because it refers to "
11114 : "a result of %<operator new%>", t);
11115 15 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11116 15 : }
11117 100858 : r = t;
11118 100858 : non_constant_p = true;
11119 : }
11120 262922 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11121 : {
11122 151659 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11123 : {
11124 100928 : if (!allow_non_constant && !non_constant_p)
11125 : {
11126 16 : auto_diagnostic_group d;
11127 16 : error ("%qE is not a constant expression because allocated "
11128 : "storage has not been deallocated", t);
11129 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11130 16 : }
11131 100928 : r = t;
11132 100928 : non_constant_p = true;
11133 : }
11134 : }
11135 : }
11136 :
11137 : /* Check that immediate invocation does not return an expression referencing
11138 : any immediate function decls. */
11139 501385299 : if (!non_constant_p && cxx_dialect >= cxx20)
11140 751249228 : if (tree immediate_fndecl
11141 375624614 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11142 : NULL))
11143 : {
11144 6010 : if (!allow_non_constant && !non_constant_p)
11145 : {
11146 74 : if (is_consteval)
11147 36 : error_at (cp_expr_loc_or_input_loc (t),
11148 : "immediate evaluation returns address of immediate "
11149 : "function %qD", immediate_fndecl);
11150 : else
11151 58 : error_at (cp_expr_loc_or_input_loc (t),
11152 : "constant evaluation returns address of immediate "
11153 : "function %qD", immediate_fndecl);
11154 : }
11155 6010 : r = t;
11156 6010 : non_constant_p = true;
11157 : }
11158 :
11159 : /* Detect consteval-only smuggling: turning a consteval-only object
11160 : into one that is not. For instance, in
11161 : struct B { };
11162 : struct D : B { info r; };
11163 : constexpr D d{^^::};
11164 : constexpr const B &b = d; // #1
11165 : #1 is wrong because D is a consteval-only type but B is not. */
11166 501385299 : if (flag_reflection
11167 10221650 : && !non_constant_p
11168 8247754 : && object
11169 939654 : && POINTER_TYPE_P (TREE_TYPE (object))
11170 1462 : && !consteval_only_p (object)
11171 501386647 : && check_out_of_consteval_use (r, /*complain=*/false))
11172 : {
11173 30 : if (!allow_non_constant)
11174 : {
11175 10 : if (TYPE_REF_P (TREE_TYPE (object)))
11176 12 : error_at (cp_expr_loc_or_input_loc (t),
11177 : "reference into an object of consteval-only type is "
11178 : "not a constant expression unless it also has "
11179 : "consteval-only type");
11180 : else
11181 4 : error_at (cp_expr_loc_or_input_loc (t),
11182 : "pointer into an object of consteval-only type is "
11183 : "not a constant expression unless it also has "
11184 : "consteval-only type");
11185 : }
11186 30 : r = t;
11187 30 : non_constant_p = true;
11188 : }
11189 :
11190 501385299 : if (!non_constant_p && !constexpr_dtor)
11191 381272277 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11192 :
11193 : /* After verify_constant because reduced_constant_expression_p can unset
11194 : CONSTRUCTOR_NO_CLEARING. */
11195 501385299 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11196 : {
11197 60292 : if (!allow_non_constant)
11198 43 : error ("%qE is not a constant expression because it refers to "
11199 : "an incompletely initialized variable", t);
11200 60292 : TREE_CONSTANT (r) = false;
11201 60292 : non_constant_p = true;
11202 : }
11203 :
11204 501385299 : if (non_constant_p)
11205 : /* If we saw something bad, go back to our argument. The wrapping below is
11206 : only for the cases of TREE_CONSTANT argument or overflow. */
11207 122070309 : r = t;
11208 :
11209 501385299 : if (!non_constant_p && overflow_p)
11210 215 : non_constant_p = true;
11211 :
11212 : /* Unshare the result. */
11213 501385299 : bool should_unshare = true;
11214 501385299 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
11215 1371338 : && TARGET_EXPR_INITIAL (t) == r))
11216 : should_unshare = false;
11217 :
11218 501385299 : if (non_constant_p && !allow_non_constant)
11219 3266 : return error_mark_node;
11220 501382033 : else if (check_for_failed_contracts (&ctx))
11221 : {
11222 24 : if (manifestly_const_eval == mce_true)
11223 : /* If MCE, we gave a hard error and return error_mark_node. */
11224 21 : return error_mark_node;
11225 : else
11226 : {
11227 : /* Otherwise treat it as non-constant so the violation is still
11228 : detectable at run-time. */
11229 3 : gcc_checking_assert (allow_non_constant);
11230 : return t;
11231 : }
11232 : }
11233 501382009 : else if (non_constant_p && TREE_CONSTANT (r))
11234 139404 : r = mark_non_constant (r);
11235 501242605 : else if (non_constant_p)
11236 : return t;
11237 :
11238 379454155 : if (constexpr_dtor)
11239 : {
11240 165 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
11241 165 : return r;
11242 : }
11243 :
11244 : /* Check we are not trying to return the wrong type. */
11245 379453990 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
11246 : {
11247 : /* If so, this is not a constant expression. */
11248 157 : if (!allow_non_constant)
11249 4 : error ("%qE is not a constant expression because it initializes "
11250 4 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
11251 157 : return t;
11252 : }
11253 :
11254 379453833 : if (should_unshare)
11255 217890772 : r = unshare_expr (r);
11256 :
11257 379453833 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
11258 : {
11259 14204988 : r = adjust_temp_type (type, r);
11260 14204988 : if (TREE_CODE (t) == TARGET_EXPR
11261 14204988 : && TARGET_EXPR_INITIAL (t) == r)
11262 : return t;
11263 13309937 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
11264 2288574 : || TREE_CODE (t) == AGGR_INIT_EXPR)
11265 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
11266 564872 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
11267 511 : r = get_target_expr (r);
11268 : else
11269 : {
11270 564361 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
11271 564361 : TREE_CONSTANT (r) = true;
11272 : }
11273 : }
11274 :
11275 378558782 : if (TREE_CODE (t) == TARGET_EXPR
11276 476287 : && TREE_CODE (r) == TARGET_EXPR)
11277 : {
11278 : /* Preserve this flag for potential_constant_expression, and the others
11279 : for good measure. */
11280 476176 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
11281 476176 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
11282 476176 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
11283 476176 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
11284 : }
11285 :
11286 : /* Remember the original location if that wouldn't need a wrapper. */
11287 378558782 : if (location_t loc = EXPR_LOCATION (t))
11288 177829669 : protected_set_expr_location (r, loc);
11289 :
11290 378558782 : return r;
11291 510608237 : }
11292 :
11293 : /* If T represents a constant expression returns its reduced value.
11294 : Otherwise return error_mark_node. */
11295 :
11296 : tree
11297 141306893 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
11298 : tsubst_flags_t complain /* = tf_error */)
11299 : {
11300 141306893 : bool sfinae = !(complain & tf_error);
11301 141306893 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
11302 141306893 : if (sfinae && !TREE_CONSTANT (r))
11303 2553 : r = error_mark_node;
11304 141306893 : return r;
11305 : }
11306 :
11307 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
11308 : of constexpr variables. The actual initializer of DECL is not modified. */
11309 :
11310 : void
11311 198 : cxx_constant_dtor (tree t, tree decl)
11312 : {
11313 198 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
11314 198 : }
11315 :
11316 : /* Helper routine for fold_simple function. Either return simplified
11317 : expression T, otherwise NULL_TREE.
11318 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
11319 : even if we are within template-declaration. So be careful on call, as in
11320 : such case types can be undefined. */
11321 :
11322 : static tree
11323 159064011 : fold_simple_1 (tree t)
11324 : {
11325 159064011 : tree op1;
11326 159064011 : enum tree_code code = TREE_CODE (t);
11327 :
11328 159064011 : switch (code)
11329 : {
11330 : case INTEGER_CST:
11331 : case REAL_CST:
11332 : case VECTOR_CST:
11333 : case FIXED_CST:
11334 : case COMPLEX_CST:
11335 : return t;
11336 :
11337 1382983 : case SIZEOF_EXPR:
11338 1382983 : return fold_sizeof_expr (t);
11339 :
11340 37506996 : case ABS_EXPR:
11341 37506996 : case ABSU_EXPR:
11342 37506996 : case CONJ_EXPR:
11343 37506996 : case REALPART_EXPR:
11344 37506996 : case IMAGPART_EXPR:
11345 37506996 : case NEGATE_EXPR:
11346 37506996 : case BIT_NOT_EXPR:
11347 37506996 : case TRUTH_NOT_EXPR:
11348 37506996 : case VIEW_CONVERT_EXPR:
11349 37506996 : CASE_CONVERT:
11350 37506996 : case FLOAT_EXPR:
11351 37506996 : case FIX_TRUNC_EXPR:
11352 37506996 : case FIXED_CONVERT_EXPR:
11353 37506996 : case ADDR_SPACE_CONVERT_EXPR:
11354 :
11355 37506996 : op1 = TREE_OPERAND (t, 0);
11356 :
11357 37506996 : t = const_unop (code, TREE_TYPE (t), op1);
11358 37506996 : if (!t)
11359 : return NULL_TREE;
11360 :
11361 5589842 : if (CONVERT_EXPR_CODE_P (code)
11362 5589842 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
11363 0 : TREE_OVERFLOW (t) = false;
11364 : return t;
11365 :
11366 : default:
11367 : return NULL_TREE;
11368 : }
11369 : }
11370 :
11371 : /* If T is a simple constant expression, returns its simplified value.
11372 : Otherwise returns T. In contrast to maybe_constant_value we
11373 : simplify only few operations on constant-expressions, and we don't
11374 : try to simplify constexpressions. */
11375 :
11376 : tree
11377 159795063 : fold_simple (tree t)
11378 : {
11379 159795063 : if (processing_template_decl)
11380 : return t;
11381 :
11382 159064011 : tree r = fold_simple_1 (t);
11383 159064011 : if (r)
11384 : return r;
11385 :
11386 : return t;
11387 : }
11388 :
11389 : /* Try folding the expression T to a simple constant.
11390 : Returns that constant, otherwise returns T. */
11391 :
11392 : tree
11393 1548021 : fold_to_constant (tree t)
11394 : {
11395 1548021 : if (processing_template_decl)
11396 : return t;
11397 :
11398 1456127 : tree r = fold (t);
11399 1456127 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
11400 : return r;
11401 : else
11402 : return t;
11403 : }
11404 :
11405 : /* If T is a constant expression, returns its reduced value.
11406 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
11407 : Otherwise, returns a version of T without TREE_CONSTANT.
11408 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
11409 : as per P0595. */
11410 :
11411 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
11412 :
11413 : tree
11414 812899993 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
11415 : mce_value manifestly_const_eval /* = mce_unknown */)
11416 : {
11417 812899993 : tree orig_t = t;
11418 812899993 : tree r;
11419 :
11420 812899993 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
11421 : {
11422 : /* Look up each operand in the cv_cache first to see if we've already
11423 : reduced it, and reuse that result to avoid quadratic behavior if
11424 : we're called when building up a large expression. */
11425 233098686 : int n = cp_tree_operand_length (t);
11426 233098686 : tree *ops = XALLOCAVEC (tree, n);
11427 233098686 : bool rebuild = false;
11428 653828263 : for (int i = 0; i < n; ++i)
11429 : {
11430 420729577 : ops[i] = TREE_OPERAND (t, i);
11431 1261543845 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
11432 29070444 : if (*cached != ops[i])
11433 : {
11434 9059066 : ops[i] = *cached;
11435 9059066 : rebuild = true;
11436 : }
11437 : }
11438 233098686 : if (rebuild)
11439 : {
11440 7631640 : t = copy_node (t);
11441 24829409 : for (int i = 0; i < n; ++i)
11442 17197769 : TREE_OPERAND (t, i) = ops[i];
11443 : }
11444 : }
11445 :
11446 812899993 : if (!is_nondependent_constant_expression (t))
11447 : {
11448 0 : if (TREE_OVERFLOW_P (t)
11449 121558186 : || (!processing_template_decl && TREE_CONSTANT (t)))
11450 4491 : t = mark_non_constant (t);
11451 121558186 : return t;
11452 : }
11453 691341807 : else if (CONSTANT_CLASS_P (t))
11454 : /* No caching or evaluation needed. */
11455 : return t;
11456 :
11457 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11458 : but at least try folding it to a simple constant. */
11459 312843873 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11460 485050 : return fold_to_constant (t);
11461 :
11462 297103890 : if (manifestly_const_eval != mce_unknown)
11463 : /* TODO: Extend the cache to be mce_value aware. And if we have a
11464 : previously cached mce_unknown result that's TREE_CONSTANT, it means
11465 : the reduced value is independent of mce_value and so we should
11466 : be able to reuse it in the mce_true/false case. */
11467 161675170 : return cxx_eval_outermost_constant_expr (t, true, true,
11468 161672472 : manifestly_const_eval, false, decl);
11469 :
11470 150683653 : if (cv_cache == NULL)
11471 142487 : cv_cache = hash_map<tree, tree>::create_ggc (101);
11472 150683653 : if (tree *cached = cv_cache->get (t))
11473 : {
11474 12597616 : r = *cached;
11475 12597616 : if (r != t)
11476 : {
11477 : /* Clear processing_template_decl for sake of break_out_target_exprs;
11478 : entries in the cv_cache are non-templated. */
11479 4302930 : processing_template_decl_sentinel ptds;
11480 :
11481 4302930 : r = break_out_target_exprs (r, /*clear_loc*/true);
11482 4302930 : protected_set_expr_location (r, EXPR_LOCATION (t));
11483 4302930 : }
11484 12597616 : return r;
11485 : }
11486 :
11487 138086037 : uid_sensitive_constexpr_evaluation_checker c;
11488 138086037 : r = cxx_eval_outermost_constant_expr (t, true, true,
11489 : manifestly_const_eval, false, decl);
11490 138086037 : gcc_checking_assert (r == t
11491 : || CONVERT_EXPR_P (t)
11492 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11493 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11494 : || !cp_tree_equal (r, t));
11495 138086037 : if (!c.evaluation_restricted_p ())
11496 137851204 : cv_cache->put (orig_t, r);
11497 : return r;
11498 : }
11499 :
11500 : /* Dispose of the whole CV_CACHE. */
11501 :
11502 : static void
11503 38544800 : clear_cv_cache (void)
11504 : {
11505 38544800 : if (cv_cache != NULL)
11506 38285242 : cv_cache->empty ();
11507 38544800 : }
11508 :
11509 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
11510 :
11511 : void
11512 38544800 : clear_cv_and_fold_caches ()
11513 : {
11514 38544800 : clear_cv_cache ();
11515 38544800 : clear_fold_cache ();
11516 38544800 : }
11517 :
11518 : /* Internal function handling expressions in templates for
11519 : fold_non_dependent_expr and fold_non_dependent_init.
11520 :
11521 : If we're in a template, but T isn't value dependent, simplify
11522 : it. We're supposed to treat:
11523 :
11524 : template <typename T> void f(T[1 + 1]);
11525 : template <typename T> void f(T[2]);
11526 :
11527 : as two declarations of the same function, for example. */
11528 :
11529 : static tree
11530 29767942 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11531 : bool manifestly_const_eval,
11532 : tree object)
11533 : {
11534 29767942 : gcc_assert (processing_template_decl);
11535 :
11536 29767942 : if (is_nondependent_constant_expression (t))
11537 : {
11538 16542627 : processing_template_decl_sentinel s;
11539 16542627 : t = instantiate_non_dependent_expr_internal (t, complain);
11540 :
11541 16542627 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11542 : {
11543 0 : if (TREE_OVERFLOW_P (t))
11544 : {
11545 0 : t = build_nop (TREE_TYPE (t), t);
11546 0 : TREE_CONSTANT (t) = false;
11547 : }
11548 0 : return t;
11549 : }
11550 16542627 : else if (CONSTANT_CLASS_P (t))
11551 : /* No evaluation needed. */
11552 : return t;
11553 :
11554 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11555 : but at least try folding it to a simple constant. */
11556 4356140 : if (cp_unevaluated_operand && !manifestly_const_eval)
11557 89 : return fold_to_constant (t);
11558 :
11559 4356051 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11560 : mce_value (manifestly_const_eval),
11561 : false, object);
11562 : /* cp_tree_equal looks through NOPs, so allow them. */
11563 4356051 : gcc_checking_assert (r == t
11564 : || CONVERT_EXPR_P (t)
11565 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11566 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11567 : || !cp_tree_equal (r, t));
11568 4356051 : return r;
11569 16542627 : }
11570 13225315 : else if (TREE_OVERFLOW_P (t))
11571 : {
11572 0 : t = build_nop (TREE_TYPE (t), t);
11573 0 : TREE_CONSTANT (t) = false;
11574 : }
11575 :
11576 : return t;
11577 : }
11578 :
11579 : /* Like maybe_constant_value but first fully instantiate the argument.
11580 :
11581 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11582 : followed by maybe_constant_value but is more efficient,
11583 : because it calls instantiation_dependent_expression_p and
11584 : potential_constant_expression at most once.
11585 : The manifestly_const_eval argument is passed to maybe_constant_value.
11586 :
11587 : Callers should generally pass their active complain, or if they are in a
11588 : non-template, diagnosing context, they can use the default of
11589 : tf_warning_or_error. Callers that might be within a template context, don't
11590 : have a complain parameter, and aren't going to remember the result for long
11591 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11592 : appropriately. */
11593 :
11594 : tree
11595 247965359 : fold_non_dependent_expr (tree t,
11596 : tsubst_flags_t complain /* = tf_warning_or_error */,
11597 : bool manifestly_const_eval /* = false */,
11598 : tree object /* = NULL_TREE */)
11599 : {
11600 247965359 : if (t == NULL_TREE)
11601 : return NULL_TREE;
11602 :
11603 246907558 : if (processing_template_decl)
11604 28713368 : return fold_non_dependent_expr_template (t, complain,
11605 28713368 : manifestly_const_eval, object);
11606 :
11607 218194190 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11608 : }
11609 :
11610 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11611 : return the original expression. */
11612 :
11613 : tree
11614 2260747 : maybe_fold_non_dependent_expr (tree expr,
11615 : tsubst_flags_t complain/*=tf_warning_or_error*/)
11616 : {
11617 2260747 : tree t = fold_non_dependent_expr (expr, complain);
11618 2260747 : if (t && TREE_CONSTANT (t))
11619 1067445 : return t;
11620 :
11621 : return expr;
11622 : }
11623 :
11624 : /* Like maybe_constant_init but first fully instantiate the argument. */
11625 :
11626 : tree
11627 59418562 : fold_non_dependent_init (tree t,
11628 : tsubst_flags_t complain /*=tf_warning_or_error*/,
11629 : bool manifestly_const_eval /*=false*/,
11630 : tree object /* = NULL_TREE */)
11631 : {
11632 59418562 : if (t == NULL_TREE)
11633 : return NULL_TREE;
11634 :
11635 59418562 : if (processing_template_decl)
11636 : {
11637 1054574 : t = fold_non_dependent_expr_template (t, complain,
11638 : manifestly_const_eval, object);
11639 : /* maybe_constant_init does this stripping, so do it here too. */
11640 1054574 : if (TREE_CODE (t) == TARGET_EXPR)
11641 : {
11642 935 : tree init = TARGET_EXPR_INITIAL (t);
11643 935 : if (TREE_CODE (init) == CONSTRUCTOR)
11644 1054574 : t = init;
11645 : }
11646 1054574 : return t;
11647 : }
11648 :
11649 58363988 : return maybe_constant_init (t, object, manifestly_const_eval);
11650 : }
11651 :
11652 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11653 : than wrapped in a TARGET_EXPR.
11654 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11655 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11656 : per P0595 even when ALLOW_NON_CONSTANT is true. */
11657 :
11658 : static tree
11659 113350808 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11660 : mce_value manifestly_const_eval)
11661 : {
11662 113350808 : if (!t)
11663 : return t;
11664 113350808 : if (TREE_CODE (t) == EXPR_STMT)
11665 252 : t = TREE_OPERAND (t, 0);
11666 113350808 : if (TREE_CODE (t) == CONVERT_EXPR
11667 113350808 : && VOID_TYPE_P (TREE_TYPE (t)))
11668 2199 : t = TREE_OPERAND (t, 0);
11669 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11670 : DECL and losing that information would cause mischief later. */
11671 113350808 : if (TREE_CODE (t) == INIT_EXPR
11672 113350808 : && (!decl
11673 1965 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11674 1965 : TREE_TYPE (t))))
11675 1721 : t = TREE_OPERAND (t, 1);
11676 113350808 : if (TREE_CODE (t) == TARGET_EXPR)
11677 519425 : t = TARGET_EXPR_INITIAL (t);
11678 113350808 : if (!is_nondependent_static_init_expression (t))
11679 : /* Don't try to evaluate it. */;
11680 105438553 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11681 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11682 : else
11683 : {
11684 : /* [basic.start.static] allows constant-initialization of variables with
11685 : static or thread storage duration even if it isn't required, but we
11686 : shouldn't bend the rules the same way for automatic variables.
11687 :
11688 : But still enforce the requirements of constexpr/constinit.
11689 : [dcl.constinit] "If a variable declared with the constinit specifier
11690 : has dynamic initialization, the program is ill-formed, even if the
11691 : implementation would perform that initialization as a static
11692 : initialization." */
11693 18898757 : bool is_static = (decl && DECL_P (decl)
11694 61472055 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11695 3256272 : bool strict = (!is_static
11696 : || (decl && DECL_P (decl)
11697 3256272 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11698 942445 : || DECL_DECLARED_CONSTINIT_P (decl))));
11699 : if (is_static)
11700 : manifestly_const_eval = mce_true;
11701 :
11702 43283146 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11703 50889 : return fold_to_constant (t);
11704 :
11705 43232257 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11706 : manifestly_const_eval,
11707 : false, decl);
11708 : }
11709 113299919 : if (TREE_CODE (t) == TARGET_EXPR)
11710 : {
11711 88139 : tree init = TARGET_EXPR_INITIAL (t);
11712 88139 : if (TREE_CODE (init) == CONSTRUCTOR)
11713 113350808 : t = init;
11714 : }
11715 : return t;
11716 : }
11717 :
11718 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11719 :
11720 : tree
11721 93850505 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11722 : {
11723 93850505 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11724 : }
11725 :
11726 : tree
11727 19498614 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11728 : {
11729 19498614 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11730 : }
11731 :
11732 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11733 :
11734 : tree
11735 1689 : cxx_constant_init (tree t, tree decl)
11736 : {
11737 1689 : return maybe_constant_init_1 (t, decl, false, mce_true);
11738 : }
11739 :
11740 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11741 :
11742 : static bool
11743 196372020 : callee_might_throw (tree t)
11744 : {
11745 196372020 : if (cxx_dialect < cxx26 || !flag_exceptions)
11746 : return false;
11747 22604054 : tree callee = cp_get_callee (t);
11748 22604054 : if (callee == NULL_TREE)
11749 : return false;
11750 22558915 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11751 22558915 : return (!flag_enforce_eh_specs
11752 22558915 : || type_dependent_expression_p (callee)
11753 20408507 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11754 41506277 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11755 8507503 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11756 : }
11757 :
11758 : #if 0
11759 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11760 : /* Return true if the object referred to by REF has automatic or thread
11761 : local storage. */
11762 :
11763 : enum { ck_ok, ck_bad, ck_unknown };
11764 : static int
11765 : check_automatic_or_tls (tree ref)
11766 : {
11767 : machine_mode mode;
11768 : poly_int64 bitsize, bitpos;
11769 : tree offset;
11770 : int volatilep = 0, unsignedp = 0;
11771 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11772 : &mode, &unsignedp, &volatilep, false);
11773 : duration_kind dk;
11774 :
11775 : /* If there isn't a decl in the middle, we don't know the linkage here,
11776 : and this isn't a constant expression anyway. */
11777 : if (!DECL_P (decl))
11778 : return ck_unknown;
11779 : dk = decl_storage_duration (decl);
11780 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11781 : }
11782 : #endif
11783 :
11784 : /* Data structure for passing data from potential_constant_expression_1
11785 : to check_for_return_continue via cp_walk_tree. */
11786 : struct check_for_return_continue_data {
11787 : hash_set<tree> *pset;
11788 : tree continue_stmt;
11789 : tree break_stmt;
11790 : bool could_throw;
11791 : };
11792 :
11793 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11794 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11795 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11796 : For C++26 also note presence of possibly throwing calls. */
11797 : static tree
11798 47051454 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11799 : {
11800 47051454 : tree t = *tp, s, b;
11801 47051454 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11802 47051454 : switch (TREE_CODE (t))
11803 : {
11804 : case RETURN_EXPR:
11805 : return t;
11806 :
11807 28 : case CONTINUE_STMT:
11808 28 : if (d->continue_stmt == NULL_TREE)
11809 28 : d->continue_stmt = t;
11810 : break;
11811 :
11812 3180 : case BREAK_STMT:
11813 3180 : if (d->break_stmt == NULL_TREE)
11814 1313 : d->break_stmt = t;
11815 : break;
11816 :
11817 : #define RECUR(x) \
11818 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11819 : d->pset)) \
11820 : return r
11821 :
11822 : /* For loops, walk subtrees manually, so that continue stmts found
11823 : inside of the bodies of the loops are ignored. */
11824 39486 : case DO_STMT:
11825 39486 : *walk_subtrees = 0;
11826 39486 : RECUR (DO_COND (t));
11827 39486 : s = d->continue_stmt;
11828 39486 : b = d->break_stmt;
11829 39486 : RECUR (DO_BODY (t));
11830 39486 : d->continue_stmt = s;
11831 39486 : d->break_stmt = b;
11832 39486 : break;
11833 :
11834 119 : case WHILE_STMT:
11835 119 : *walk_subtrees = 0;
11836 119 : RECUR (WHILE_COND_PREP (t));
11837 119 : RECUR (WHILE_COND (t));
11838 119 : s = d->continue_stmt;
11839 119 : b = d->break_stmt;
11840 119 : RECUR (WHILE_BODY (t));
11841 107 : d->continue_stmt = s;
11842 107 : d->break_stmt = b;
11843 107 : break;
11844 :
11845 302 : case FOR_STMT:
11846 302 : *walk_subtrees = 0;
11847 302 : RECUR (FOR_INIT_STMT (t));
11848 302 : RECUR (FOR_COND_PREP (t));
11849 302 : RECUR (FOR_COND (t));
11850 302 : RECUR (FOR_EXPR (t));
11851 302 : s = d->continue_stmt;
11852 302 : b = d->break_stmt;
11853 302 : RECUR (FOR_BODY (t));
11854 272 : d->continue_stmt = s;
11855 272 : d->break_stmt = b;
11856 272 : break;
11857 :
11858 0 : case RANGE_FOR_STMT:
11859 0 : *walk_subtrees = 0;
11860 0 : RECUR (RANGE_FOR_EXPR (t));
11861 0 : s = d->continue_stmt;
11862 0 : b = d->break_stmt;
11863 0 : RECUR (RANGE_FOR_BODY (t));
11864 0 : d->continue_stmt = s;
11865 0 : d->break_stmt = b;
11866 0 : break;
11867 :
11868 209 : case SWITCH_STMT:
11869 209 : *walk_subtrees = 0;
11870 209 : RECUR (SWITCH_STMT_COND (t));
11871 209 : b = d->break_stmt;
11872 209 : RECUR (SWITCH_STMT_BODY (t));
11873 189 : d->break_stmt = b;
11874 189 : break;
11875 : #undef RECUR
11876 :
11877 : case STATEMENT_LIST:
11878 : case CONSTRUCTOR:
11879 : break;
11880 :
11881 2429407 : case AGGR_INIT_EXPR:
11882 2429407 : case CALL_EXPR:
11883 : /* In C++26 a function could throw. */
11884 2429407 : if (callee_might_throw (t))
11885 91657 : d->could_throw = true;
11886 : break;
11887 :
11888 43257647 : default:
11889 43257647 : if (!EXPR_P (t))
11890 13325921 : *walk_subtrees = 0;
11891 : break;
11892 : }
11893 :
11894 : return NULL_TREE;
11895 : }
11896 :
11897 : /* Return true if T denotes a potentially constant expression. Issue
11898 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11899 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11900 : consider the expression in the current context, independent of constexpr
11901 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11902 : and hard errors should not be reported by constexpr_error.
11903 :
11904 : C++0x [expr.const] used to say
11905 :
11906 : 6 An expression is a potential constant expression if it is
11907 : a constant expression where all occurrences of function
11908 : parameters are replaced by arbitrary constant expressions
11909 : of the appropriate type.
11910 :
11911 : 2 A conditional expression is a constant expression unless it
11912 : involves one of the following as a potentially evaluated
11913 : subexpression (3.2), but subexpressions of logical AND (5.14),
11914 : logical OR (5.15), and conditional (5.16) operations that are
11915 : not evaluated are not considered. */
11916 :
11917 : static bool
11918 4398234821 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11919 : bool fundef_p, tsubst_flags_t flags,
11920 : tree *jump_target)
11921 : {
11922 : #define RECUR(T,RV) \
11923 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11924 : jump_target)
11925 :
11926 4398234821 : enum { any = false, rval = true };
11927 4398234821 : int i;
11928 4398234821 : tree tmp;
11929 :
11930 4398234821 : if (t == error_mark_node)
11931 : return false;
11932 4398222963 : if (t == NULL_TREE)
11933 : return true;
11934 4390901407 : location_t loc = cp_expr_loc_or_input_loc (t);
11935 4390901407 : iloc_sentinel ils = loc;
11936 :
11937 4390901407 : if (*jump_target)
11938 : /* If we are jumping, ignore everything. This is simpler than the
11939 : cxx_eval_constant_expression handling because we only need to be
11940 : conservatively correct, and we don't necessarily have a constant value
11941 : available, so we don't bother with switch tracking. */
11942 : return true;
11943 :
11944 1706402 : if (TREE_THIS_VOLATILE (t) && want_rval
11945 1142734 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11946 4382721876 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11947 : {
11948 78385 : if (TREE_CLOBBER_P (t))
11949 : {
11950 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
11951 0 : gcc_checking_assert (false);
11952 : return true;
11953 : }
11954 :
11955 78385 : if (flags & tf_error)
11956 25 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11957 : "a volatile lvalue %qE with type %qT", t,
11958 25 : TREE_TYPE (t));
11959 78385 : return false;
11960 : }
11961 4382565080 : if (CONSTANT_CLASS_P (t))
11962 : return true;
11963 3138025179 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11964 3138025179 : && TREE_TYPE (t) == error_mark_node)
11965 : return false;
11966 :
11967 3138025136 : switch (TREE_CODE (t))
11968 : {
11969 : case FUNCTION_DECL:
11970 : case BASELINK:
11971 : case TEMPLATE_DECL:
11972 : case OVERLOAD:
11973 : case TEMPLATE_ID_EXPR:
11974 : case LABEL_DECL:
11975 : case CASE_LABEL_EXPR:
11976 : case PREDICT_EXPR:
11977 : case CONST_DECL:
11978 : case SIZEOF_EXPR:
11979 : case ALIGNOF_EXPR:
11980 : case OFFSETOF_EXPR:
11981 : case NOEXCEPT_EXPR:
11982 : case TEMPLATE_PARM_INDEX:
11983 : case TRAIT_EXPR:
11984 : case IDENTIFIER_NODE:
11985 : case USERDEF_LITERAL:
11986 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11987 : case FIELD_DECL:
11988 : case RESULT_DECL:
11989 : case USING_DECL:
11990 : case USING_STMT:
11991 : case PLACEHOLDER_EXPR:
11992 : case REQUIRES_EXPR:
11993 : case STATIC_ASSERT:
11994 : case DEBUG_BEGIN_STMT:
11995 : case REFLECT_EXPR:
11996 : return true;
11997 :
11998 23247801 : case RETURN_EXPR:
11999 23247801 : if (!RECUR (TREE_OPERAND (t, 0), any))
12000 : return false;
12001 : /* FALLTHROUGH */
12002 :
12003 23105996 : case BREAK_STMT:
12004 23105996 : case CONTINUE_STMT:
12005 23105996 : *jump_target = t;
12006 23105996 : return true;
12007 :
12008 327151202 : case PARM_DECL:
12009 327151202 : if (now && want_rval)
12010 : {
12011 83218910 : tree type = TREE_TYPE (t);
12012 83218910 : if (dependent_type_p (type)
12013 55681014 : || !COMPLETE_TYPE_P (processing_template_decl
12014 : ? type : complete_type (type))
12015 138899924 : || is_really_empty_class (type, /*ignore_vptr*/false))
12016 : /* An empty class has no data to read. */
12017 27538373 : return true;
12018 55680537 : if (flags & tf_error)
12019 18 : constexpr_error (input_location, fundef_p,
12020 : "%qE is not a constant expression", t);
12021 55680537 : return false;
12022 : }
12023 : return true;
12024 :
12025 236466293 : case AGGR_INIT_EXPR:
12026 236466293 : case CALL_EXPR:
12027 : /* -- an invocation of a function other than a constexpr function
12028 : or a constexpr constructor. */
12029 236466293 : {
12030 236466293 : tree fun = get_function_named_in_call (t);
12031 236466293 : const int nargs = call_expr_nargs (t);
12032 236466293 : i = 0;
12033 :
12034 236466293 : if (fun == NULL_TREE)
12035 : {
12036 : /* Reset to allow the function to continue past the end
12037 : of the block below. Otherwise return early. */
12038 402208 : bool bail = true;
12039 :
12040 402208 : if (TREE_CODE (t) == CALL_EXPR
12041 402208 : && CALL_EXPR_FN (t) == NULL_TREE)
12042 402208 : switch (CALL_EXPR_IFN (t))
12043 : {
12044 : /* These should be ignored, they are optimized away from
12045 : constexpr functions. */
12046 : case IFN_UBSAN_NULL:
12047 : case IFN_UBSAN_BOUNDS:
12048 : case IFN_UBSAN_VPTR:
12049 : case IFN_FALLTHROUGH:
12050 : case IFN_ASSUME:
12051 : return true;
12052 :
12053 : case IFN_ADD_OVERFLOW:
12054 : case IFN_SUB_OVERFLOW:
12055 : case IFN_MUL_OVERFLOW:
12056 : case IFN_LAUNDER:
12057 : case IFN_VEC_CONVERT:
12058 : case IFN_BSWAP:
12059 : case IFN_BITREVERSE:
12060 : bail = false;
12061 : break;
12062 :
12063 : default:
12064 : break;
12065 : }
12066 :
12067 : if (bail)
12068 : {
12069 : /* fold_call_expr can't do anything with IFN calls. */
12070 66 : if (flags & tf_error)
12071 0 : constexpr_error (loc, fundef_p,
12072 : "call to internal function %qE", t);
12073 66 : return false;
12074 : }
12075 : }
12076 :
12077 236064085 : if (fun && is_overloaded_fn (fun))
12078 : {
12079 218652946 : if (!RECUR (fun, true))
12080 : return false;
12081 218234465 : fun = get_fns (fun);
12082 :
12083 218234465 : if (TREE_CODE (fun) == FUNCTION_DECL)
12084 : {
12085 201269765 : if (builtin_valid_in_constant_expr_p (fun))
12086 : return true;
12087 199579849 : if (!maybe_constexpr_fn (fun)
12088 : /* Allow any built-in function; if the expansion
12089 : isn't constant, we'll deal with that then. */
12090 38851450 : && !fndecl_built_in_p (fun)
12091 : /* In C++20, replaceable global allocation functions
12092 : are constant expressions. */
12093 26697099 : && (!cxx_replaceable_global_alloc_fn (fun)
12094 706208 : || TREE_CODE (t) != CALL_EXPR
12095 706208 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
12096 264222 : && (current_function_decl == NULL_TREE
12097 264222 : || !is_std_allocator_allocate
12098 264222 : (current_function_decl))))
12099 : /* Allow placement new in std::construct_at. */
12100 25994983 : && (!cxx_placement_new_fn (fun)
12101 379660 : || TREE_CODE (t) != CALL_EXPR
12102 379660 : || current_function_decl == NULL_TREE
12103 379648 : || !is_std_construct_at (current_function_decl))
12104 25732995 : && !cxx_dynamic_cast_fn_p (fun)
12105 225309265 : && !cxx_cxa_builtin_fn_p (fun))
12106 : {
12107 : /* In C++26 evaluation of the function arguments might
12108 : throw and in that case it is irrelevant whether
12109 : fun is constexpr or not. */
12110 25690626 : if (cxx_dialect >= cxx26)
12111 4155064 : for (; i < nargs; ++i)
12112 : {
12113 2357240 : tree x = get_nth_callarg (t, i);
12114 2357240 : bool rv = processing_template_decl ? any : rval;
12115 2357240 : bool sub_now = false;
12116 2357240 : if (!potential_constant_expression_1 (x, rv, strict,
12117 : sub_now,
12118 : fundef_p,
12119 : flags,
12120 : jump_target))
12121 : return false;
12122 2216502 : if (throws (jump_target))
12123 : return true;
12124 : }
12125 25533990 : if ((flags & tf_error)
12126 25533990 : && constexpr_error (loc, fundef_p,
12127 : "call to non-%<constexpr%> "
12128 : "function %qD", fun))
12129 272 : explain_invalid_constexpr_fn (fun);
12130 25533990 : return false;
12131 : }
12132 : }
12133 :
12134 190853923 : fun = OVL_FIRST (fun);
12135 : /* Skip initial arguments to base constructors. */
12136 190853923 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12137 3919812 : i = num_artificial_parms_for (fun);
12138 : }
12139 17411139 : else if (fun)
12140 : {
12141 17411139 : if (TREE_TYPE (fun)
12142 17411139 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12143 : want_rval = rval;
12144 : else
12145 : want_rval = any;
12146 17411139 : if (RECUR (fun, want_rval))
12147 : /* Might end up being a constant function pointer. But it
12148 : could also be a function object with constexpr op(), so
12149 : we pass 'any' so that the underlying VAR_DECL is deemed
12150 : as potentially-constant even though it wasn't declared
12151 : constexpr. */;
12152 : else
12153 : return false;
12154 : }
12155 455282266 : for (; i < nargs; ++i)
12156 : {
12157 261301114 : tree x = get_nth_callarg (t, i);
12158 : /* In a template, reference arguments haven't been converted to
12159 : REFERENCE_TYPE and we might not even know if the parameter
12160 : is a reference, so accept lvalue constants too. */
12161 261301114 : bool rv = processing_template_decl ? any : rval;
12162 : /* Don't require an immediately constant value, as constexpr
12163 : substitution might not use the value of the argument. */
12164 261301114 : bool sub_now = false;
12165 261301114 : if (!potential_constant_expression_1 (x, rv, strict,
12166 : sub_now, fundef_p, flags,
12167 : jump_target))
12168 : return false;
12169 2875099200 : if (throws (jump_target))
12170 : return true;
12171 : }
12172 : /* In C++26 a function could throw. */
12173 193981152 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12174 7632245 : *jump_target = void_node;
12175 : return true;
12176 : }
12177 :
12178 189250509 : case NON_LVALUE_EXPR:
12179 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
12180 : -- an lvalue of integral type that refers to a non-volatile
12181 : const variable or static data member initialized with
12182 : constant expressions, or
12183 :
12184 : -- an lvalue of literal type that refers to non-volatile
12185 : object defined with constexpr, or that refers to a
12186 : sub-object of such an object; */
12187 189250509 : return RECUR (TREE_OPERAND (t, 0), rval);
12188 :
12189 34982 : case EXCESS_PRECISION_EXPR:
12190 34982 : return RECUR (TREE_OPERAND (t, 0), rval);
12191 :
12192 285864703 : case VAR_DECL:
12193 285864703 : if (DECL_HAS_VALUE_EXPR_P (t))
12194 : {
12195 3972055 : if (now && is_normal_capture_proxy (t))
12196 : {
12197 : /* -- in a lambda-expression, a reference to this or to a
12198 : variable with automatic storage duration defined outside that
12199 : lambda-expression, where the reference would be an
12200 : odr-use. */
12201 :
12202 359678 : if (want_rval)
12203 : /* Since we're doing an lvalue-rvalue conversion, this might
12204 : not be an odr-use, so evaluate the variable directly. */
12205 358555 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
12206 :
12207 1123 : if (flags & tf_error)
12208 : {
12209 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
12210 3 : auto_diagnostic_group d;
12211 3 : if (constexpr_error (input_location, fundef_p,
12212 : "lambda capture of %qE is not a "
12213 : "constant expression", cap)
12214 3 : && decl_constant_var_p (cap))
12215 3 : inform (input_location, "because it is used as a glvalue");
12216 3 : }
12217 1123 : return false;
12218 : }
12219 3612377 : tree ve = DECL_VALUE_EXPR (t);
12220 : /* Treat __PRETTY_FUNCTION__ inside a template function as
12221 : potentially-constant. */
12222 3612377 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
12223 : return true;
12224 3612372 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
12225 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
12226 3609636 : return RECUR (ve, rval);
12227 : }
12228 281892648 : if (want_rval
12229 201464522 : && (now || !var_in_maybe_constexpr_fn (t))
12230 184464630 : && !type_dependent_expression_p (t)
12231 153989196 : && !decl_maybe_constant_var_p (t)
12232 48914241 : && !is_local_temp (t)
12233 48404206 : && (strict
12234 1136468 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
12235 594454 : || (DECL_INITIAL (t)
12236 591548 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
12237 48400779 : && COMPLETE_TYPE_P (TREE_TYPE (t))
12238 330293277 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
12239 : {
12240 48395948 : if (flags & tf_error)
12241 167 : non_const_var_error (loc, t, fundef_p);
12242 48395948 : return false;
12243 : }
12244 : return true;
12245 :
12246 378172317 : case NOP_EXPR:
12247 378172317 : if (REINTERPRET_CAST_P (t))
12248 : {
12249 154304 : if (flags & tf_error)
12250 49 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
12251 : "constant expression");
12252 154304 : return false;
12253 : }
12254 : /* FALLTHRU */
12255 790964602 : case CONVERT_EXPR:
12256 790964602 : case VIEW_CONVERT_EXPR:
12257 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
12258 : may change to something more specific to type-punning (DR 1312). */
12259 790964602 : {
12260 790964602 : tree from = TREE_OPERAND (t, 0);
12261 790964602 : if (location_wrapper_p (t))
12262 343504921 : return (RECUR (from, want_rval));
12263 447459681 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
12264 : {
12265 246904366 : STRIP_ANY_LOCATION_WRAPPER (from);
12266 246904366 : if (TREE_CODE (from) == INTEGER_CST
12267 246904366 : && !integer_zerop (from))
12268 : {
12269 1580 : if (flags & tf_error)
12270 28 : constexpr_error (loc, fundef_p,
12271 : "%<reinterpret_cast%> from integer to "
12272 : "pointer");
12273 1580 : return false;
12274 : }
12275 : }
12276 447458101 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
12277 : }
12278 :
12279 12142 : case ADDRESSOF_EXPR:
12280 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
12281 12142 : t = TREE_OPERAND (t, 0);
12282 12142 : goto handle_addr_expr;
12283 :
12284 81353629 : case ADDR_EXPR:
12285 : /* -- a unary operator & that is applied to an lvalue that
12286 : designates an object with thread or automatic storage
12287 : duration; */
12288 81353629 : t = TREE_OPERAND (t, 0);
12289 :
12290 81353629 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
12291 : /* A pointer-to-member constant. */
12292 : return true;
12293 :
12294 81365509 : handle_addr_expr:
12295 : #if 0
12296 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
12297 : any checking here, as we might dereference the pointer later. If
12298 : we remove this code, also remove check_automatic_or_tls. */
12299 : i = check_automatic_or_tls (t);
12300 : if (i == ck_ok)
12301 : return true;
12302 : if (i == ck_bad)
12303 : {
12304 : if (flags & tf_error)
12305 : error ("address-of an object %qE with thread local or "
12306 : "automatic storage is not a constant expression", t);
12307 : return false;
12308 : }
12309 : #endif
12310 81365509 : return RECUR (t, any);
12311 :
12312 92102573 : case COMPONENT_REF:
12313 92102573 : case ARROW_EXPR:
12314 92102573 : case OFFSET_REF:
12315 : /* -- a class member access unless its postfix-expression is
12316 : of literal type or of pointer to literal type. */
12317 : /* This test would be redundant, as it follows from the
12318 : postfix-expression being a potential constant expression. */
12319 92102573 : if (type_unknown_p (t))
12320 : return true;
12321 84618070 : if (is_overloaded_fn (t))
12322 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
12323 : which uses ob as an lvalue. */
12324 86187245 : want_rval = false;
12325 86187245 : gcc_fallthrough ();
12326 :
12327 86187245 : case REALPART_EXPR:
12328 86187245 : case IMAGPART_EXPR:
12329 86187245 : case BIT_FIELD_REF:
12330 86187245 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12331 :
12332 255839 : case EXPR_PACK_EXPANSION:
12333 255839 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
12334 :
12335 : case PACK_INDEX_EXPR:
12336 : return true;
12337 :
12338 93181633 : case INDIRECT_REF:
12339 93181633 : return RECUR (TREE_OPERAND (t, 0), rval);
12340 :
12341 23781651 : case STATEMENT_LIST:
12342 4484460894 : for (tree stmt : tsi_range (t))
12343 70276350 : if (!RECUR (stmt, any))
12344 251969797 : return false;
12345 : return true;
12346 :
12347 4378203 : case MODIFY_EXPR:
12348 4378203 : if (cxx_dialect < cxx14)
12349 1976 : goto fail;
12350 4376227 : if (!RECUR (TREE_OPERAND (t, 0), any))
12351 : return false;
12352 : /* Just ignore clobbers. */
12353 3951989 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12354 : return true;
12355 3501375 : if (!RECUR (TREE_OPERAND (t, 1), rval))
12356 : return false;
12357 : return true;
12358 :
12359 872734 : case MODOP_EXPR:
12360 872734 : if (cxx_dialect < cxx14)
12361 98 : goto fail;
12362 872636 : if (!RECUR (TREE_OPERAND (t, 0), rval))
12363 : return false;
12364 862689 : if (!RECUR (TREE_OPERAND (t, 2), rval))
12365 : return false;
12366 : return true;
12367 :
12368 520899 : case DO_STMT:
12369 520899 : if (!RECUR (DO_COND (t), rval))
12370 : return false;
12371 520899 : if (!RECUR (DO_BODY (t), any))
12372 : return false;
12373 520328 : if (breaks (jump_target) || continues (jump_target))
12374 2 : *jump_target = NULL_TREE;
12375 : return true;
12376 :
12377 422492 : case FOR_STMT:
12378 422492 : if (!RECUR (FOR_INIT_STMT (t), any))
12379 : return false;
12380 422492 : if (!RECUR (FOR_COND_PREP (t), any))
12381 : return false;
12382 422492 : tmp = FOR_COND (t);
12383 422492 : if (!RECUR (tmp, rval))
12384 : return false;
12385 422424 : if (tmp)
12386 : {
12387 376649 : if (!processing_template_decl)
12388 373387 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12389 : /* If we couldn't evaluate the condition, it might not ever be
12390 : true. */
12391 376649 : if (!integer_onep (tmp))
12392 : {
12393 : /* Before returning true, check if the for body can contain
12394 : a return. */
12395 376649 : hash_set<tree> pset;
12396 376649 : check_for_return_continue_data data = { &pset, NULL_TREE,
12397 376649 : NULL_TREE, false };
12398 376649 : if (tree ret_expr
12399 376649 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
12400 : &data, &pset))
12401 131367 : *jump_target = ret_expr;
12402 376649 : if (data.could_throw)
12403 13525 : *jump_target = void_node;
12404 376649 : return true;
12405 376649 : }
12406 : }
12407 45775 : if (!RECUR (FOR_EXPR (t), any))
12408 : return false;
12409 45775 : if (!RECUR (FOR_BODY (t), any))
12410 : return false;
12411 45775 : if (!RECUR (FOR_COND_CLEANUP (t), any))
12412 : return false;
12413 45775 : if (breaks (jump_target) || continues (jump_target))
12414 12 : *jump_target = NULL_TREE;
12415 : return true;
12416 :
12417 472 : case RANGE_FOR_STMT:
12418 472 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
12419 : return false;
12420 472 : if (!RECUR (RANGE_FOR_EXPR (t), any))
12421 : return false;
12422 472 : if (!RECUR (RANGE_FOR_BODY (t), any))
12423 : return false;
12424 470 : if (breaks (jump_target) || continues (jump_target))
12425 0 : *jump_target = NULL_TREE;
12426 : return true;
12427 :
12428 159048 : case WHILE_STMT:
12429 159048 : if (!RECUR (WHILE_COND_PREP (t), any))
12430 : return false;
12431 159048 : tmp = WHILE_COND (t);
12432 159048 : if (!RECUR (tmp, rval))
12433 : return false;
12434 158984 : if (!processing_template_decl)
12435 158960 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12436 : /* If we couldn't evaluate the condition, it might not ever be true. */
12437 158984 : if (!integer_onep (tmp))
12438 : {
12439 : /* Before returning true, check if the while body can contain
12440 : a return. */
12441 149233 : hash_set<tree> pset;
12442 149233 : check_for_return_continue_data data = { &pset, NULL_TREE,
12443 149233 : NULL_TREE, false };
12444 149233 : if (tree ret_expr
12445 149233 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
12446 : &data, &pset))
12447 423 : *jump_target = ret_expr;
12448 149233 : if (data.could_throw)
12449 3291 : *jump_target = void_node;
12450 149233 : return true;
12451 149233 : }
12452 9751 : if (!RECUR (WHILE_BODY (t), any))
12453 : return false;
12454 9740 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
12455 : return false;
12456 9740 : if (breaks (jump_target) || continues (jump_target))
12457 24 : *jump_target = NULL_TREE;
12458 : return true;
12459 :
12460 38357 : case SWITCH_STMT:
12461 38357 : if (!RECUR (SWITCH_STMT_COND (t), rval))
12462 : return false;
12463 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
12464 : unreachable labels would be checked and it is enough if there is
12465 : a single switch cond value for which it is a valid constant
12466 : expression. We need to check if there are any RETURN_EXPRs
12467 : or CONTINUE_STMTs inside of the body though, as in that case
12468 : we need to set *jump_target. */
12469 : else
12470 : {
12471 38351 : hash_set<tree> pset;
12472 38351 : check_for_return_continue_data data = { &pset, NULL_TREE,
12473 38351 : NULL_TREE, false };
12474 38351 : if (tree ret_expr
12475 38351 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
12476 : &data, &pset))
12477 : /* The switch might return. */
12478 37966 : *jump_target = ret_expr;
12479 385 : else if (data.continue_stmt)
12480 : /* The switch can't return, but might continue. */
12481 3 : *jump_target = data.continue_stmt;
12482 38351 : if (data.could_throw)
12483 1318 : *jump_target = void_node;
12484 38351 : }
12485 38351 : return true;
12486 :
12487 50 : case STMT_EXPR:
12488 50 : return RECUR (STMT_EXPR_STMT (t), rval);
12489 :
12490 542692 : case LAMBDA_EXPR:
12491 542692 : if (cxx_dialect >= cxx17)
12492 : /* In C++17 lambdas can be constexpr, don't give up yet. */
12493 : return true;
12494 449 : else if (flags & tf_error)
12495 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
12496 : "constant expression before C++17");
12497 : return false;
12498 :
12499 108308 : case NEW_EXPR:
12500 108308 : case VEC_NEW_EXPR:
12501 108308 : case DELETE_EXPR:
12502 108308 : case VEC_DELETE_EXPR:
12503 108308 : if (cxx_dialect >= cxx20)
12504 : /* In C++20, new-expressions are potentially constant. */
12505 : return true;
12506 1077 : else if (flags & tf_error)
12507 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
12508 : "constant expression before C++20");
12509 : return false;
12510 :
12511 79644 : case DYNAMIC_CAST_EXPR:
12512 79644 : case PSEUDO_DTOR_EXPR:
12513 79644 : case OMP_PARALLEL:
12514 79644 : case OMP_TASK:
12515 79644 : case OMP_FOR:
12516 79644 : case OMP_SIMD:
12517 79644 : case OMP_DISTRIBUTE:
12518 79644 : case OMP_TASKLOOP:
12519 79644 : case OMP_LOOP:
12520 79644 : case OMP_TEAMS:
12521 79644 : case OMP_TARGET_DATA:
12522 79644 : case OMP_TARGET:
12523 79644 : case OMP_SECTIONS:
12524 79644 : case OMP_ORDERED:
12525 79644 : case OMP_CRITICAL:
12526 79644 : case OMP_SINGLE:
12527 79644 : case OMP_SCAN:
12528 79644 : case OMP_SCOPE:
12529 79644 : case OMP_SECTION:
12530 79644 : case OMP_MASTER:
12531 79644 : case OMP_MASKED:
12532 79644 : case OMP_TASKGROUP:
12533 79644 : case OMP_TARGET_UPDATE:
12534 79644 : case OMP_TARGET_ENTER_DATA:
12535 79644 : case OMP_TARGET_EXIT_DATA:
12536 79644 : case OMP_ATOMIC:
12537 79644 : case OMP_ATOMIC_READ:
12538 79644 : case OMP_ATOMIC_CAPTURE_OLD:
12539 79644 : case OMP_ATOMIC_CAPTURE_NEW:
12540 79644 : case OMP_DEPOBJ:
12541 79644 : case OACC_PARALLEL:
12542 79644 : case OACC_KERNELS:
12543 79644 : case OACC_SERIAL:
12544 79644 : case OACC_DATA:
12545 79644 : case OACC_HOST_DATA:
12546 79644 : case OACC_LOOP:
12547 79644 : case OACC_CACHE:
12548 79644 : case OACC_DECLARE:
12549 79644 : case OACC_ENTER_DATA:
12550 79644 : case OACC_EXIT_DATA:
12551 79644 : case OACC_UPDATE:
12552 79644 : case OMP_ARRAY_SECTION:
12553 : /* GCC internal stuff. */
12554 79644 : case VA_ARG_EXPR:
12555 79644 : case TRANSACTION_EXPR:
12556 79644 : case AT_ENCODE_EXPR:
12557 79644 : fail:
12558 79644 : if (flags & tf_error)
12559 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12560 : "expression", t);
12561 : return false;
12562 :
12563 : case OMP_DECLARE_MAPPER:
12564 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12565 : constant. */
12566 : return true;
12567 :
12568 21836 : case THROW_EXPR:
12569 21836 : if (cxx_dialect < cxx26)
12570 278 : goto fail;
12571 21558 : return RECUR (TREE_OPERAND (t, 0), rval);
12572 :
12573 527 : case ASM_EXPR:
12574 527 : if (flags & tf_error)
12575 6 : inline_asm_in_constexpr_error (loc, fundef_p);
12576 : return false;
12577 :
12578 304760 : case OBJ_TYPE_REF:
12579 304760 : if (cxx_dialect >= cxx20)
12580 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12581 : return true;
12582 5547 : else if (flags & tf_error)
12583 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12584 : "%<constexpr%> before C++20");
12585 : return false;
12586 :
12587 5436 : case TYPEID_EXPR:
12588 : /* In C++20, a typeid expression whose operand is of polymorphic
12589 : class type can be constexpr. */
12590 5436 : {
12591 5436 : tree e = TREE_OPERAND (t, 0);
12592 5436 : if (cxx_dialect < cxx20
12593 26 : && strict
12594 26 : && !TYPE_P (e)
12595 19 : && !type_dependent_expression_p (e)
12596 7 : && CLASS_TYPE_P (TREE_TYPE (e))
12597 5441 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12598 : {
12599 4 : if (flags & tf_error)
12600 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12601 : "constant expression because %qE is "
12602 : "of polymorphic type", e);
12603 4 : return false;
12604 : }
12605 : return true;
12606 : }
12607 :
12608 28638185 : case POINTER_DIFF_EXPR:
12609 28638185 : case MINUS_EXPR:
12610 28638185 : want_rval = true;
12611 28638185 : goto binary;
12612 :
12613 74828245 : case LT_EXPR:
12614 74828245 : case LE_EXPR:
12615 74828245 : case GT_EXPR:
12616 74828245 : case GE_EXPR:
12617 74828245 : case EQ_EXPR:
12618 74828245 : case NE_EXPR:
12619 74828245 : case SPACESHIP_EXPR:
12620 74828245 : want_rval = true;
12621 74828245 : goto binary;
12622 :
12623 1761939 : case PREINCREMENT_EXPR:
12624 1761939 : case POSTINCREMENT_EXPR:
12625 1761939 : case PREDECREMENT_EXPR:
12626 1761939 : case POSTDECREMENT_EXPR:
12627 1761939 : if (cxx_dialect < cxx14)
12628 8333 : goto fail;
12629 1753606 : goto unary;
12630 :
12631 1054619 : case BIT_NOT_EXPR:
12632 : /* A destructor. */
12633 1054619 : if (TYPE_P (TREE_OPERAND (t, 0)))
12634 : return true;
12635 : /* fall through. */
12636 :
12637 42663408 : case CONJ_EXPR:
12638 42663408 : case SAVE_EXPR:
12639 42663408 : case FIX_TRUNC_EXPR:
12640 42663408 : case FLOAT_EXPR:
12641 42663408 : case NEGATE_EXPR:
12642 42663408 : case ABS_EXPR:
12643 42663408 : case ABSU_EXPR:
12644 42663408 : case TRUTH_NOT_EXPR:
12645 42663408 : case FIXED_CONVERT_EXPR:
12646 42663408 : case UNARY_PLUS_EXPR:
12647 42663408 : case UNARY_LEFT_FOLD_EXPR:
12648 42663408 : case UNARY_RIGHT_FOLD_EXPR:
12649 42663408 : case VEC_DUPLICATE_EXPR:
12650 1054619 : unary:
12651 42663408 : return RECUR (TREE_OPERAND (t, 0), rval);
12652 :
12653 30041528 : case CAST_EXPR:
12654 30041528 : case CONST_CAST_EXPR:
12655 30041528 : case STATIC_CAST_EXPR:
12656 30041528 : case REINTERPRET_CAST_EXPR:
12657 30041528 : case IMPLICIT_CONV_EXPR:
12658 30041528 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12659 : /* In C++98, a conversion to non-integral type can't be part of a
12660 : constant expression. */
12661 : {
12662 295 : if (flags & tf_error)
12663 0 : constexpr_error (loc, fundef_p,
12664 : "cast to non-integral type %qT in a constant "
12665 0 : "expression", TREE_TYPE (t));
12666 295 : return false;
12667 : }
12668 : /* This might be a conversion from a class to a (potentially) literal
12669 : type. Let's consider it potentially constant since the conversion
12670 : might be a constexpr user-defined conversion. */
12671 30041233 : else if (cxx_dialect >= cxx11
12672 30021651 : && (dependent_type_p (TREE_TYPE (t))
12673 10511867 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12674 10498459 : || literal_type_p (TREE_TYPE (t)))
12675 29984994 : && TREE_OPERAND (t, 0)
12676 59795621 : && (TREE_CODE (t) != CAST_EXPR
12677 22551303 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12678 : {
12679 29713595 : tree from = TREE_OPERAND (t, 0);
12680 29713595 : if (TREE_CODE (t) == CAST_EXPR)
12681 22510510 : from = TREE_VALUE (from);
12682 29713595 : tree type = TREE_TYPE (from);
12683 : /* If this is a dependent type, it could end up being a class
12684 : with conversions. */
12685 29713595 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12686 : return true;
12687 : /* Or a non-dependent class which has conversions. */
12688 597121 : else if (CLASS_TYPE_P (type)
12689 597121 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12690 258681 : return true;
12691 : }
12692 :
12693 23901714 : return (RECUR (TREE_OPERAND (t, 0),
12694 23901714 : !TYPE_REF_P (TREE_TYPE (t))));
12695 :
12696 13249665 : case BIND_EXPR:
12697 13249665 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12698 :
12699 66824805 : case CLEANUP_POINT_EXPR:
12700 66824805 : case MUST_NOT_THROW_EXPR:
12701 66824805 : case TRY_CATCH_EXPR:
12702 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12703 : body could throw, even with catch (...) among handlers we'd need
12704 : to analyze them in detail if they couldn't rethrow it. More
12705 : importantly though, throws (jump_target) is just conservative,
12706 : and there could be e.g.
12707 : try
12708 : {
12709 : possibly_throwing_fn (args);
12710 : break;
12711 : }
12712 : catch (...)
12713 : {
12714 : }
12715 : or continue or return instead of break. So, clearing *jump_target
12716 : because we see catch (...) handler might mean we missed break
12717 : etc. */
12718 66824805 : case TRY_BLOCK:
12719 66824805 : case EH_SPEC_BLOCK:
12720 66824805 : case EXPR_STMT:
12721 66824805 : case PAREN_EXPR:
12722 : /* For convenience. */
12723 66824805 : case LOOP_EXPR:
12724 66824805 : case EXIT_EXPR:
12725 66824805 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12726 :
12727 11958110 : case DECL_EXPR:
12728 11958110 : tmp = DECL_EXPR_DECL (t);
12729 7772903 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12730 17678410 : && (processing_template_decl
12731 5262811 : ? !decl_maybe_constant_var_p (tmp)
12732 4805322 : : !decl_constant_var_p (tmp)))
12733 : {
12734 4669990 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12735 : {
12736 8 : if (flags & tf_error)
12737 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12738 : "%qD defined %<thread_local%> in "
12739 : "%<constexpr%> context", tmp);
12740 8 : return false;
12741 : }
12742 4669982 : else if (TREE_STATIC (tmp))
12743 : {
12744 141 : if (flags & tf_error)
12745 34 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12746 : "%qD defined %<static%> in %<constexpr%> "
12747 : "context", tmp);
12748 141 : return false;
12749 : }
12750 4669841 : else if (!check_for_uninitialized_const_var
12751 4669841 : (tmp, /*constexpr_context_p=*/true, flags))
12752 : return false;
12753 : }
12754 11954226 : if (VAR_P (tmp))
12755 7769019 : return RECUR (DECL_INITIAL (tmp), want_rval);
12756 : return true;
12757 :
12758 35605 : case TRY_FINALLY_EXPR:
12759 35605 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12760 35605 : && RECUR (TREE_OPERAND (t, 1), any));
12761 :
12762 0 : case EH_ELSE_EXPR:
12763 : /* maybe_apply_function_contracts uses this to check postconditions only
12764 : on normal return. */
12765 0 : return (RECUR (TREE_OPERAND (t, 1), any)
12766 0 : || RECUR (TREE_OPERAND (t, 0), any));
12767 :
12768 23495750 : case SCOPE_REF:
12769 23495750 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12770 :
12771 40583214 : case TARGET_EXPR:
12772 40583214 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12773 40390253 : && !TARGET_EXPR_ELIDING_P (t)
12774 72874811 : && !literal_type_p (TREE_TYPE (t)))
12775 : {
12776 768536 : if (flags & tf_error)
12777 : {
12778 25 : auto_diagnostic_group d;
12779 25 : if (constexpr_error (loc, fundef_p,
12780 : "temporary of non-literal type %qT in a "
12781 25 : "constant expression", TREE_TYPE (t)))
12782 25 : explain_non_literal_class (TREE_TYPE (t));
12783 25 : }
12784 768536 : return false;
12785 : }
12786 : /* FALLTHRU */
12787 66576084 : case INIT_EXPR:
12788 66576084 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12789 : return true;
12790 66512140 : return RECUR (TREE_OPERAND (t, 1), rval);
12791 :
12792 34061259 : case CONSTRUCTOR:
12793 34061259 : {
12794 34061259 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12795 34061259 : constructor_elt *ce;
12796 4640250696 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12797 216257577 : if (!RECUR (ce->value, want_rval))
12798 : return false;
12799 : return true;
12800 : }
12801 :
12802 20268857 : case TREE_LIST:
12803 20268857 : {
12804 20268857 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12805 : || DECL_P (TREE_PURPOSE (t)));
12806 20268857 : if (!RECUR (TREE_VALUE (t), want_rval))
12807 : return false;
12808 18163971 : if (TREE_CHAIN (t) == NULL_TREE)
12809 : return true;
12810 51966 : return RECUR (TREE_CHAIN (t), want_rval);
12811 : }
12812 :
12813 14603991 : case TRUNC_DIV_EXPR:
12814 14603991 : case CEIL_DIV_EXPR:
12815 14603991 : case FLOOR_DIV_EXPR:
12816 14603991 : case ROUND_DIV_EXPR:
12817 14603991 : case TRUNC_MOD_EXPR:
12818 14603991 : case CEIL_MOD_EXPR:
12819 14603991 : case ROUND_MOD_EXPR:
12820 14603991 : {
12821 14603991 : tree denom = TREE_OPERAND (t, 1);
12822 14603991 : if (!RECUR (denom, rval))
12823 : return false;
12824 : /* We can't call cxx_eval_outermost_constant_expr on an expression
12825 : that hasn't been through instantiate_non_dependent_expr yet. */
12826 14160785 : if (!processing_template_decl)
12827 7465025 : denom = cxx_eval_outermost_constant_expr (denom, true);
12828 14160785 : if (integer_zerop (denom))
12829 : {
12830 337 : if (flags & tf_error)
12831 35 : constexpr_error (input_location, fundef_p,
12832 : "division by zero is not a constant expression");
12833 337 : return false;
12834 : }
12835 : else
12836 : {
12837 14160448 : want_rval = true;
12838 14160448 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12839 : }
12840 : }
12841 :
12842 6535139 : case COMPOUND_EXPR:
12843 6535139 : {
12844 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12845 : COMPOUND_EXPR; don't get confused. */
12846 6535139 : tree op0 = TREE_OPERAND (t, 0);
12847 6535139 : tree op1 = TREE_OPERAND (t, 1);
12848 6535139 : STRIP_NOPS (op1);
12849 6535139 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12850 1205523 : return RECUR (op0, want_rval);
12851 : else
12852 5329616 : goto binary;
12853 : }
12854 :
12855 : /* If the first operand is the non-short-circuit constant, look at
12856 : the second operand; otherwise we only care about the first one for
12857 : potentiality. */
12858 17099370 : case TRUTH_AND_EXPR:
12859 17099370 : case TRUTH_ANDIF_EXPR:
12860 17099370 : tmp = boolean_true_node;
12861 17099370 : goto truth;
12862 6904943 : case TRUTH_OR_EXPR:
12863 6904943 : case TRUTH_ORIF_EXPR:
12864 6904943 : tmp = boolean_false_node;
12865 24004313 : truth:
12866 24004313 : {
12867 24004313 : tree op0 = TREE_OPERAND (t, 0);
12868 24004313 : tree op1 = TREE_OPERAND (t, 1);
12869 24004313 : if (!RECUR (op0, rval))
12870 : return false;
12871 16450428 : if (!(flags & tf_error) && RECUR (op1, rval))
12872 : /* When quiet, try to avoid expensive trial evaluation by first
12873 : checking potentiality of the second operand. */
12874 : return true;
12875 1689537 : if (!processing_template_decl)
12876 1299414 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12877 1689537 : if (tree_int_cst_equal (op0, tmp))
12878 5159 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12879 : else
12880 : return true;
12881 : }
12882 :
12883 : case PLUS_EXPR:
12884 : case MULT_EXPR:
12885 : case POINTER_PLUS_EXPR:
12886 : case RDIV_EXPR:
12887 : case EXACT_DIV_EXPR:
12888 : case MIN_EXPR:
12889 : case MAX_EXPR:
12890 : case LSHIFT_EXPR:
12891 : case RSHIFT_EXPR:
12892 : case LROTATE_EXPR:
12893 : case RROTATE_EXPR:
12894 : case BIT_IOR_EXPR:
12895 : case BIT_XOR_EXPR:
12896 : case BIT_AND_EXPR:
12897 : case TRUTH_XOR_EXPR:
12898 : case UNORDERED_EXPR:
12899 : case ORDERED_EXPR:
12900 : case UNLT_EXPR:
12901 : case UNLE_EXPR:
12902 : case UNGT_EXPR:
12903 : case UNGE_EXPR:
12904 : case UNEQ_EXPR:
12905 : case LTGT_EXPR:
12906 : case RANGE_EXPR:
12907 : case COMPLEX_EXPR:
12908 220061685 : want_rval = true;
12909 : /* Fall through. */
12910 220061685 : case ARRAY_REF:
12911 220061685 : case ARRAY_RANGE_REF:
12912 220061685 : case MEMBER_REF:
12913 220061685 : case DOTSTAR_EXPR:
12914 220061685 : case MEM_REF:
12915 220061685 : case BINARY_LEFT_FOLD_EXPR:
12916 220061685 : case BINARY_RIGHT_FOLD_EXPR:
12917 220061685 : binary:
12918 486764924 : for (i = 0; i < 2; ++i)
12919 359857839 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12920 : return false;
12921 : return true;
12922 :
12923 : case VEC_PERM_EXPR:
12924 106204 : for (i = 0; i < 3; ++i)
12925 105806 : if (!RECUR (TREE_OPERAND (t, i), true))
12926 : return false;
12927 : return true;
12928 :
12929 6195416 : case COND_EXPR:
12930 6195416 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12931 : {
12932 8 : if (flags & tf_error)
12933 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12934 : "constant expression");
12935 8 : return false;
12936 : }
12937 : /* Fall through. */
12938 16083347 : case IF_STMT:
12939 16083347 : case VEC_COND_EXPR:
12940 : /* If the condition is a known constant, we know which of the legs we
12941 : care about; otherwise we only require that the condition and
12942 : either of the legs be potentially constant. */
12943 16083347 : tmp = TREE_OPERAND (t, 0);
12944 16083347 : if (!RECUR (tmp, rval))
12945 : return false;
12946 14570023 : if (!processing_template_decl)
12947 12657543 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12948 : /* potential_constant_expression* isn't told if it is called for
12949 : manifestly_const_eval or not, so for consteval if always
12950 : process both branches as if the condition is not a known
12951 : constant. */
12952 14570023 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12953 : {
12954 14505498 : if (integer_zerop (tmp))
12955 3134927 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12956 11370571 : else if (TREE_CODE (tmp) == INTEGER_CST)
12957 3523370 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12958 : }
12959 7911726 : tmp = *jump_target;
12960 9032565 : for (i = 1; i < 3; ++i)
12961 : {
12962 8960013 : tree this_jump_target = tmp;
12963 8960013 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12964 : want_rval, strict, now, fundef_p,
12965 : tf_none, &this_jump_target))
12966 : {
12967 8059202 : if (returns (&this_jump_target) || throws (&this_jump_target))
12968 2258992 : *jump_target = this_jump_target;
12969 5580182 : else if (!returns (jump_target) && !throws (jump_target))
12970 : {
12971 5580182 : if (breaks (&this_jump_target)
12972 5580182 : || continues (&this_jump_target))
12973 42 : *jump_target = this_jump_target;
12974 5580182 : if (i == 1)
12975 : {
12976 : /* If the then branch is potentially constant, but
12977 : does not return, check if the else branch
12978 : couldn't return, break or continue. */
12979 4604965 : hash_set<tree> pset;
12980 4604965 : check_for_return_continue_data data = { &pset, NULL_TREE,
12981 : NULL_TREE,
12982 4604965 : false };
12983 9209930 : if (tree ret_expr
12984 4604965 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12985 : check_for_return_continue, &data,
12986 : &pset))
12987 73310 : *jump_target = ret_expr;
12988 4531655 : else if (*jump_target == NULL_TREE)
12989 : {
12990 4531616 : if (data.continue_stmt)
12991 0 : *jump_target = data.continue_stmt;
12992 4531616 : else if (data.break_stmt)
12993 13 : *jump_target = data.break_stmt;
12994 : }
12995 4604965 : if (data.could_throw)
12996 40518 : *jump_target = void_node;
12997 4604965 : }
12998 : }
12999 7839174 : return true;
13000 : }
13001 : }
13002 72552 : if (flags & tf_error)
13003 : {
13004 3 : if (TREE_CODE (t) == IF_STMT)
13005 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
13006 : "constant expression");
13007 : else
13008 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
13009 : "constant expression", t);
13010 : }
13011 : return false;
13012 :
13013 1066 : case VEC_INIT_EXPR:
13014 1066 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
13015 : return true;
13016 453 : if (flags & tf_error)
13017 : {
13018 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
13019 : "initialization"))
13020 2 : diagnose_non_constexpr_vec_init (t);
13021 : }
13022 : return false;
13023 :
13024 : case TYPE_DECL:
13025 : case TAG_DEFN:
13026 : /* We can see these in statement-expressions. */
13027 : return true;
13028 :
13029 1386648 : case CLEANUP_STMT:
13030 1386648 : if (!RECUR (CLEANUP_BODY (t), any))
13031 : return false;
13032 1386121 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
13033 : return false;
13034 : return true;
13035 :
13036 : case EMPTY_CLASS_EXPR:
13037 : return true;
13038 :
13039 29 : case GOTO_EXPR:
13040 29 : {
13041 29 : tree *target = &TREE_OPERAND (t, 0);
13042 : /* Gotos representing break, continue and cdtor return are OK. */
13043 29 : if (breaks (target) || continues (target) || returns (target))
13044 : {
13045 9 : *jump_target = *target;
13046 9 : return true;
13047 : }
13048 20 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
13049 : /* The user didn't write this goto, this isn't the problem. */
13050 : return true;
13051 16 : if (flags & tf_error)
13052 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
13053 : "expression");
13054 : return false;
13055 : }
13056 :
13057 : case ASSERTION_STMT:
13058 : case PRECONDITION_STMT:
13059 : case POSTCONDITION_STMT:
13060 : /* Contracts are not supposed to alter this; we have to check that this
13061 : is not violated at a later time. */
13062 : return true;
13063 :
13064 225 : case LABEL_EXPR:
13065 225 : t = LABEL_EXPR_LABEL (t);
13066 225 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
13067 : return true;
13068 25 : else if (flags & tf_error)
13069 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
13070 : "function only available with %<-std=c++23%> or "
13071 : "%<-std=gnu++23%>");
13072 : return false;
13073 :
13074 9819 : case ANNOTATE_EXPR:
13075 9819 : return RECUR (TREE_OPERAND (t, 0), rval);
13076 :
13077 131308 : case BIT_CAST_EXPR:
13078 131308 : return RECUR (TREE_OPERAND (t, 0), rval);
13079 :
13080 : /* Coroutine await, yield and return expressions are not. */
13081 718 : case CO_AWAIT_EXPR:
13082 718 : case CO_YIELD_EXPR:
13083 718 : case CO_RETURN_EXPR:
13084 718 : case TEMPLATE_FOR_STMT:
13085 718 : if (flags & tf_error)
13086 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
13087 : "%qE is not a constant expression", t);
13088 : return false;
13089 :
13090 : /* Assume a TU-local entity is not constant, we'll error later when
13091 : instantiating. */
13092 : case TU_LOCAL_ENTITY:
13093 : return false;
13094 :
13095 : /* A splice expression is dependent, but will be constant after
13096 : substitution. */
13097 : case SPLICE_EXPR:
13098 : return true;
13099 :
13100 30 : case NONTYPE_ARGUMENT_PACK:
13101 30 : {
13102 30 : tree args = ARGUMENT_PACK_ARGS (t);
13103 30 : int len = TREE_VEC_LENGTH (args);
13104 90 : for (int i = 0; i < len; ++i)
13105 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
13106 : return false;
13107 : return true;
13108 : }
13109 :
13110 0 : default:
13111 0 : if (objc_non_constant_expr_p (t))
13112 : return false;
13113 :
13114 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13115 0 : gcc_unreachable ();
13116 : return false;
13117 : }
13118 : #undef RECUR
13119 4390901407 : }
13120 :
13121 : bool
13122 1611950383 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13123 : bool fundef_p, tsubst_flags_t flags)
13124 : {
13125 1611950383 : if (flags & tf_error)
13126 : {
13127 : /* Check potentiality quietly first, as that could be performed more
13128 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13129 : that fails, replay the check noisily to give errors. */
13130 10235577 : flags &= ~tf_error;
13131 10235577 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13132 : flags))
13133 : return true;
13134 1062 : flags |= tf_error;
13135 : }
13136 :
13137 1601715868 : tree target = NULL_TREE;
13138 1601715868 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13139 1601715868 : flags, &target);
13140 : }
13141 :
13142 : /* The main entry point to the above. */
13143 :
13144 : bool
13145 422670663 : potential_constant_expression (tree t)
13146 : {
13147 422670663 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13148 : /*now*/false, /*fundef_p*/false,
13149 422670663 : tf_none);
13150 : }
13151 :
13152 : /* As above, but require a constant rvalue. */
13153 :
13154 : bool
13155 33347287 : potential_rvalue_constant_expression (tree t)
13156 : {
13157 33347287 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13158 : /*now*/false, /*fundef_p*/false,
13159 33347287 : tf_none);
13160 : }
13161 :
13162 : /* Like above, but complain about non-constant expressions. */
13163 :
13164 : bool
13165 88 : require_potential_constant_expression (tree t)
13166 : {
13167 88 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13168 : /*now*/false, /*fundef_p*/false,
13169 88 : tf_warning_or_error);
13170 : }
13171 :
13172 : /* Cross product of the above. */
13173 :
13174 : bool
13175 127075 : require_potential_rvalue_constant_expression (tree t)
13176 : {
13177 127075 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13178 : /*now*/false, /*fundef_p*/false,
13179 127075 : tf_warning_or_error);
13180 : }
13181 :
13182 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
13183 :
13184 : bool
13185 283 : require_potential_rvalue_constant_expression_fncheck (tree t)
13186 : {
13187 283 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13188 : /*now*/false, /*fundef_p*/true,
13189 283 : tf_warning_or_error);
13190 : }
13191 :
13192 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
13193 :
13194 : bool
13195 906 : require_rvalue_constant_expression (tree t)
13196 : {
13197 906 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13198 : /*now*/true, /*fundef_p*/false,
13199 906 : tf_warning_or_error);
13200 : }
13201 :
13202 : /* Like potential_constant_expression, but don't consider possible constexpr
13203 : substitution of the current function. That is, PARM_DECL qualifies under
13204 : potential_constant_expression, but not here.
13205 :
13206 : This is basically what you can check when any actual constant values might
13207 : be value-dependent. */
13208 :
13209 : bool
13210 872840925 : is_constant_expression (tree t)
13211 : {
13212 872840925 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13213 : /*now*/true, /*fundef_p*/false,
13214 872840925 : tf_none);
13215 : }
13216 :
13217 : /* As above, but expect an rvalue. */
13218 :
13219 : bool
13220 149269546 : is_rvalue_constant_expression (tree t)
13221 : {
13222 149269546 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13223 : /*now*/true, /*fundef_p*/false,
13224 149269546 : tf_none);
13225 : }
13226 :
13227 : /* Like above, but complain about non-constant expressions. */
13228 :
13229 : bool
13230 10107225 : require_constant_expression (tree t)
13231 : {
13232 10107225 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13233 : /*now*/true, /*fundef_p*/false,
13234 10107225 : tf_warning_or_error);
13235 : }
13236 :
13237 : /* Like is_constant_expression, but allow const variables that are not allowed
13238 : under constexpr rules. */
13239 :
13240 : bool
13241 113350808 : is_static_init_expression (tree t)
13242 : {
13243 113350808 : return potential_constant_expression_1 (t, /*want_rval*/false,
13244 : /*strict*/false, /*now*/true,
13245 113350808 : /*fundef_p*/false, tf_none);
13246 : }
13247 :
13248 : /* Returns true if T is a potential constant expression that is not
13249 : instantiation-dependent, and therefore a candidate for constant folding even
13250 : in a template. */
13251 :
13252 : bool
13253 843017080 : is_nondependent_constant_expression (tree t)
13254 : {
13255 843017080 : return (!type_unknown_p (t)
13256 843017031 : && is_constant_expression (t)
13257 1580781662 : && !instantiation_dependent_expression_p (t));
13258 : }
13259 :
13260 : /* Returns true if T is a potential static initializer expression that is not
13261 : instantiation-dependent. */
13262 :
13263 : bool
13264 113350808 : is_nondependent_static_init_expression (tree t)
13265 : {
13266 113350808 : return (!type_unknown_p (t)
13267 113350808 : && is_static_init_expression (t)
13268 218826449 : && !instantiation_dependent_expression_p (t));
13269 : }
13270 :
13271 : /* True iff FN is an implicitly constexpr function. */
13272 :
13273 : bool
13274 204971 : decl_implicit_constexpr_p (tree fn)
13275 : {
13276 204971 : if (!(flag_implicit_constexpr
13277 3 : && TREE_CODE (fn) == FUNCTION_DECL
13278 3 : && DECL_DECLARED_CONSTEXPR_P (fn)))
13279 : return false;
13280 :
13281 3 : if (DECL_CLONED_FUNCTION_P (fn))
13282 0 : fn = DECL_CLONED_FUNCTION (fn);
13283 :
13284 3 : return (DECL_LANG_SPECIFIC (fn)
13285 3 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
13286 : }
13287 :
13288 : /* Finalize constexpr processing after parsing. */
13289 :
13290 : void
13291 98071 : fini_constexpr (void)
13292 : {
13293 : /* The contexpr call and fundef copies tables are no longer needed. */
13294 98071 : constexpr_call_table = NULL;
13295 98071 : fundef_copies_table = NULL;
13296 98071 : }
13297 :
13298 : #include "gt-cp-constexpr.h"
|