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 195271265 : is_instantiation_of_constexpr (tree fun)
62 : {
63 270556841 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 120479532 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 290317143 : || (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 194949566 : literal_type_p (tree t)
73 : {
74 196605734 : if (SCALAR_TYPE_P (t)
75 92239715 : || VECTOR_TYPE_P (t)
76 92208062 : || TYPE_REF_P (t)
77 76440664 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : return true;
79 73144009 : if (CLASS_TYPE_P (t))
80 : {
81 71487629 : t = complete_type (t);
82 71487629 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 71487629 : return CLASSTYPE_LITERAL_P (t);
84 : }
85 1656380 : if (TREE_CODE (t) == ARRAY_TYPE)
86 1656168 : 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 339519161 : ensure_literal_type_for_constexpr_object (tree decl)
96 : {
97 339519161 : tree type = TREE_TYPE (decl);
98 339519161 : if (VAR_P (decl)
99 128128602 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 89434327 : || var_in_constexpr_fn (decl))
101 397899427 : && !processing_template_decl)
102 : {
103 37882963 : tree stype = strip_array_types (type);
104 37882963 : 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 37882957 : else if (!literal_type_p (type))
108 : {
109 24239 : 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 24186 : else if (cxx_dialect < cxx23)
119 : {
120 18406 : 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 18406 : cp_function_chain->invalid_constexpr = true;
131 : }
132 : }
133 37858718 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 37858718 : && 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 339519161 : 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 1044 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : const char *gmsgid, ...)
156 : {
157 1044 : diagnostics::diagnostic_info diagnostic;
158 1044 : va_list ap;
159 1044 : rich_location richloc (line_table, location);
160 1044 : va_start (ap, gmsgid);
161 1044 : bool ret;
162 1044 : if (!constexpr_fundef_p)
163 : {
164 : /* Report an error that cannot be suppressed. */
165 742 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
166 : diagnostics::kind::error);
167 742 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
168 : }
169 302 : 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 1044 : va_end (ap);
181 2088 : return ret;
182 1044 : }
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 987895747 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : const constexpr_fundef *rhs)
202 : {
203 960004676 : 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 954224302 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : {
212 954224302 : return DECL_UID (fundef->decl);
213 : }
214 :
215 : /* Return a previously saved definition of function FUN. */
216 :
217 : constexpr_fundef *
218 110552825 : retrieve_constexpr_fundef (tree fun)
219 : {
220 110552825 : if (constexpr_fundef_table == NULL)
221 : return NULL;
222 :
223 110548807 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 110548807 : 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 31985318 : is_valid_constexpr_fn (tree fun, bool complain)
232 : {
233 31985318 : bool ret = true;
234 :
235 63970636 : if (DECL_INHERITED_CTOR (fun)
236 3706332 : && 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 31985318 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 64593156 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 32607838 : if (!literal_type_p (TREE_TYPE (parm)))
248 : {
249 13014 : ret = false;
250 13014 : 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 53039224 : 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 63970630 : 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 31985315 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : {
279 27599444 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 27599444 : if (!literal_type_p (rettype))
281 : {
282 50902 : ret = false;
283 50902 : 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 27599444 : if (cxx_dialect < cxx14
295 32976 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 27600567 : && !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 4385871 : 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 31985318 : 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 4549 : 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 4549 : auto_vec<tree, 2> fields;
344 9194 : do
345 : {
346 9194 : fields.safe_push (TREE_OPERAND (member, 1));
347 9194 : member = TREE_OPERAND (member, 0);
348 : }
349 9194 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 18394 : && 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 9194 : while (field = fields.pop(),
357 9194 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : {
359 4645 : tree ctor;
360 : /* If there is already an outer constructor entry for the anonymous
361 : aggregate FIELD, use it; otherwise, insert one. */
362 4645 : if (vec_safe_is_empty (*vec)
363 198 : || (*vec)->last().index != field)
364 : {
365 4543 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 4543 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : }
368 : else
369 102 : ctor = (*vec)->last().value;
370 4645 : 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 4549 : gcc_assert (fields.is_empty());
376 4549 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 :
378 4549 : return true;
379 4549 : }
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 6127768 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : {
390 6839435 : tree member, init;
391 6839435 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 3119667 : t = TREE_OPERAND (t, 0);
393 6839435 : if (TREE_CODE (t) == EXPR_STMT)
394 3118453 : t = TREE_OPERAND (t, 0);
395 6839435 : if (t == error_mark_node)
396 : return false;
397 6839426 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : {
399 1614805 : for (tree stmt : tsi_range (t))
400 908031 : if (! build_data_member_initialization (stmt, vec))
401 6127768 : return false;
402 : return true;
403 : }
404 6132652 : 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 677472 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : }
413 5455180 : if (TREE_CODE (t) == CONVERT_EXPR)
414 3044283 : t = TREE_OPERAND (t, 0);
415 5455180 : if (TREE_CODE (t) == INIT_EXPR)
416 : {
417 2898426 : member = TREE_OPERAND (t, 0);
418 2898426 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : }
420 2556754 : else if (TREE_CODE (t) == CALL_EXPR)
421 : {
422 2079186 : tree fn = get_callee_fndecl (t);
423 4158372 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : /* We're only interested in calls to subobject constructors. */
425 : return true;
426 1665885 : 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 1665885 : init = break_out_target_exprs (t);
431 : }
432 477568 : else if (TREE_CODE (t) == BIND_EXPR)
433 34195 : 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 4564311 : if (INDIRECT_REF_P (member))
438 61805 : member = TREE_OPERAND (member, 0);
439 4564311 : if (TREE_CODE (member) == NOP_EXPR)
440 : {
441 564626 : tree op = member;
442 564626 : STRIP_NOPS (op);
443 564626 : if (TREE_CODE (op) == ADDR_EXPR)
444 : {
445 680 : 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 563946 : else if (op == current_class_ptr
453 1127741 : && (same_type_ignoring_top_level_qualifiers_p
454 563795 : (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 486195 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : }
464 : }
465 4564311 : if (TREE_CODE (member) == ADDR_EXPR)
466 1163744 : member = TREE_OPERAND (member, 0);
467 4564311 : if (TREE_CODE (member) == COMPONENT_REF)
468 : {
469 3974515 : tree aggr = TREE_OPERAND (member, 0);
470 3974515 : if (TREE_CODE (aggr) == VAR_DECL)
471 : /* Initializing a local variable, don't add anything. */
472 : return true;
473 3974513 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : /* Normal member initialization. */
475 3969961 : member = TREE_OPERAND (member, 1);
476 4552 : else if (VAR_P (get_base_address (aggr)))
477 : /* Initializing a local variable, don't add anything. */
478 : return true;
479 4549 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
480 : /* Initializing a member of an anonymous union. */
481 4549 : 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 5692660 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
491 80 : (*vec)->last().value = init;
492 : else
493 4559677 : 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 2402 : check_constexpr_bind_expr_vars (tree t)
503 : {
504 2402 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
505 :
506 3240 : 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 4137 : check_constexpr_ctor_body_1 (tree last, tree list)
518 : {
519 4137 : 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 2059 : case BIND_EXPR:
532 2059 : if (!check_constexpr_bind_expr_vars (list)
533 2059 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
534 : /*complain=*/false))
535 : 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 6069847 : 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 6069847 : if (cxx_dialect >= cxx14)
556 : return true;
557 :
558 13845 : bool ok = true;
559 13845 : if (TREE_CODE (list) == STATEMENT_LIST)
560 : {
561 13817 : tree_stmt_iterator i = tsi_last (list);
562 17855 : for (; !tsi_end_p (i); tsi_prev (&i))
563 : {
564 15727 : tree t = tsi_stmt (i);
565 15727 : if (t == last)
566 : break;
567 4109 : if (!check_constexpr_ctor_body_1 (last, t))
568 : {
569 : ok = false;
570 : break;
571 : }
572 : }
573 : }
574 28 : else if (list != last
575 28 : && !check_constexpr_ctor_body_1 (last, list))
576 : ok = false;
577 13845 : if (!ok && complain)
578 : {
579 50 : pedwarn (input_location, OPT_Wc__14_extensions,
580 : "%<constexpr%> constructor does not have empty body");
581 50 : 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 3627762 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
594 : {
595 3627762 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
596 3627762 : tree field_type;
597 3627762 : unsigned i;
598 3627762 : constructor_elt *ce;
599 :
600 3627762 : if (pri)
601 89326 : field_type = BINFO_TYPE (pri);
602 3538436 : else if (TYPE_CONTAINS_VPTR_P (type))
603 37767 : 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 168930 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
610 131039 : if (TREE_TYPE (ce->index) == field_type)
611 : break;
612 :
613 149417 : 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 3705470 : build_constexpr_constructor_member_initializers (tree type, tree body)
630 : {
631 3705470 : vec<constructor_elt, va_gc> *vec = NULL;
632 3705470 : bool ok = true;
633 5781064 : while (true)
634 5781064 : switch (TREE_CODE (body))
635 : {
636 2075515 : case MUST_NOT_THROW_EXPR:
637 2075515 : case EH_SPEC_BLOCK:
638 2075515 : case TRY_FINALLY_EXPR: // For C++26 postconditions.
639 2075515 : body = TREE_OPERAND (body, 0);
640 2075515 : break;
641 :
642 79 : case STATEMENT_LIST:
643 2075779 : for (tree stmt : tsi_range (body))
644 : {
645 161 : body = stmt;
646 161 : if (TREE_CODE (body) == BIND_EXPR)
647 : break;
648 : }
649 : break;
650 :
651 3705470 : case BIND_EXPR:
652 3705470 : body = BIND_EXPR_BODY (body);
653 3705470 : goto found;
654 :
655 0 : default:
656 0 : gcc_unreachable ();
657 : }
658 3705470 : found:
659 3705470 : 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 3705470 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
666 : {
667 2004760 : body = TREE_OPERAND (body, 0);
668 2004760 : if (TREE_CODE (body) == EXPR_STMT)
669 2004724 : body = TREE_OPERAND (body, 0);
670 2004760 : if (TREE_CODE (body) == INIT_EXPR
671 2004760 : && (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 2004760 : ok = build_data_member_initialization (body, &vec);
679 : }
680 1700710 : else if (TREE_CODE (body) == STATEMENT_LIST)
681 : {
682 4904870 : for (tree stmt : tsi_range (body))
683 : {
684 3209569 : ok = build_data_member_initialization (stmt, &vec);
685 3209569 : if (!ok)
686 : break;
687 : }
688 : }
689 5408 : else if (EXPR_P (body))
690 5408 : ok = build_data_member_initialization (body, &vec);
691 : else
692 0 : gcc_assert (errorcount > 0);
693 3705470 : if (ok)
694 : {
695 3705461 : if (vec_safe_length (vec) > 0)
696 : {
697 : /* In a delegating constructor, return the target. */
698 3431277 : constructor_elt *ce = &(*vec)[0];
699 3431277 : if (ce->index == current_class_ptr)
700 : {
701 77699 : body = ce->value;
702 77699 : vec_free (vec);
703 77699 : return body;
704 : }
705 : }
706 3627762 : vec = sort_constexpr_mem_initializers (type, vec);
707 3627762 : 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 566932574 : get_function_named_in_call (tree t)
719 : {
720 566932574 : tree callee = cp_get_callee (t);
721 566932574 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
722 566932574 : 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 70609 : constexpr_fn_retval (tree body)
732 : {
733 77425 : switch (TREE_CODE (body))
734 : {
735 18785 : case STATEMENT_LIST:
736 18785 : {
737 18785 : tree expr = NULL_TREE;
738 56285 : for (tree stmt : tsi_range (body))
739 : {
740 37529 : tree s = constexpr_fn_retval (stmt);
741 37529 : if (s == error_mark_node)
742 : return error_mark_node;
743 37502 : else if (s == NULL_TREE)
744 : /* Keep iterating. */;
745 18750 : else if (expr)
746 : /* Multiple return statements. */
747 : return error_mark_node;
748 : else
749 : expr = s;
750 : }
751 : return expr;
752 : }
753 :
754 33030 : case RETURN_EXPR:
755 33030 : 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 6479 : case CLEANUP_POINT_EXPR:
768 6479 : 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 31272401 : massage_constexpr_body (tree fun, tree body)
799 : {
800 62544802 : if (DECL_CONSTRUCTOR_P (fun))
801 3705470 : body = build_constexpr_constructor_member_initializers
802 3705470 : (DECL_CONTEXT (fun), body);
803 27566931 : else if (cxx_dialect < cxx14)
804 : {
805 32845 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
806 1 : body = EH_SPEC_STMTS (body);
807 32845 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
808 21455 : body = TREE_OPERAND (body, 0);
809 32845 : body = constexpr_fn_retval (body);
810 : }
811 31272401 : 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 3358770 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
819 : {
820 : /* We allow uninitialized bases/fields in C++20. */
821 3358770 : if (cxx_dialect >= cxx20)
822 : return false;
823 :
824 13697 : unsigned nelts = 0;
825 :
826 13697 : if (body)
827 : {
828 13693 : if (TREE_CODE (body) != CONSTRUCTOR)
829 : return false;
830 13640 : nelts = CONSTRUCTOR_NELTS (body);
831 : }
832 13644 : tree field = TYPE_FIELDS (ctype);
833 :
834 13644 : 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 : return true;
842 : }
843 : 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 28789 : for (unsigned i = 0; i <= nelts; ++i)
850 : {
851 28789 : tree index = NULL_TREE;
852 28789 : if (i < nelts)
853 : {
854 15255 : index = CONSTRUCTOR_ELT (body, i)->index;
855 : /* Skip vptr adjustment represented with a COMPONENT_REF. */
856 15255 : if (TREE_CODE (index) != FIELD_DECL)
857 627 : continue;
858 : }
859 :
860 497910 : for (; field != index; field = DECL_CHAIN (field))
861 : {
862 469754 : tree ftype;
863 469754 : if (TREE_CODE (field) != FIELD_DECL)
864 469726 : 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 28156 : if (field == NULL_TREE)
906 : break;
907 :
908 14628 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
909 : {
910 : /* Check the anonymous aggregate initializer is valid. */
911 38 : 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 14628 : 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 168531756 : maybe_save_constexpr_fundef (tree fun)
928 : {
929 168531756 : if (processing_template_decl
930 69710442 : || cp_function_chain->invalid_constexpr
931 238223901 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
932 137259649 : 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 53299277 : bool implicit = false;
937 53299277 : 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 53299277 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
952 : return;
953 :
954 31328009 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
955 :
956 31328009 : if (!is_valid_constexpr_fn (fun, complain))
957 : return;
958 :
959 31272321 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
960 31272321 : 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 : return;
966 : }
967 :
968 31272285 : bool potential = potential_rvalue_constant_expression (massaged);
969 31272285 : if (!potential && complain)
970 258 : require_potential_rvalue_constant_expression_fncheck (massaged);
971 :
972 34977732 : if (DECL_CONSTRUCTOR_P (fun) && potential
973 34973700 : && !DECL_DEFAULTED_FN (fun))
974 : {
975 3358714 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
976 : massaged, complain))
977 : potential = false;
978 3358688 : 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 3353857 : massaged = DECL_SAVED_TREE (fun);
983 3353857 : potential = potential_rvalue_constant_expression (massaged);
984 3353857 : if (!potential && complain)
985 20 : require_potential_rvalue_constant_expression_fncheck (massaged);
986 : }
987 : }
988 :
989 31272285 : 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 298 : && warn_invalid_constexpr != 0)
995 : return;
996 :
997 31272118 : 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 31272107 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1013 31272107 : bool clear_ctx = false;
1014 31272107 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1015 : {
1016 31272107 : clear_ctx = true;
1017 31272107 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1018 : }
1019 31272107 : tree saved_fn = current_function_decl;
1020 31272107 : current_function_decl = fun;
1021 31272107 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1022 31272107 : current_function_decl = saved_fn;
1023 31272107 : if (clear_ctx)
1024 31272107 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1025 31272107 : 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 60968 : entry.result = error_mark_node;
1030 :
1031 31272107 : 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 31310290 : register_constexpr_fundef (const constexpr_fundef &value)
1039 : {
1040 : /* Create the constexpr function table if necessary. */
1041 31310290 : if (constexpr_fundef_table == NULL)
1042 27674 : constexpr_fundef_table
1043 27674 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1044 :
1045 31310290 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1046 31310290 : (const_cast<constexpr_fundef *> (&value), INSERT);
1047 :
1048 31310290 : gcc_assert (*slot == NULL);
1049 31310290 : *slot = ggc_alloc<constexpr_fundef> ();
1050 31310290 : **slot = value;
1051 31310290 : }
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 1063 : explain_invalid_constexpr_fn (tree fun)
1061 : {
1062 1063 : static hash_set<tree> *diagnosed;
1063 1063 : tree body;
1064 :
1065 : /* Don't try to explain a function we already complained about. */
1066 1063 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1067 859 : if (f->language->erroneous)
1068 1063 : 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 1014 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1075 : /* Go on. */;
1076 : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1077 978 : else if (!DECL_DEFAULTED_FN (fun)
1078 1230 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1079 941 : && !(flag_implicit_constexpr
1080 9 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1081 9 : && DECL_DECLARED_INLINE_P (fun))
1082 1913 : && !is_instantiation_of_constexpr (fun))
1083 : {
1084 917 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1085 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1086 920 : && decl_defined_p (fun))
1087 3 : inform (DECL_SOURCE_LOCATION (fun),
1088 : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1089 : 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 65094136 : 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). If __cxa_begin_catch or
1197 : __cxa_get_exception_ptr need to create temporaries, the VAR_DECL of the
1198 : exception object is wrapped in the vector into a TREE_LIST where
1199 : TREE_VALUE of it is the VAR_DECL of the exception object and TREE_PURPOSE
1200 : one of the temporaries, others chained through DECL_CHAIN. */
1201 : auto_vec<tree, 2> caught_exceptions;
1202 : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1203 : vec<tree> *cleanups;
1204 : /* If non-null, only allow modification of existing values of the variables
1205 : in this set. Set by modifiable_tracker, below. */
1206 : hash_set<tree> *modifiable;
1207 : /* If cxx_eval_outermost_constant_expr is called on the consteval block
1208 : operator (), this is the FUNCTION_DECL of that operator (). */
1209 : tree consteval_block;
1210 : /* Number of heap VAR_DECL deallocations. */
1211 : unsigned heap_dealloc_count;
1212 : /* Number of uncaught exceptions. */
1213 : unsigned uncaught_exceptions;
1214 : /* A contract statement that failed or was not constant, we only store the
1215 : first one that fails. */
1216 : tree contract_statement;
1217 : /* [basic.contract.eval]/7.3 if this expression would otherwise be constant
1218 : then a non-const contract makes the program ill-formed. */
1219 : bool contract_condition_non_const;
1220 : /* Some metafunctions aren't dependent just on their arguments, but also
1221 : on various other dependencies, e.g. has_identifier on a function parameter
1222 : reflection can change depending on further declarations of corresponding
1223 : function, is_complete_type depends on type definitions and template
1224 : specializations in between the calls, define_aggregate even defines
1225 : class types, etc. Thus, we need to arrange for calls which call
1226 : at least some metafunctions to be non-cacheable, because their behavior
1227 : might not be the same. Until we figure out which exact metafunctions
1228 : need this and which don't, do it for all of them.
1229 : Also used for some cases in constexpr EH, e.g. __cxa_rethrow,
1230 : __builtin_uncaught_exceptions and __builtin_current_exception, which can
1231 : be also dependent on some state (pending uncaught or caught exceptions)
1232 : not tracked in the constexpr call caching. */
1233 : bool state_dependent;
1234 :
1235 : /* Constructor. */
1236 568832578 : constexpr_global_ctx ()
1237 568832578 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1238 568832578 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1239 568832578 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1240 568832578 : contract_condition_non_const (false), state_dependent (false) {}
1241 :
1242 464023931 : bool is_outside_lifetime (tree t)
1243 : {
1244 464023931 : if (tree *p = values.get (t))
1245 208803984 : if (*p == void_node)
1246 223 : return true;
1247 : return false;
1248 : }
1249 586934384 : tree get_value (tree t)
1250 : {
1251 586934384 : if (tree *p = values.get (t))
1252 307110796 : if (*p != void_node)
1253 298926588 : return *p;
1254 : return NULL_TREE;
1255 : }
1256 124950954 : tree *get_value_ptr (tree t, bool initializing)
1257 : {
1258 124950954 : if (modifiable && !modifiable->contains (t))
1259 : return nullptr;
1260 124950943 : if (tree *p = values.get (t))
1261 : {
1262 124941412 : if (*p != void_node)
1263 : return p;
1264 48 : else if (initializing)
1265 : {
1266 6 : *p = NULL_TREE;
1267 6 : return p;
1268 : }
1269 : }
1270 : return nullptr;
1271 : }
1272 301655493 : void put_value (tree t, tree v)
1273 : {
1274 301655493 : bool already_in_map = values.put (t, v);
1275 301655493 : if (!already_in_map && modifiable)
1276 30 : modifiable->add (t);
1277 301655493 : }
1278 241716607 : void destroy_value (tree t)
1279 : {
1280 241716607 : if (TREE_CODE (t) == VAR_DECL
1281 241716607 : || TREE_CODE (t) == PARM_DECL
1282 : || TREE_CODE (t) == RESULT_DECL)
1283 241707400 : values.put (t, void_node);
1284 : else
1285 9207 : values.remove (t);
1286 241716607 : }
1287 19274793 : void clear_value (tree t)
1288 : {
1289 38549586 : values.remove (t);
1290 : }
1291 : };
1292 :
1293 : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1294 : side-effects from evaluation of a particular subexpression of a
1295 : constant-expression. In such cases we use modifiable_tracker to prevent
1296 : modification of variables created outside of that subexpression.
1297 :
1298 : ??? We could change the hash_set to a hash_map, allow and track external
1299 : modifications, and roll them back in the destructor. It's not clear to me
1300 : that this would be worthwhile. */
1301 :
1302 : class modifiable_tracker
1303 : {
1304 : hash_set<tree> set;
1305 : constexpr_global_ctx *global;
1306 : public:
1307 173601 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1308 : {
1309 173601 : global->modifiable = &set;
1310 173601 : }
1311 173601 : ~modifiable_tracker ()
1312 : {
1313 173631 : for (tree t: set)
1314 30 : global->clear_value (t);
1315 173601 : global->modifiable = nullptr;
1316 173601 : }
1317 : };
1318 :
1319 : /* The constexpr expansion context. CALL is the current function
1320 : expansion, CTOR is the current aggregate initializer, OBJECT is the
1321 : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1322 :
1323 : struct constexpr_ctx {
1324 : /* The part of the context that needs to be unique to the whole
1325 : cxx_eval_outermost_constant_expr invocation. */
1326 : constexpr_global_ctx *global;
1327 : /* The innermost call we're evaluating. */
1328 : constexpr_call *call;
1329 : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1330 : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1331 : vec<tree> *save_exprs;
1332 : /* The CONSTRUCTOR we're currently building up for an aggregate
1333 : initializer. */
1334 : tree ctor;
1335 : /* The object we're building the CONSTRUCTOR for. */
1336 : tree object;
1337 : /* If inside SWITCH_EXPR. */
1338 : constexpr_switch_state *css_state;
1339 : /* The aggregate initialization context inside which this one is nested. This
1340 : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1341 : const constexpr_ctx *parent;
1342 :
1343 : /* Whether we should error on a non-constant expression or fail quietly.
1344 : This flag needs to be here, but some of the others could move to global
1345 : if they get larger than a word. */
1346 : bool quiet;
1347 : /* Whether we are strictly conforming to constant expression rules or
1348 : trying harder to get a constant value. */
1349 : bool strict;
1350 : /* Whether __builtin_is_constant_evaluated () should be true. */
1351 : mce_value manifestly_const_eval;
1352 : };
1353 :
1354 : /* Return ctx->quiet. For use in reflect.cc. */
1355 :
1356 : bool
1357 1376 : cxx_constexpr_quiet_p (const constexpr_ctx *ctx)
1358 : {
1359 1376 : return ctx->quiet;
1360 : }
1361 :
1362 : /* Return ctx->manifestly_const_eval. For use in reflect.cc. */
1363 :
1364 : mce_value
1365 400 : cxx_constexpr_manifestly_const_eval (const constexpr_ctx *ctx)
1366 : {
1367 400 : return ctx->manifestly_const_eval;
1368 : }
1369 :
1370 : /* Return ctx->call->fundef->decl or NULL_TREE. For use in
1371 : reflect.cc. */
1372 :
1373 : tree
1374 2128 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1375 : {
1376 2128 : if (ctx->call)
1377 958 : return ctx->call->fundef->decl;
1378 : else
1379 : return NULL_TREE;
1380 : }
1381 :
1382 : /* Return ctx->global->consteval_block. For use in reflect.cc. */
1383 :
1384 : tree
1385 158 : cxx_constexpr_consteval_block (const constexpr_ctx *ctx)
1386 : {
1387 158 : return ctx->global->consteval_block;
1388 : }
1389 :
1390 : /* Predicates for the meaning of *jump_target. */
1391 :
1392 : static bool
1393 110644709 : returns (tree *jump_target)
1394 : {
1395 19390770 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1396 : }
1397 :
1398 : static bool
1399 105075248 : breaks (tree *jump_target)
1400 : {
1401 105075248 : return (*jump_target
1402 105075248 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1403 339 : && LABEL_DECL_BREAK (*jump_target))
1404 1374831 : || TREE_CODE (*jump_target) == BREAK_STMT
1405 1236204 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1406 : }
1407 :
1408 : static bool
1409 141968830 : continues (tree *jump_target)
1410 : {
1411 141968830 : return (*jump_target
1412 141968830 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1413 339 : && LABEL_DECL_CONTINUE (*jump_target))
1414 1252658 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1415 : }
1416 :
1417 : static bool
1418 12815084 : switches (tree *jump_target)
1419 : {
1420 78012 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1421 : }
1422 :
1423 : static bool
1424 1900909758 : throws (tree *jump_target)
1425 : {
1426 : /* void_node is for use in potential_constant_expression_1, otherwise
1427 : it should an artificial VAR_DECL created by constant evaluation
1428 : of __cxa_allocate_exception (). */
1429 212736048 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1430 : }
1431 :
1432 : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1433 : references and objects are in effect. */
1434 :
1435 : static bool
1436 270911344 : p2280_active_p (const constexpr_ctx *ctx)
1437 : {
1438 34551682 : if (ctx->manifestly_const_eval != mce_true)
1439 : /* Disable these relaxations during speculative constexpr folding,
1440 : as it can significantly increase compile time/memory use
1441 : (PR119387). */
1442 : return false;
1443 :
1444 : /* P2280R4 was accepted as a DR against C++11. */
1445 0 : return cxx_dialect >= cxx11;
1446 : }
1447 :
1448 : /* Remove T from the global values map, checking for attempts to destroy
1449 : a value that has already finished its lifetime. */
1450 :
1451 : static void
1452 241329145 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1453 : {
1454 241329145 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1455 : return;
1456 :
1457 : /* Don't error again here if we've already reported a problem. */
1458 241329084 : if (!*non_constant_p
1459 210010354 : && DECL_P (t)
1460 : /* Non-trivial destructors have their lifetimes ended explicitly
1461 : with a clobber, so don't worry about it here. */
1462 210001345 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1463 : /* ...except parameters are remapped in cxx_eval_call_expression,
1464 : and the destructor call during cleanup won't be able to tell that
1465 : this value has already been destroyed, so complain now. This is
1466 : not quite unobservable, but is extremely unlikely to crop up in
1467 : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1468 774033 : || TREE_CODE (t) == PARM_DECL)
1469 450557931 : && ctx->global->is_outside_lifetime (t))
1470 : {
1471 54 : if (!ctx->quiet)
1472 : {
1473 18 : auto_diagnostic_group d;
1474 18 : error ("destroying %qE outside its lifetime", t);
1475 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1476 18 : }
1477 54 : *non_constant_p = true;
1478 : }
1479 241329084 : ctx->global->destroy_value (t);
1480 : }
1481 :
1482 : /* This internal flag controls whether we should avoid doing anything during
1483 : constexpr evaluation that would cause extra DECL_UID generation, such as
1484 : template instantiation and function body copying. */
1485 :
1486 : static bool uid_sensitive_constexpr_evaluation_value;
1487 :
1488 : /* An internal counter that keeps track of the number of times
1489 : uid_sensitive_constexpr_evaluation_p returned true. */
1490 :
1491 : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1492 :
1493 : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1494 : increments the corresponding counter. */
1495 :
1496 : static bool
1497 9090455 : uid_sensitive_constexpr_evaluation_p ()
1498 : {
1499 9003233 : if (uid_sensitive_constexpr_evaluation_value)
1500 : {
1501 256716 : ++uid_sensitive_constexpr_evaluation_true_counter;
1502 256707 : return true;
1503 : }
1504 : else
1505 : return false;
1506 : }
1507 :
1508 : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1509 : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1510 : during the lifetime of the sentinel object. Upon its destruction, the
1511 : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1512 :
1513 81454919 : uid_sensitive_constexpr_evaluation_sentinel
1514 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1515 81454919 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1516 : {
1517 81454919 : }
1518 :
1519 : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1520 : records the current number of times that uid_sensitive_constexpr_evaluation_p
1521 : has been called and returned true. */
1522 :
1523 3873013285 : uid_sensitive_constexpr_evaluation_checker
1524 : ::uid_sensitive_constexpr_evaluation_checker ()
1525 3873013285 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1526 : {
1527 3873013285 : }
1528 :
1529 : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1530 : some constexpr evaluation was restricted due to u_s_c_e_p being called
1531 : and returning true during the lifetime of this checker object. */
1532 :
1533 : bool
1534 2568807544 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1535 : {
1536 2568807544 : return (uid_sensitive_constexpr_evaluation_value
1537 2568807544 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1538 : }
1539 :
1540 :
1541 : /* A table of all constexpr calls that have been evaluated by the
1542 : compiler in this translation unit. */
1543 :
1544 : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1545 :
1546 : /* Compute a hash value for a constexpr call representation. */
1547 :
1548 : inline hashval_t
1549 235394073 : constexpr_call_hasher::hash (constexpr_call *info)
1550 : {
1551 235394073 : return info->hash;
1552 : }
1553 :
1554 : /* Return true if the objects pointed to by P and Q represent calls
1555 : to the same constexpr function with the same arguments.
1556 : Otherwise, return false. */
1557 :
1558 : bool
1559 244786285 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1560 : {
1561 244786285 : if (lhs == rhs)
1562 : return true;
1563 244786285 : if (lhs->hash != rhs->hash)
1564 : return false;
1565 27891071 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1566 : return false;
1567 27891071 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1568 : }
1569 :
1570 : /* Initialize the constexpr call table, if needed. */
1571 :
1572 : static void
1573 36925659 : maybe_initialize_constexpr_call_table (void)
1574 : {
1575 36925659 : if (constexpr_call_table == NULL)
1576 18673 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1577 36925659 : }
1578 :
1579 : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1580 : a function happens to get called recursively, we unshare the callee
1581 : function's body and evaluate this unshared copy instead of evaluating the
1582 : original body.
1583 :
1584 : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1585 : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1586 : that's keyed off of the original FUNCTION_DECL and whose value is a
1587 : TREE_LIST of this function's unused copies awaiting reuse.
1588 :
1589 : This is not GC-deletable to avoid GC affecting UID generation. */
1590 :
1591 : static GTY(()) decl_tree_map *fundef_copies_table;
1592 :
1593 : /* Reuse a copy or create a new unshared copy of the function FUN.
1594 : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1595 : is parms, TYPE is result. */
1596 :
1597 : static tree
1598 86602775 : get_fundef_copy (constexpr_fundef *fundef)
1599 : {
1600 86602775 : tree copy;
1601 86602775 : bool existed;
1602 86602775 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1603 86602775 : (fundef_copies_table, fundef->decl, &existed, 127));
1604 :
1605 86602775 : if (!existed)
1606 : {
1607 : /* There is no cached function available, or in use. We can use
1608 : the function directly. That the slot is now created records
1609 : that this function is now in use. */
1610 7631424 : copy = build_tree_list (fundef->body, fundef->parms);
1611 7631424 : TREE_TYPE (copy) = fundef->result;
1612 : }
1613 78971351 : else if (*slot == NULL_TREE)
1614 : {
1615 4438 : if (uid_sensitive_constexpr_evaluation_p ())
1616 9 : return NULL_TREE;
1617 :
1618 : /* We've already used the function itself, so make a copy. */
1619 4429 : copy = build_tree_list (NULL, NULL);
1620 4429 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1621 4429 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1622 4429 : tree saved_result = DECL_RESULT (fundef->decl);
1623 4429 : tree saved_fn = current_function_decl;
1624 4429 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1625 4429 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1626 4429 : DECL_RESULT (fundef->decl) = fundef->result;
1627 4429 : current_function_decl = fundef->decl;
1628 4429 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1629 4429 : TREE_TYPE (copy));
1630 4429 : current_function_decl = saved_fn;
1631 4429 : DECL_RESULT (fundef->decl) = saved_result;
1632 4429 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1633 4429 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1634 : }
1635 : else
1636 : {
1637 : /* We have a cached function available. */
1638 78966913 : copy = *slot;
1639 78966913 : *slot = TREE_CHAIN (copy);
1640 : }
1641 :
1642 : return copy;
1643 : }
1644 :
1645 : /* Save the copy COPY of function FUN for later reuse by
1646 : get_fundef_copy(). By construction, there will always be an entry
1647 : to find. */
1648 :
1649 : static void
1650 86602762 : save_fundef_copy (tree fun, tree copy)
1651 : {
1652 86602762 : tree *slot = fundef_copies_table->get (fun);
1653 86602762 : TREE_CHAIN (copy) = *slot;
1654 86602762 : *slot = copy;
1655 86602762 : }
1656 :
1657 : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1658 : value_cat, bool *, bool *, tree *);
1659 : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1660 : bool *, tree *);
1661 : static tree find_heap_var_refs (tree *, int *, void *);
1662 :
1663 : /* For exception object EXC if it has class type and usable what () method
1664 : which returns cv char * return the xmalloced string literal which it returns
1665 : if possible, otherwise return NULL. */
1666 :
1667 : static char *
1668 396 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1669 : {
1670 396 : tree type = strip_array_types (TREE_TYPE (exc));
1671 396 : if (!CLASS_TYPE_P (type))
1672 : return NULL;
1673 340 : tree std_exception = lookup_qualified_name (std_node, "exception",
1674 : LOOK_want::NORMAL, false);
1675 340 : if (TREE_CODE (std_exception) != TYPE_DECL)
1676 : return NULL;
1677 334 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1678 : return NULL;
1679 334 : base_kind b_kind;
1680 334 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1681 : tf_none);
1682 334 : if (binfo == NULL_TREE || binfo == error_mark_node)
1683 : return NULL;
1684 326 : if (type != TREE_TYPE (exc))
1685 288 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1686 326 : tree call
1687 326 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1688 : tf_none);
1689 326 : if (call == error_mark_node)
1690 : return NULL;
1691 326 : releasing_vec what_args;
1692 326 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1693 326 : if (call == error_mark_node)
1694 : return NULL;
1695 326 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1696 326 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1697 326 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1698 326 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1699 326 : TYPE_SIZE_UNIT (char_type_node))
1700 652 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1701 : return NULL;
1702 326 : if (!potential_constant_expression (call))
1703 : return NULL;
1704 326 : bool non_constant_p = false, overflow_p = false;
1705 326 : tree jmp_target = NULL;
1706 326 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1707 : &non_constant_p, &overflow_p,
1708 : &jmp_target);
1709 652 : if (throws (&jmp_target) || non_constant_p)
1710 : return NULL;
1711 326 : if (reduced_constant_expression_p (ptr))
1712 42 : if (const char *msg = c_getstr (ptr))
1713 42 : return xstrdup (msg);
1714 284 : auto_vec <char, 32> v;
1715 11754 : for (unsigned i = 0; i < INT_MAX; ++i)
1716 : {
1717 11754 : tree t = call;
1718 11754 : if (i)
1719 11470 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1720 11754 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1721 11754 : non_constant_p = false;
1722 11754 : overflow_p = false;
1723 11754 : jmp_target = NULL;
1724 11754 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1725 : &non_constant_p, &overflow_p,
1726 : &jmp_target);
1727 11754 : if (throws (&jmp_target)
1728 11754 : || non_constant_p
1729 11754 : || !tree_fits_shwi_p (t2))
1730 0 : return NULL;
1731 11754 : char c = tree_to_shwi (t2);
1732 11754 : v.safe_push (c);
1733 11754 : if (c == '\0')
1734 : break;
1735 : }
1736 568 : return xstrdup (v.address ());
1737 610 : }
1738 :
1739 : /* Diagnose constant expression evaluation encountering call to
1740 : std::terminate due to exception EXC. */
1741 :
1742 : static void
1743 22 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1744 : {
1745 22 : tree type = strip_array_types (TREE_TYPE (exc));
1746 22 : if (char *str = exception_what_str (ctx, exc))
1747 : {
1748 4 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1749 : "%<what()%>: %qs", "std::terminate", type, str);
1750 4 : free (str);
1751 : }
1752 : else
1753 : {
1754 18 : if (type != TREE_TYPE (exc))
1755 18 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1756 18 : bool non_constant_p = false, overflow_p = false;
1757 18 : tree jmp_target = NULL;
1758 18 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1759 : &non_constant_p, &overflow_p,
1760 : &jmp_target);
1761 18 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1762 18 : if (reduced_constant_expression_p (val))
1763 18 : error_at (loc, "%qs called after throwing an exception %qE",
1764 : "std::terminate", val);
1765 : else
1766 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1767 : "std::terminate", type);
1768 : }
1769 22 : }
1770 :
1771 : /* Diagnose constant expression evaluation encountering call to
1772 : uncaught exception EXC. */
1773 :
1774 : static void
1775 374 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1776 : {
1777 374 : tree type = strip_array_types (TREE_TYPE (exc));
1778 374 : if (char *str = exception_what_str (ctx, exc))
1779 : {
1780 322 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1781 322 : free (str);
1782 : }
1783 : else
1784 : {
1785 52 : if (type != TREE_TYPE (exc))
1786 52 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1787 52 : bool non_constant_p = false, overflow_p = false;
1788 52 : tree jmp_target = NULL;
1789 52 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1790 : &non_constant_p, &overflow_p,
1791 : &jmp_target);
1792 52 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1793 52 : if (reduced_constant_expression_p (val))
1794 50 : error_at (loc, "uncaught exception %qE", val);
1795 : else
1796 2 : error_at (loc, "uncaught exception of type %qT", type);
1797 : }
1798 374 : }
1799 :
1800 : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1801 : magic constexpr functions for C++26. */
1802 :
1803 : enum cxa_builtin {
1804 : CXA_NONE = 0,
1805 : CXA_ALLOCATE_EXCEPTION = 1,
1806 : CXA_FREE_EXCEPTION = 2,
1807 : CXA_THROW = 3,
1808 : CXA_BEGIN_CATCH = 4,
1809 : CXA_END_CATCH = 5,
1810 : CXA_RETHROW = 6,
1811 : CXA_GET_EXCEPTION_PTR = 7,
1812 : CXA_BAD_CAST = 8,
1813 : CXA_BAD_TYPEID = 9,
1814 : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1815 : STD_RETHROW_EXCEPTION = 11,
1816 : BUILTIN_EH_PTR_ADJUST_REF = 12,
1817 : BUILTIN_UNCAUGHT_EXCEPTIONS = 13,
1818 : BUILTIN_CURRENT_EXCEPTION = 14
1819 : };
1820 :
1821 : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1822 : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1823 :
1824 : static enum cxa_builtin
1825 36908366 : cxx_cxa_builtin_fn_p (tree fndecl)
1826 : {
1827 36908366 : if (cxx_dialect < cxx26)
1828 : return CXA_NONE;
1829 3647718 : if (DECL_LANGUAGE (fndecl) != lang_c)
1830 : {
1831 3044812 : if (!decl_in_std_namespace_p (fndecl))
1832 : return CXA_NONE;
1833 1286401 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1834 : return STD_RETHROW_EXCEPTION;
1835 1286359 : return CXA_NONE;
1836 : }
1837 602906 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1838 : return CXA_NONE;
1839 80133 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1840 : return CXA_ALLOCATE_EXCEPTION;
1841 24750 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1842 : return CXA_FREE_EXCEPTION;
1843 24748 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1844 : return CXA_THROW;
1845 15724 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1846 : return CXA_BEGIN_CATCH;
1847 4647 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1848 : return CXA_END_CATCH;
1849 1080 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1850 : return CXA_RETHROW;
1851 974 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1852 : return CXA_GET_EXCEPTION_PTR;
1853 930 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1854 : return CXA_BAD_CAST;
1855 634 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1856 : return CXA_BAD_TYPEID;
1857 621 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1858 33 : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1859 : return CXA_NONE;
1860 : }
1861 :
1862 : /* Helper function for cxx_eval_cxa_builtin_fn.
1863 : Check if ARG is a valid first argument of __cxa_throw or
1864 : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1865 : not, otherwise return the artificial __cxa_allocate_exception allocated
1866 : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1867 :
1868 : static tree
1869 1656 : cxa_check_throw_arg (tree arg, bool free_exc)
1870 : {
1871 1656 : STRIP_NOPS (arg);
1872 1656 : if (TREE_CODE (arg) != ADDR_EXPR)
1873 : return NULL_TREE;
1874 1656 : arg = TREE_OPERAND (arg, 0);
1875 1656 : if (!VAR_P (arg)
1876 1656 : || !DECL_ARTIFICIAL (arg)
1877 1656 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1878 1656 : && DECL_NAME (arg) != heap_identifier)
1879 3312 : || !DECL_LANG_SPECIFIC (arg))
1880 0 : return NULL_TREE;
1881 : return arg;
1882 : }
1883 :
1884 : /* Helper function for cxx_eval_cxa_builtin_fn.
1885 : "Allocate" on the constexpr heap an exception object of TYPE
1886 : with REFCOUNT. */
1887 :
1888 : static tree
1889 17594 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1890 : tree refcount)
1891 : {
1892 17594 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1893 17594 : DECL_ARTIFICIAL (var) = 1;
1894 17594 : retrofit_lang_decl (var);
1895 17594 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1896 17594 : ctx->global->heap_vars.safe_push (var);
1897 17594 : return var;
1898 : }
1899 :
1900 : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1901 : C++26 constexpr exception support (P3068R5). */
1902 :
1903 : static tree
1904 29417 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1905 : enum cxa_builtin kind, tree fndecl,
1906 : bool *non_constant_p, bool *overflow_p,
1907 : tree *jump_target)
1908 : {
1909 29417 : int nargs = call_expr_nargs (call);
1910 29417 : location_t loc = cp_expr_loc_or_input_loc (call);
1911 29417 : tree args[4], arg;
1912 29417 : if (nargs > 4)
1913 : {
1914 0 : invalid_nargs:
1915 0 : if (!ctx->quiet)
1916 0 : error_at (loc, "call to %qD function with incorrect "
1917 : "number of arguments", fndecl);
1918 0 : *non_constant_p = true;
1919 0 : return call;
1920 : }
1921 29417 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1922 7360 : && nargs == 1
1923 7360 : && (arg = CALL_EXPR_ARG (call, 0))
1924 7360 : && TREE_CODE (arg) == CALL_EXPR
1925 7360 : && call_expr_nargs (arg) == 1
1926 36777 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1927 7360 : if (tree fun = get_function_named_in_call (arg))
1928 7360 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1929 : {
1930 7360 : if (ctx->global->caught_exceptions.length () < 2)
1931 : {
1932 3761 : no_caught_exceptions:
1933 3761 : if (!ctx->quiet)
1934 0 : error_at (loc, "%qD called with no caught exceptions pending",
1935 : fndecl);
1936 3761 : *non_constant_p = true;
1937 3761 : return call;
1938 : }
1939 : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1940 : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1941 : ctx->global->caught_exceptions vector to end with
1942 : __cxa_allocate_exception created artificial VAR_DECL (the
1943 : exception object) followed by handler type, pushed by TRY_BLOCK
1944 : evaluation. The only difference between the functions is that
1945 : __cxa_begin_catch pops the handler type from the vector and keeps
1946 : the VAR_DECL last and decreases uncaught_exceptions. The
1947 : VAR_DECL after __cxa_begin_catch serves as the current exception
1948 : and is then popped in __cxa_end_catch evaluation. */
1949 3599 : tree handler_type = ctx->global->caught_exceptions.last ();
1950 3599 : if (handler_type && (VAR_P (handler_type)
1951 3517 : || TREE_CODE (handler_type) == TREE_LIST))
1952 0 : goto no_caught_exceptions;
1953 3599 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1954 3599 : arg = ctx->global->caught_exceptions[idx];
1955 3599 : if (TREE_CODE (arg) == TREE_LIST)
1956 0 : arg = TREE_VALUE (arg);
1957 3599 : gcc_assert (VAR_P (arg));
1958 3599 : if (kind == CXA_BEGIN_CATCH)
1959 : {
1960 3571 : ctx->global->caught_exceptions.pop ();
1961 3571 : --ctx->global->uncaught_exceptions;
1962 : }
1963 3599 : if (handler_type == NULL_TREE)
1964 : /* Used for catch (...). Just return void. */
1965 82 : return void_node;
1966 3517 : else if (POINTER_TYPE_P (handler_type))
1967 : {
1968 : /* Used for catch of a pointer. */
1969 66 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1970 66 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1971 : size_zero_node, NULL_TREE, NULL_TREE);
1972 66 : arg = cp_convert (handler_type, arg,
1973 66 : ctx->quiet ? tf_none : tf_warning_or_error);
1974 66 : if (arg == error_mark_node)
1975 : {
1976 0 : *non_constant_p = true;
1977 0 : return call;
1978 : }
1979 : }
1980 : else
1981 : {
1982 : /* Used for catch of a non-pointer type. */
1983 3451 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1984 3431 : if (TYPE_PTRMEM_P (handler_type)
1985 3497 : && !same_type_ignoring_top_level_qualifiers_p
1986 46 : (handler_type, exc_type))
1987 : {
1988 46 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1989 46 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1990 : size_zero_node, NULL_TREE, NULL_TREE);
1991 46 : arg = cp_convert (handler_type, arg,
1992 46 : ctx->quiet ? tf_none
1993 : : tf_warning_or_error);
1994 46 : if (arg == error_mark_node)
1995 : {
1996 0 : *non_constant_p = true;
1997 0 : return call;
1998 : }
1999 46 : tree var = build_decl (loc, VAR_DECL, heap_identifier,
2000 46 : handler_type);
2001 46 : DECL_ARTIFICIAL (var) = 1;
2002 46 : ctx->global->heap_vars.safe_push (var);
2003 46 : ctx->global->put_value (var, NULL_TREE);
2004 46 : tree init = build2_loc (loc, INIT_EXPR, handler_type,
2005 : var, arg);
2006 46 : arg = ctx->global->caught_exceptions[idx];
2007 46 : if (TREE_CODE (arg) == TREE_LIST)
2008 : {
2009 0 : DECL_CHAIN (var) = TREE_PURPOSE (arg);
2010 0 : TREE_PURPOSE (arg) = var;
2011 : }
2012 : else
2013 46 : ctx->global->caught_exceptions[idx]
2014 92 : = build_tree_list (var, arg);
2015 46 : arg = cp_build_addr_expr (var, tf_none);
2016 46 : arg = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (arg),
2017 : init, arg);
2018 : }
2019 : else
2020 : {
2021 3405 : tree exc_ptr_type = build_pointer_type (exc_type);
2022 3405 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
2023 3405 : if (CLASS_TYPE_P (handler_type))
2024 : {
2025 3315 : tree ptr_type = build_pointer_type (handler_type);
2026 3315 : arg = cp_convert (ptr_type, arg,
2027 3315 : ctx->quiet ? tf_none
2028 : : tf_warning_or_error);
2029 3315 : if (arg == error_mark_node)
2030 : {
2031 0 : *non_constant_p = true;
2032 0 : return call;
2033 : }
2034 : }
2035 : }
2036 : }
2037 3517 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2038 : non_constant_p, overflow_p,
2039 3517 : jump_target);
2040 : }
2041 41110 : for (int i = 0; i < nargs; ++i)
2042 : {
2043 19053 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
2044 : vc_prvalue, non_constant_p,
2045 : overflow_p, jump_target);
2046 19053 : if (*non_constant_p)
2047 : return call;
2048 19053 : if (*jump_target)
2049 : return NULL_TREE;
2050 : }
2051 22057 : switch (kind)
2052 : {
2053 14295 : case CXA_ALLOCATE_EXCEPTION:
2054 14295 : if (nargs != 1)
2055 0 : goto invalid_nargs;
2056 14295 : if (!tree_fits_uhwi_p (args[0]))
2057 : {
2058 0 : if (!ctx->quiet)
2059 0 : error_at (loc, "cannot allocate exception: size not constant");
2060 0 : *non_constant_p = true;
2061 0 : return call;
2062 : }
2063 : else
2064 : {
2065 28590 : tree type = build_array_type_nelts (char_type_node,
2066 14295 : tree_to_uhwi (args[0]));
2067 14295 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2068 14295 : ctx->global->put_value (var, NULL_TREE);
2069 14295 : return fold_convert (ptr_type_node, build_address (var));
2070 : }
2071 2 : case CXA_FREE_EXCEPTION:
2072 2 : if (nargs != 1)
2073 0 : goto invalid_nargs;
2074 2 : arg = cxa_check_throw_arg (args[0], true);
2075 2 : if (arg == NULL_TREE)
2076 : {
2077 0 : invalid_ptr:
2078 0 : if (!ctx->quiet)
2079 0 : error_at (loc, "first argument to %qD function not result of "
2080 : "%<__cxa_allocate_exception%>", fndecl);
2081 0 : *non_constant_p = true;
2082 0 : return call;
2083 : }
2084 2 : DECL_NAME (arg) = heap_deleted_identifier;
2085 2 : ctx->global->destroy_value (arg);
2086 2 : ctx->global->heap_dealloc_count++;
2087 2 : return void_node;
2088 1488 : case CXA_THROW:
2089 1488 : if (nargs != 3)
2090 0 : goto invalid_nargs;
2091 1488 : arg = cxa_check_throw_arg (args[0], false);
2092 1488 : if (arg == NULL_TREE)
2093 0 : goto invalid_ptr;
2094 1488 : DECL_EXCEPTION_REFCOUNT (arg)
2095 1488 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2096 : size_one_node);
2097 1488 : ++ctx->global->uncaught_exceptions;
2098 1488 : *jump_target = arg;
2099 1488 : return void_node;
2100 0 : case CXA_BEGIN_CATCH:
2101 0 : case CXA_GET_EXCEPTION_PTR:
2102 0 : goto invalid_nargs;
2103 3567 : case CXA_END_CATCH:
2104 3567 : if (nargs != 0)
2105 0 : goto invalid_nargs;
2106 3567 : if (ctx->global->caught_exceptions.is_empty ())
2107 : {
2108 0 : no_active_exc:
2109 0 : if (!ctx->quiet)
2110 0 : error_at (loc, "%qD called with no caught exceptions active",
2111 : fndecl);
2112 0 : *non_constant_p = true;
2113 0 : return call;
2114 : }
2115 : else
2116 : {
2117 3567 : arg = ctx->global->caught_exceptions.pop ();
2118 3567 : if (arg == NULL_TREE
2119 3567 : || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2120 0 : goto no_active_exc;
2121 3567 : if (TREE_CODE (arg) == TREE_LIST)
2122 : {
2123 92 : for (tree aux = TREE_PURPOSE (arg); aux; aux = DECL_CHAIN (aux))
2124 : {
2125 46 : DECL_NAME (aux) = heap_deleted_identifier;
2126 46 : ctx->global->destroy_value (aux);
2127 46 : ctx->global->heap_dealloc_count++;
2128 : }
2129 46 : arg = TREE_VALUE (arg);
2130 : }
2131 3567 : DECL_CHAIN (arg) = NULL_TREE;
2132 3645 : free_except:
2133 3645 : DECL_EXCEPTION_REFCOUNT (arg)
2134 3645 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2135 : size_one_node);
2136 3645 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2137 : {
2138 3435 : if (type_build_dtor_call (TREE_TYPE (arg)))
2139 : {
2140 : /* So that we don't complain about out-of-consteval use. */
2141 3217 : temp_override<tree> ovr (current_function_decl);
2142 3217 : if (ctx->call && ctx->call->fundef)
2143 3217 : current_function_decl = ctx->call->fundef->decl;
2144 3217 : tree cleanup
2145 3239 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2146 : : tf_warning_or_error));
2147 3217 : if (cleanup == error_mark_node)
2148 0 : *non_constant_p = true;
2149 3217 : tree jmp_target = NULL_TREE;
2150 3217 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2151 : non_constant_p, overflow_p,
2152 : &jmp_target);
2153 3217 : if (throws (&jmp_target))
2154 0 : *jump_target = jmp_target;
2155 3217 : }
2156 3435 : DECL_NAME (arg) = heap_deleted_identifier;
2157 3435 : ctx->global->destroy_value (arg);
2158 3435 : ctx->global->heap_dealloc_count++;
2159 : }
2160 : }
2161 3645 : return void_node;
2162 98 : case CXA_RETHROW:
2163 98 : if (nargs != 0)
2164 0 : goto invalid_nargs;
2165 98 : unsigned idx;
2166 196 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2167 82 : if (arg == NULL_TREE || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2168 0 : --idx;
2169 : else
2170 : break;
2171 98 : if (arg == NULL_TREE)
2172 : {
2173 16 : if (!ctx->quiet)
2174 4 : error_at (loc, "%qD called with no caught exceptions active",
2175 : fndecl);
2176 16 : *non_constant_p = true;
2177 16 : return call;
2178 : }
2179 82 : if (TREE_CODE (arg) == TREE_LIST)
2180 0 : arg = TREE_VALUE (arg);
2181 82 : DECL_EXCEPTION_REFCOUNT (arg)
2182 82 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2183 82 : ++ctx->global->uncaught_exceptions;
2184 : /* Don't cache calls which rethrow, they depend on the current
2185 : exception which might be caught in the caller. */
2186 82 : ctx->global->state_dependent = true;
2187 82 : *jump_target = arg;
2188 82 : return void_node;
2189 163 : case CXA_BAD_CAST:
2190 163 : case CXA_BAD_TYPEID:
2191 163 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2192 163 : if (nargs != 0)
2193 0 : goto invalid_nargs;
2194 : else
2195 : {
2196 163 : tree name;
2197 163 : switch (kind)
2198 : {
2199 123 : case CXA_BAD_CAST:
2200 123 : name = get_identifier ("bad_cast");
2201 123 : break;
2202 7 : case CXA_BAD_TYPEID:
2203 7 : name = get_identifier ("bad_typeid");
2204 7 : break;
2205 33 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2206 33 : name = get_identifier ("bad_array_new_length");
2207 33 : break;
2208 : default:
2209 : gcc_unreachable ();
2210 : }
2211 163 : tree decl = lookup_qualified_name (std_node, name);
2212 163 : if (TREE_CODE (decl) != TYPE_DECL
2213 135 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2214 298 : || !type_build_ctor_call (TREE_TYPE (decl)))
2215 : {
2216 28 : if (!ctx->quiet)
2217 8 : error_at (loc, "%qD called without %<std::%D%> being defined",
2218 : fndecl, name);
2219 28 : *non_constant_p = true;
2220 28 : return call;
2221 : }
2222 135 : tree type = TREE_TYPE (decl);
2223 135 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2224 135 : tree ctor
2225 135 : = build_special_member_call (var, complete_ctor_identifier,
2226 : NULL, type, LOOKUP_NORMAL,
2227 135 : ctx->quiet ? tf_none
2228 : : tf_warning_or_error);
2229 135 : if (ctor == error_mark_node)
2230 : {
2231 0 : *non_constant_p = true;
2232 0 : return call;
2233 : }
2234 135 : if (TREE_CONSTANT (ctor))
2235 0 : ctx->global->put_value (var, ctor);
2236 : else
2237 : {
2238 135 : ctx->global->put_value (var, NULL_TREE);
2239 135 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2240 : non_constant_p, overflow_p,
2241 : jump_target);
2242 135 : if (*non_constant_p)
2243 : return call;
2244 135 : if (throws (jump_target))
2245 : return NULL_TREE;
2246 : }
2247 135 : ++ctx->global->uncaught_exceptions;
2248 135 : *jump_target = var;
2249 : }
2250 135 : return void_node;
2251 82 : case BUILTIN_UNCAUGHT_EXCEPTIONS:
2252 82 : if (nargs != 0)
2253 0 : goto invalid_nargs;
2254 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2255 : want to give a definite answer during mce_unknown evaluation,
2256 : because that might prevent evaluation later on when some
2257 : exceptions might be uncaught. But unlike that, we don't
2258 : want to constant fold it even during cp_fold, because at runtime
2259 : std::uncaught_exceptions () might still be non-zero. */
2260 82 : if (ctx->manifestly_const_eval != mce_true)
2261 : {
2262 46 : *non_constant_p = true;
2263 46 : return call;
2264 : }
2265 : /* Don't cache calls which call __builtin_uncaught_exceptions (),
2266 : they depend on the current uncaught exceptions which might
2267 : be the state from their caller. */
2268 36 : ctx->global->state_dependent = true;
2269 36 : return build_int_cst (integer_type_node,
2270 36 : ctx->global->uncaught_exceptions);
2271 2196 : case BUILTIN_CURRENT_EXCEPTION:
2272 2196 : if (nargs != 0)
2273 0 : goto invalid_nargs;
2274 : else
2275 : {
2276 2196 : tree name = get_identifier ("exception_ptr");
2277 2196 : tree decl = lookup_qualified_name (std_node, name);
2278 2196 : tree fld;
2279 2196 : if (TREE_CODE (decl) != TYPE_DECL
2280 2196 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2281 2196 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2282 2196 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2283 2196 : || DECL_ARTIFICIAL (fld)
2284 2196 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2285 2196 : || next_aggregate_field (DECL_CHAIN (fld))
2286 4392 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2287 2196 : TYPE_SIZE (TREE_TYPE (fld))))
2288 : {
2289 0 : if (!ctx->quiet)
2290 0 : error_at (loc, "%qD called without supportable %qs",
2291 : fndecl, "std::exception_ptr");
2292 0 : *non_constant_p = true;
2293 0 : return call;
2294 : }
2295 4392 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2296 42 : if (arg == NULL_TREE
2297 42 : || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2298 0 : --idx;
2299 : else
2300 : break;
2301 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2302 : want to give a definite answer during mce_unknown evaluation,
2303 : because that might prevent evaluation later on when some
2304 : exceptions might be current. But unlike that, we don't
2305 : want to constant fold it to null even during cp_fold, because
2306 : at runtime std::current_exception () might still be non-null. */
2307 2196 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2308 : {
2309 2142 : *non_constant_p = true;
2310 2142 : return call;
2311 : }
2312 54 : if (arg == NULL_TREE)
2313 12 : arg = build_zero_cst (TREE_TYPE (fld));
2314 : else
2315 : {
2316 42 : if (TREE_CODE (arg) == TREE_LIST)
2317 0 : arg = TREE_VALUE (arg);
2318 42 : DECL_EXCEPTION_REFCOUNT (arg)
2319 42 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2320 : size_one_node);
2321 42 : arg = fold_convert (ptr_type_node, build_address (arg));
2322 : }
2323 : /* Don't cache calls which call __builtin_current_exception (),
2324 : they depend on the current exception which might be caught
2325 : in the caller. */
2326 54 : ctx->global->state_dependent = true;
2327 54 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2328 : }
2329 40 : case STD_RETHROW_EXCEPTION:
2330 40 : if (nargs != 1)
2331 0 : goto invalid_nargs;
2332 40 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2333 : {
2334 40 : arg = args[0];
2335 40 : STRIP_NOPS (arg);
2336 40 : if (TREE_CODE (arg) == ADDR_EXPR)
2337 : {
2338 40 : args[0]
2339 40 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2340 : vc_prvalue, non_constant_p,
2341 : overflow_p, jump_target);
2342 40 : if (*non_constant_p)
2343 : return call;
2344 40 : if (*jump_target)
2345 : return NULL_TREE;
2346 : }
2347 : }
2348 40 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2349 40 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2350 : {
2351 0 : invalid_std_rethrow:
2352 0 : if (!ctx->quiet)
2353 0 : error_at (loc, "%qD called with unexpected %qs argument",
2354 : fndecl, "std::exception_ptr");
2355 0 : *non_constant_p = true;
2356 0 : return void_node;
2357 : }
2358 40 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2359 40 : if (arg == NULL_TREE)
2360 0 : goto invalid_std_rethrow;
2361 40 : DECL_EXCEPTION_REFCOUNT (arg)
2362 40 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2363 40 : ++ctx->global->uncaught_exceptions;
2364 40 : *jump_target = arg;
2365 40 : return void_node;
2366 126 : case BUILTIN_EH_PTR_ADJUST_REF:
2367 126 : if (nargs != 2)
2368 0 : goto invalid_nargs;
2369 126 : arg = cxa_check_throw_arg (args[0], false);
2370 126 : if (arg == NULL_TREE)
2371 0 : goto invalid_ptr;
2372 126 : if (integer_onep (args[1]))
2373 48 : DECL_EXCEPTION_REFCOUNT (arg)
2374 96 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2375 : size_one_node);
2376 78 : else if (integer_minus_onep (args[1]))
2377 78 : goto free_except;
2378 : else
2379 : {
2380 0 : if (!ctx->quiet)
2381 0 : error_at (loc, "%qD called with second argument "
2382 : "other than 1 or -1", fndecl);
2383 0 : *non_constant_p = true;
2384 : }
2385 48 : return void_node;
2386 0 : default:
2387 0 : gcc_unreachable ();
2388 : }
2389 : }
2390 :
2391 : /* Variables and functions to manage constexpr call expansion context.
2392 : These do not need to be marked for PCH or GC. */
2393 :
2394 : /* FIXME remember and print actual constant arguments. */
2395 : static vec<tree> call_stack;
2396 : static int call_stack_tick;
2397 : static int last_cx_error_tick;
2398 :
2399 : /* Attempt to evaluate T which represents a call to __builtin_constexpr_diag.
2400 : The arguments should be an integer (0 for inform, 1 for warning, 2 for
2401 : error) optionally with 16 ored in if it should use caller's caller location
2402 : instead of caller's location and 2 messages which are either a pointer to
2403 : a STRING_CST or class with data () and size () member functions like
2404 : string_view or u8string_view. The first message is a tag, with "" passed
2405 : for no tag, data () should return const char *, the tag should only contain
2406 : alphanumeric letters or underscores. The second message is the diagnostic
2407 : message, data () can be either const char * or const char8_t *. size ()
2408 : should return the corresponding length of the strings in bytes as an
2409 : integer. */
2410 :
2411 : static tree
2412 210 : cxx_eval_constexpr_diag (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
2413 : bool *overflow_p, tree *jump_target)
2414 : {
2415 210 : location_t loc = EXPR_LOCATION (t);
2416 210 : if (call_expr_nargs (t) != 3)
2417 : {
2418 12 : if (!ctx->quiet)
2419 12 : error_at (loc, "wrong number of arguments to %qs call",
2420 : "__builtin_constexpr_diag");
2421 12 : *non_constant_p = true;
2422 12 : return t;
2423 : }
2424 : tree args[3];
2425 792 : for (int i = 0; i < 3; ++i)
2426 : {
2427 594 : tree arg = convert_from_reference (CALL_EXPR_ARG (t, i));
2428 594 : arg = cxx_eval_constant_expression (ctx, arg,
2429 : (i == 0
2430 534 : || POINTER_TYPE_P (TREE_TYPE (arg)))
2431 990 : ? vc_prvalue : vc_glvalue,
2432 : non_constant_p, overflow_p,
2433 : jump_target);
2434 594 : if (*jump_target)
2435 : return NULL_TREE;
2436 594 : if (*non_constant_p)
2437 : return t;
2438 594 : args[i] = arg;
2439 : }
2440 396 : if (TREE_CODE (args[0]) != INTEGER_CST
2441 198 : || wi::to_widest (args[0]) < 0
2442 194 : || wi::to_widest (args[0]) > 18
2443 396 : || (wi::to_widest (args[0]) & 15) > 2)
2444 : {
2445 8 : if (!ctx->quiet)
2446 8 : error_at (loc, "first %qs call argument should be 0, 1, 2, 16, 17 or "
2447 : "18", "__builtin_constexpr_diag");
2448 8 : *non_constant_p = true;
2449 8 : return t;
2450 : }
2451 190 : const char *msgs[2] = {};
2452 190 : int lens[3] = {};
2453 950 : cexpr_str cstrs[2];
2454 478 : diagnostics::kind kind = diagnostics::kind::error;
2455 478 : for (int i = 1; i < 3; ++i)
2456 : {
2457 344 : tree arg = args[i];
2458 344 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2459 : {
2460 206 : tree str = arg;
2461 206 : STRIP_NOPS (str);
2462 206 : if (TREE_CODE (str) == ADDR_EXPR
2463 206 : && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST)
2464 : {
2465 206 : str = TREE_OPERAND (str, 0);
2466 206 : tree eltype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)));
2467 206 : if (eltype == char_type_node
2468 24 : || (i == 2 && eltype == char8_type_node))
2469 344 : arg = str;
2470 : }
2471 : }
2472 344 : cstrs[i - 1].message = arg;
2473 344 : if (!cstrs[i - 1].type_check (loc, i == 2))
2474 : {
2475 40 : *non_constant_p = true;
2476 40 : return t;
2477 : }
2478 304 : if (!cstrs[i - 1].extract (loc, msgs[i - 1], lens[i - 1], ctx,
2479 : non_constant_p, overflow_p, jump_target))
2480 : {
2481 16 : if (*jump_target)
2482 : return NULL_TREE;
2483 16 : *non_constant_p = true;
2484 16 : return t;
2485 : }
2486 : }
2487 134 : if (msgs[0])
2488 : {
2489 726 : for (int i = 0; i < lens[0]; ++i)
2490 596 : if (!ISALNUM (msgs[0][i]) && msgs[0][i] != '_')
2491 : {
2492 4 : if (!ctx->quiet)
2493 4 : error_at (loc, "%qs tag string contains %qc character other than"
2494 : " letters, digits or %<_%>",
2495 : "__builtin_constexpr_diag", msgs[0][i]);
2496 4 : *non_constant_p = true;
2497 4 : return t;
2498 : }
2499 : }
2500 130 : if (ctx->manifestly_const_eval == mce_unknown)
2501 : {
2502 0 : *non_constant_p = true;
2503 0 : return t;
2504 : }
2505 130 : int arg0 = tree_to_uhwi (args[0]);
2506 130 : if (arg0 & 16)
2507 : {
2508 32 : arg0 &= 15;
2509 32 : if (!call_stack.is_empty ())
2510 : {
2511 32 : tree call = call_stack.last ();
2512 32 : if (EXPR_HAS_LOCATION (call))
2513 32 : loc = EXPR_LOCATION (call);
2514 : }
2515 : }
2516 130 : if (arg0 == 0)
2517 : kind = diagnostics::kind::note;
2518 82 : else if (arg0 == 1)
2519 36 : kind = diagnostics::kind::warning;
2520 130 : if (lens[0])
2521 : {
2522 88 : const char *color = "error";
2523 88 : if (kind == diagnostics::kind::note)
2524 : color = "note";
2525 60 : else if (kind == diagnostics::kind::warning)
2526 32 : color = "warning";
2527 88 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s [%r%.*s%R]",
2528 : lens[1], msgs[1], color, lens[0], msgs[0]);
2529 : }
2530 : else
2531 42 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s",
2532 : lens[1], msgs[1]);
2533 130 : return void_node;
2534 570 : }
2535 :
2536 : /* Attempt to evaluate T which represents a call to a builtin function.
2537 : We assume here that all builtin functions evaluate to scalar types
2538 : represented by _CST nodes. */
2539 :
2540 : static tree
2541 20720020 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2542 : value_cat lval,
2543 : bool *non_constant_p, bool *overflow_p,
2544 : tree *jump_target)
2545 : {
2546 20720020 : const int nargs = call_expr_nargs (t);
2547 20720020 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2548 20720020 : tree new_call;
2549 20720020 : int i;
2550 :
2551 : /* Don't fold __builtin_constant_p within a constexpr function. */
2552 20720020 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2553 :
2554 : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2555 : in a constexpr function until we have values for the parameters. */
2556 3567381 : if (bi_const_p
2557 3567381 : && ctx->manifestly_const_eval != mce_true
2558 1749539 : && current_function_decl
2559 1748410 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2560 : {
2561 1396373 : *non_constant_p = true;
2562 1396373 : return t;
2563 : }
2564 :
2565 19323647 : if (fndecl_built_in_p (fun, BUILT_IN_FRONTEND))
2566 3995147 : switch (DECL_FE_FUNCTION_CODE (fun))
2567 : {
2568 3976563 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
2569 : /* For __builtin_is_constant_evaluated, defer it if not
2570 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2571 : without manifestly_const_eval even expressions or parts thereof
2572 : which will later be manifestly const_eval evaluated), otherwise fold
2573 : it to true. */
2574 3976563 : if (ctx->manifestly_const_eval == mce_unknown)
2575 : {
2576 3928364 : *non_constant_p = true;
2577 3928364 : return t;
2578 : }
2579 48199 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2580 48199 : boolean_type_node);
2581 :
2582 10917 : case CP_BUILT_IN_SOURCE_LOCATION:
2583 10917 : {
2584 10917 : temp_override<tree> ovr (current_function_decl);
2585 10917 : if (ctx->call && ctx->call->fundef)
2586 2753 : current_function_decl = ctx->call->fundef->decl;
2587 10917 : return fold_builtin_source_location (t);
2588 10917 : }
2589 :
2590 126 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
2591 126 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2592 : fun, non_constant_p, overflow_p,
2593 126 : jump_target);
2594 :
2595 2196 : case CP_BUILT_IN_CURRENT_EXCEPTION:
2596 2196 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_CURRENT_EXCEPTION,
2597 : fun, non_constant_p, overflow_p,
2598 2196 : jump_target);
2599 :
2600 82 : case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
2601 82 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_UNCAUGHT_EXCEPTIONS,
2602 : fun, non_constant_p, overflow_p,
2603 82 : jump_target);
2604 :
2605 210 : case CP_BUILT_IN_CONSTEXPR_DIAG:
2606 210 : return cxx_eval_constexpr_diag (ctx, t, non_constant_p, overflow_p,
2607 210 : jump_target);
2608 :
2609 : default:
2610 : break;
2611 : }
2612 :
2613 15333553 : int strops = 0;
2614 15333553 : int strret = 0;
2615 15333553 : bool bos = false;
2616 15333553 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2617 13987942 : switch (DECL_FUNCTION_CODE (fun))
2618 : {
2619 : case BUILT_IN_STRLEN:
2620 : case BUILT_IN_STRNLEN:
2621 15333402 : strops = 1;
2622 : break;
2623 201134 : case BUILT_IN_MEMCHR:
2624 201134 : case BUILT_IN_STRCHR:
2625 201134 : case BUILT_IN_STRRCHR:
2626 201134 : strops = 1;
2627 201134 : strret = 1;
2628 201134 : break;
2629 112482 : case BUILT_IN_MEMCMP:
2630 112482 : case BUILT_IN_STRCMP:
2631 112482 : strops = 2;
2632 112482 : break;
2633 49888 : case BUILT_IN_STRSTR:
2634 49888 : strops = 2;
2635 49888 : strret = 1;
2636 49888 : break;
2637 66 : case BUILT_IN_ASAN_POINTER_COMPARE:
2638 66 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2639 66 : case BUILT_IN_OBSERVABLE_CHKPT:
2640 : /* These builtins shall be ignored during constant expression
2641 : evaluation. */
2642 66 : return void_node;
2643 85 : case BUILT_IN_UNREACHABLE:
2644 85 : case BUILT_IN_TRAP:
2645 85 : if (!*non_constant_p && !ctx->quiet)
2646 : {
2647 : /* Do not allow__builtin_unreachable in constexpr function.
2648 : The __builtin_unreachable call with BUILTINS_LOCATION
2649 : comes from cp_maybe_instrument_return. */
2650 25 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2651 0 : error ("%<constexpr%> call flows off the end of the function");
2652 : else
2653 25 : error ("%q+E is not a constant expression", t);
2654 : }
2655 85 : *non_constant_p = true;
2656 85 : return t;
2657 2881 : case BUILT_IN_OBJECT_SIZE:
2658 2881 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
2659 2881 : bos = ctx->manifestly_const_eval == mce_true;
2660 2881 : break;
2661 : default:
2662 : break;
2663 : }
2664 :
2665 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2666 : return constant false for a non-constant argument. */
2667 15333402 : constexpr_ctx new_ctx = *ctx;
2668 15333402 : new_ctx.quiet = true;
2669 41670447 : for (i = 0; i < nargs; ++i)
2670 : {
2671 26337047 : tree arg = CALL_EXPR_ARG (t, i);
2672 26337047 : tree oarg = arg;
2673 :
2674 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2675 : expand_builtin doesn't know how to look in the values table. */
2676 26337047 : bool strop = i < strops;
2677 26337047 : if (strop)
2678 : {
2679 687914 : STRIP_NOPS (arg);
2680 687914 : if (TREE_CODE (arg) == ADDR_EXPR)
2681 8134 : arg = TREE_OPERAND (arg, 0);
2682 : else
2683 : strop = false;
2684 : }
2685 :
2686 : /* If builtin_valid_in_constant_expr_p is true,
2687 : potential_constant_expression_1 has not recursed into the arguments
2688 : of the builtin, verify it here. */
2689 26337047 : if (!builtin_valid_in_constant_expr_p (fun)
2690 26337047 : || potential_constant_expression (arg))
2691 : {
2692 26336910 : bool dummy1 = false, dummy2 = false;
2693 26336910 : tree jmp_target = NULL_TREE;
2694 26336910 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2695 : &dummy1, &dummy2, &jmp_target);
2696 26336908 : if (jmp_target)
2697 : {
2698 0 : *jump_target = jmp_target;
2699 0 : return NULL_TREE;
2700 : }
2701 : }
2702 :
2703 26337045 : if (bi_const_p)
2704 : /* For __builtin_constant_p, fold all expressions with constant values
2705 : even if they aren't C++ constant-expressions. */
2706 2171006 : arg = cp_fold_rvalue (arg);
2707 24166039 : else if (strop)
2708 : {
2709 8134 : if (TREE_CODE (arg) == CONSTRUCTOR)
2710 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2711 8134 : if (TREE_CODE (arg) == STRING_CST)
2712 3042 : arg = build_address (arg);
2713 : else
2714 : arg = oarg;
2715 : }
2716 :
2717 26337045 : args[i] = arg;
2718 : }
2719 15333400 : if (bos)
2720 : {
2721 102 : tree arg = args[0];
2722 102 : STRIP_NOPS (arg);
2723 102 : if (TREE_CODE (arg) == ADDR_EXPR)
2724 102 : args[0] = arg;
2725 : }
2726 :
2727 15333400 : bool save_ffbcp = force_folding_builtin_constant_p;
2728 15333400 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2729 15333400 : tree save_cur_fn = current_function_decl;
2730 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2731 15333400 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2732 37 : && ctx->call
2733 15333404 : && ctx->call->fundef)
2734 4 : current_function_decl = ctx->call->fundef->decl;
2735 15333400 : if (fndecl_built_in_p (fun,
2736 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2737 : BUILT_IN_FRONTEND))
2738 : {
2739 336 : location_t loc = EXPR_LOCATION (t);
2740 336 : if (nargs >= 1)
2741 333 : VERIFY_CONSTANT (args[0]);
2742 136 : new_call
2743 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2744 : args);
2745 : }
2746 15333064 : else if (fndecl_built_in_p (fun,
2747 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2748 : BUILT_IN_FRONTEND))
2749 : {
2750 471 : location_t loc = EXPR_LOCATION (t);
2751 471 : if (nargs >= 2)
2752 : {
2753 465 : VERIFY_CONSTANT (args[0]);
2754 237 : VERIFY_CONSTANT (args[1]);
2755 : }
2756 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2757 : }
2758 15332593 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
2759 : BUILT_IN_FRONTEND))
2760 : {
2761 4246 : location_t loc = EXPR_LOCATION (t);
2762 4246 : if (nargs >= 1)
2763 : {
2764 4246 : tree arg = CALL_EXPR_ARG (t, 0);
2765 4246 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2766 : non_constant_p, overflow_p,
2767 : jump_target);
2768 4246 : if (*jump_target)
2769 : return NULL_TREE;
2770 4246 : args[0] = arg;
2771 : }
2772 4246 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
2773 : }
2774 : else
2775 30656694 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2776 15328347 : CALL_EXPR_FN (t), nargs, args);
2777 15332972 : current_function_decl = save_cur_fn;
2778 15332972 : force_folding_builtin_constant_p = save_ffbcp;
2779 15332972 : if (new_call == NULL)
2780 : {
2781 10427559 : if (!*non_constant_p && !ctx->quiet)
2782 : {
2783 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2784 0 : CALL_EXPR_FN (t), nargs, args);
2785 0 : error ("%q+E is not a constant expression", new_call);
2786 : }
2787 10427559 : *non_constant_p = true;
2788 10427559 : return t;
2789 : }
2790 :
2791 4905413 : if (!potential_constant_expression (new_call))
2792 : {
2793 861 : if (!*non_constant_p && !ctx->quiet)
2794 4 : error ("%q+E is not a constant expression", new_call);
2795 861 : *non_constant_p = true;
2796 861 : return t;
2797 : }
2798 :
2799 4904552 : if (strret)
2800 : {
2801 : /* memchr returns a pointer into the first argument, but we replaced the
2802 : argument above with a STRING_CST; put it back it now. */
2803 283 : tree op = CALL_EXPR_ARG (t, strret-1);
2804 283 : STRIP_NOPS (new_call);
2805 283 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2806 142 : TREE_OPERAND (new_call, 0) = op;
2807 141 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2808 4904552 : new_call = op;
2809 : }
2810 :
2811 4904552 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2812 : non_constant_p, overflow_p,
2813 4904552 : jump_target);
2814 : }
2815 :
2816 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2817 : the type of the value to match. */
2818 :
2819 : static tree
2820 136547193 : adjust_temp_type (tree type, tree temp)
2821 : {
2822 136547193 : if (same_type_p (TREE_TYPE (temp), type))
2823 : return temp;
2824 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2825 62815481 : if (TREE_CODE (temp) == CONSTRUCTOR)
2826 : {
2827 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2828 3803372 : tree t = copy_node (temp);
2829 3803372 : TREE_TYPE (t) = type;
2830 3803372 : return t;
2831 : }
2832 59012109 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2833 0 : return build0 (EMPTY_CLASS_EXPR, type);
2834 59012109 : gcc_assert (scalarish_type_p (type));
2835 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2836 : type is cv-unqualified. */
2837 59012109 : return cp_fold_convert (cv_unqualified (type), temp);
2838 : }
2839 :
2840 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2841 : sub-CONSTRUCTORs. Otherwise return T.
2842 :
2843 : We use this whenever we initialize an object as a whole, whether it's a
2844 : parameter, a local variable, or a subobject, so that subsequent
2845 : modifications don't affect other places where it was used. */
2846 :
2847 : tree
2848 123309873 : unshare_constructor (tree t MEM_STAT_DECL)
2849 : {
2850 123309873 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2851 : return t;
2852 18622835 : auto_vec <tree*, 4> ptrs;
2853 18622835 : ptrs.safe_push (&t);
2854 18622835 : while (!ptrs.is_empty ())
2855 : {
2856 24506721 : tree *p = ptrs.pop ();
2857 24506721 : tree n = copy_node (*p PASS_MEM_STAT);
2858 40661718 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2859 24506721 : *p = n;
2860 24506721 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2861 24506721 : constructor_elt *ce;
2862 94160169 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2863 26523892 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2864 5883886 : ptrs.safe_push (&ce->value);
2865 : }
2866 18622835 : return t;
2867 18622835 : }
2868 :
2869 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2870 :
2871 : static void
2872 879052 : free_constructor (tree t)
2873 : {
2874 879052 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2875 0 : return;
2876 879052 : releasing_vec ctors;
2877 879052 : vec_safe_push (ctors, t);
2878 2676561 : while (!ctors->is_empty ())
2879 : {
2880 918457 : tree c = ctors->pop ();
2881 918457 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2882 : {
2883 : constructor_elt *ce;
2884 394274 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2885 255243 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2886 39405 : vec_safe_push (ctors, ce->value);
2887 139031 : ggc_free (elts);
2888 : }
2889 918457 : ggc_free (c);
2890 : }
2891 879052 : }
2892 :
2893 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2894 : if *TP is address of a static variable (or part of it) currently being
2895 : constructed or of a heap artificial variable. */
2896 :
2897 : static tree
2898 48080368 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2899 : {
2900 48080368 : if (TREE_CODE (*tp) == ADDR_EXPR)
2901 8907384 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2902 8907384 : if (VAR_P (var) && TREE_STATIC (var))
2903 : {
2904 5501099 : if (DECL_NAME (var) == heap_uninit_identifier
2905 5501099 : || DECL_NAME (var) == heap_identifier
2906 5501099 : || DECL_NAME (var) == heap_vec_uninit_identifier
2907 11002198 : || DECL_NAME (var) == heap_vec_identifier)
2908 : return var;
2909 :
2910 5501099 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2911 5501099 : if (global->get_value (var))
2912 : return var;
2913 : }
2914 47633682 : if (TYPE_P (*tp))
2915 2493 : *walk_subtrees = false;
2916 : return NULL_TREE;
2917 : }
2918 :
2919 : /* Subroutine of cxx_eval_call_expression.
2920 : We are processing a call expression (either CALL_EXPR or
2921 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2922 : all arguments and bind their values to correspondings
2923 : parameters, making up the NEW_CALL context. */
2924 :
2925 : static tree
2926 184262713 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2927 : tree orig_fun, bool *non_constant_p,
2928 : bool *overflow_p, bool *non_constant_args,
2929 : tree *jump_target)
2930 : {
2931 184262713 : int nargs = call_expr_nargs (t);
2932 184262713 : tree parms = DECL_ARGUMENTS (fun);
2933 184262713 : int i, j = 0;
2934 184262713 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2935 1523 : ++nargs;
2936 184262713 : if (DECL_HAS_VTT_PARM_P (fun)
2937 1525 : && fun != orig_fun
2938 184264236 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2939 1082 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2940 463 : ++nargs;
2941 : /* We don't record ellipsis args below. */
2942 184262713 : int nparms = list_length (parms);
2943 184262713 : int nbinds = nargs < nparms ? nargs : nparms;
2944 184262713 : tree binds = make_tree_vec (nbinds);
2945 :
2946 : /* The call is not a constant expression if it involves the cdtor for a type
2947 : with virtual bases before C++26. */
2948 184262713 : if (cxx_dialect < cxx26
2949 184262713 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2950 : {
2951 17 : if (!ctx->quiet)
2952 : {
2953 3 : error_at (cp_expr_loc_or_input_loc (t),
2954 : "call to non-%<constexpr%> function %qD", fun);
2955 3 : explain_invalid_constexpr_fn (fun);
2956 : }
2957 17 : *non_constant_p = true;
2958 17 : return binds;
2959 : }
2960 :
2961 331108566 : for (i = 0; i < nargs; ++i)
2962 : {
2963 221135009 : tree x, arg;
2964 221135009 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2965 221135009 : if (parms && DECL_BY_REFERENCE (parms))
2966 26929 : type = TREE_TYPE (type);
2967 221135009 : if (i == 1
2968 221135009 : && j == 0
2969 47330631 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2970 221136400 : && orig_fun != fun)
2971 : {
2972 1391 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2973 1391 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2974 428 : x = boolean_true_node;
2975 : else
2976 963 : x = boolean_false_node;
2977 : j = -1;
2978 : }
2979 221133618 : else if (i == 2
2980 221133618 : && j == -1
2981 1391 : && DECL_HAS_VTT_PARM_P (fun)
2982 1391 : && orig_fun != fun
2983 221135009 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2984 985 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2985 : {
2986 428 : x = build_zero_cst (type);
2987 428 : j = -2;
2988 : }
2989 : else
2990 221133190 : x = get_nth_callarg (t, i + j);
2991 : /* For member function, the first argument is a pointer to the implied
2992 : object. For a constructor, it might still be a dummy object, in
2993 : which case we get the real argument from ctx. */
2994 324538682 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2995 244186780 : && is_dummy_object (x))
2996 : {
2997 11809576 : x = ctx->object;
2998 11809576 : x = build_address (x);
2999 : }
3000 221135009 : if (TREE_ADDRESSABLE (type))
3001 : {
3002 : /* Undo convert_for_arg_passing work here. */
3003 39401 : x = convert_from_reference (x);
3004 39401 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
3005 : non_constant_p, overflow_p,
3006 : jump_target);
3007 : }
3008 : else
3009 : /* Normally we would strip a TARGET_EXPR in an initialization context
3010 : such as this, but here we do the elision differently: we keep the
3011 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
3012 221095608 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
3013 : non_constant_p, overflow_p,
3014 : jump_target);
3015 221135009 : if (*jump_target)
3016 : break;
3017 : /* Check we aren't dereferencing a null pointer when calling a non-static
3018 : member function, which is undefined behaviour. */
3019 162269286 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
3020 92278100 : && integer_zerop (arg)
3021 : /* But ignore calls from within compiler-generated code, to handle
3022 : cases like lambda function pointer conversion operator thunks
3023 : which pass NULL as the 'this' pointer. */
3024 221309412 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
3025 : {
3026 527 : if (!ctx->quiet)
3027 6 : error_at (cp_expr_loc_or_input_loc (x),
3028 : "dereferencing a null pointer");
3029 527 : *non_constant_p = true;
3030 : }
3031 : /* Don't VERIFY_CONSTANT here. */
3032 221134925 : if (*non_constant_p && ctx->quiet)
3033 : break;
3034 : /* Just discard ellipsis args after checking their constantitude. */
3035 146845870 : if (!parms)
3036 296 : continue;
3037 :
3038 146845574 : if (!*non_constant_p)
3039 : {
3040 : /* Make sure the binding has the same type as the parm. But
3041 : only for constant args. */
3042 146844800 : if (TREE_ADDRESSABLE (type))
3043 : {
3044 26176 : if (!same_type_p (type, TREE_TYPE (arg)))
3045 : {
3046 9 : arg = build_fold_addr_expr (arg);
3047 9 : arg = cp_fold_convert (build_reference_type (type), arg);
3048 9 : arg = convert_from_reference (arg);
3049 : }
3050 : }
3051 146818624 : else if (!TYPE_REF_P (type))
3052 105345556 : arg = adjust_temp_type (type, arg);
3053 146844800 : if (!TREE_CONSTANT (arg))
3054 96924164 : *non_constant_args = true;
3055 49920636 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3056 : /* The destructor needs to see any modifications the callee makes
3057 : to the argument. */
3058 0 : *non_constant_args = true;
3059 : /* If arg is or contains address of a heap artificial variable or
3060 : of a static variable being constructed, avoid caching the
3061 : function call, as those variables might be modified by the
3062 : function, or might be modified by the callers in between
3063 : the cached function and just read by the function. */
3064 49920636 : else if (!*non_constant_args
3065 49920636 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
3066 : NULL))
3067 446686 : *non_constant_args = true;
3068 :
3069 : /* For virtual calls, adjust the this argument, so that it is
3070 : the object on which the method is called, rather than
3071 : one of its bases. */
3072 146844800 : if (i == 0 && DECL_VIRTUAL_P (fun))
3073 : {
3074 27010 : tree addr = arg;
3075 27010 : STRIP_NOPS (addr);
3076 27010 : if (TREE_CODE (addr) == ADDR_EXPR)
3077 : {
3078 26973 : tree obj = TREE_OPERAND (addr, 0);
3079 26973 : while (TREE_CODE (obj) == COMPONENT_REF
3080 11620 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
3081 38671 : && !same_type_ignoring_top_level_qualifiers_p
3082 11527 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
3083 171 : obj = TREE_OPERAND (obj, 0);
3084 26973 : if (obj != TREE_OPERAND (addr, 0))
3085 168 : arg = build_fold_addr_expr_with_type (obj,
3086 : TREE_TYPE (arg));
3087 : }
3088 : }
3089 146844800 : TREE_VEC_ELT (binds, i) = arg;
3090 : }
3091 146845574 : parms = TREE_CHAIN (parms);
3092 : }
3093 :
3094 : return binds;
3095 : }
3096 :
3097 : static int
3098 109366466 : push_cx_call_context (tree call)
3099 : {
3100 109366466 : ++call_stack_tick;
3101 109366466 : if (!EXPR_HAS_LOCATION (call))
3102 78371 : SET_EXPR_LOCATION (call, input_location);
3103 109366466 : call_stack.safe_push (call);
3104 109366466 : int len = call_stack.length ();
3105 109366466 : if (len > max_constexpr_depth)
3106 33 : return false;
3107 : return len;
3108 : }
3109 :
3110 : static void
3111 109366462 : pop_cx_call_context (void)
3112 : {
3113 109366462 : ++call_stack_tick;
3114 109366462 : call_stack.pop ();
3115 109366462 : }
3116 :
3117 : vec<tree>
3118 257777 : cx_error_context (void)
3119 : {
3120 257777 : vec<tree> r = vNULL;
3121 257777 : if (call_stack_tick != last_cx_error_tick
3122 257777 : && !call_stack.is_empty ())
3123 : r = call_stack;
3124 257777 : last_cx_error_tick = call_stack_tick;
3125 257777 : return r;
3126 : }
3127 :
3128 : /* E is an operand of a failed assertion, fold it either with or without
3129 : constexpr context. */
3130 :
3131 : static tree
3132 817 : fold_operand (tree e, const constexpr_ctx *ctx)
3133 : {
3134 817 : if (ctx)
3135 : {
3136 32 : bool new_non_constant_p = false, new_overflow_p = false;
3137 32 : tree jmp_target = NULL_TREE;
3138 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3139 : &new_non_constant_p,
3140 : &new_overflow_p, &jmp_target);
3141 : }
3142 : else
3143 785 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3144 817 : return e;
3145 : }
3146 :
3147 : /* If we have a condition in conjunctive normal form (CNF), find the first
3148 : failing clause. In other words, given an expression like
3149 :
3150 : true && true && false && true && false
3151 :
3152 : return the first 'false'. EXPR is the expression. */
3153 :
3154 : static tree
3155 332 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3156 : {
3157 437 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3158 : {
3159 : /* First check the left side... */
3160 192 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3161 192 : if (e == NULL_TREE)
3162 : /* ...if we didn't find a false clause, check the right side. */
3163 105 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3164 : return e;
3165 : }
3166 245 : tree e = contextual_conv_bool (expr, tf_none);
3167 245 : e = fold_operand (e, ctx);
3168 245 : if (integer_zerop (e))
3169 : /* This is the failing clause. */
3170 140 : return expr;
3171 : return NULL_TREE;
3172 : }
3173 :
3174 : /* Wrapper for find_failing_clause_r. */
3175 :
3176 : tree
3177 1966 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3178 : {
3179 1966 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3180 140 : if (tree e = find_failing_clause_r (ctx, expr))
3181 1966 : expr = e;
3182 1966 : return expr;
3183 : }
3184 :
3185 : /* Emit additional diagnostics for failing condition BAD.
3186 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3187 : If SHOW_EXPR_P is true, print the condition (because it was
3188 : instantiation-dependent). */
3189 :
3190 : void
3191 1966 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3192 : const constexpr_ctx *ctx /* = nullptr */)
3193 : {
3194 : /* Nobody wants to see the artificial (bool) cast. */
3195 1966 : bad = tree_strip_nop_conversions (bad);
3196 1966 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3197 3 : bad = TREE_OPERAND (bad, 0);
3198 :
3199 1966 : auto_diagnostic_nesting_level sentinel;
3200 :
3201 : /* Actually explain the failure if this is a concept check or a
3202 : requires-expression. */
3203 1966 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3204 322 : diagnose_constraints (cloc, bad, NULL_TREE);
3205 : /* Similarly if this is a standard trait. */
3206 1644 : else if (maybe_diagnose_standard_trait (cloc, bad))
3207 : ;
3208 1289 : else if (COMPARISON_CLASS_P (bad)
3209 1289 : && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))
3210 76 : || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))))
3211 : {
3212 286 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3213 286 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3214 286 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3215 286 : inform (cloc, "the comparison reduces to %qE", cond);
3216 : }
3217 1003 : else if (show_expr_p)
3218 755 : inform (cloc, "%qE evaluates to false", bad);
3219 1966 : }
3220 :
3221 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3222 : do it without changing the current evaluation state. If it evaluates to
3223 : false, complain and return false; otherwise, return true. */
3224 :
3225 : static bool
3226 173593 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3227 : location_t loc, bool evaluated,
3228 : bool *non_constant_p, bool *overflow_p)
3229 : {
3230 173593 : if (*non_constant_p)
3231 : return true;
3232 :
3233 173593 : tree eval, jmp_target = NULL_TREE;
3234 173593 : if (!evaluated)
3235 : {
3236 173593 : if (!potential_rvalue_constant_expression (arg))
3237 16 : return true;
3238 :
3239 173577 : constexpr_ctx new_ctx = *ctx;
3240 173577 : new_ctx.quiet = true;
3241 173577 : bool new_non_constant_p = false, new_overflow_p = false;
3242 : /* Avoid modification of existing values. */
3243 173577 : modifiable_tracker ms (new_ctx.global);
3244 173577 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3245 : &new_non_constant_p,
3246 : &new_overflow_p, &jmp_target);
3247 173577 : }
3248 : else
3249 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3250 : non_constant_p,
3251 : overflow_p, &jmp_target);
3252 173577 : if (jmp_target)
3253 : return true;
3254 :
3255 173577 : if (!*non_constant_p && integer_zerop (eval))
3256 : {
3257 42 : if (!ctx->quiet)
3258 : {
3259 : /* See if we can find which clause was failing
3260 : (for logical AND). */
3261 13 : tree bad = find_failing_clause (ctx, arg);
3262 : /* If not, or its location is unusable, fall back to the
3263 : previous location. */
3264 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3265 :
3266 : /* Report the error. */
3267 13 : auto_diagnostic_group d;
3268 13 : error_at (cloc, msg);
3269 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3270 13 : return bad;
3271 13 : }
3272 29 : *non_constant_p = true;
3273 29 : return false;
3274 : }
3275 :
3276 : return true;
3277 : }
3278 :
3279 : /* Evaluate a call T to a GCC internal function when possible and return
3280 : the evaluated result or, under the control of CTX, give an error, set
3281 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3282 :
3283 : static tree
3284 388077 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3285 : value_cat lval,
3286 : bool *non_constant_p, bool *overflow_p,
3287 : tree *jump_target)
3288 : {
3289 388077 : enum tree_code opcode = ERROR_MARK;
3290 :
3291 388077 : switch (CALL_EXPR_IFN (t))
3292 : {
3293 165 : case IFN_UBSAN_NULL:
3294 165 : case IFN_UBSAN_BOUNDS:
3295 165 : case IFN_UBSAN_VPTR:
3296 165 : case IFN_FALLTHROUGH:
3297 165 : return void_node;
3298 :
3299 173593 : case IFN_ASSUME:
3300 173593 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3301 : G_("failed %<assume%> attribute assumption"),
3302 173593 : EXPR_LOCATION (t), /*eval*/false,
3303 : non_constant_p, overflow_p))
3304 : return t;
3305 173564 : return void_node;
3306 :
3307 : case IFN_ADD_OVERFLOW:
3308 : opcode = PLUS_EXPR;
3309 : break;
3310 385 : case IFN_SUB_OVERFLOW:
3311 385 : opcode = MINUS_EXPR;
3312 385 : break;
3313 206584 : case IFN_MUL_OVERFLOW:
3314 206584 : opcode = MULT_EXPR;
3315 206584 : break;
3316 :
3317 96 : case IFN_LAUNDER:
3318 96 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3319 : vc_prvalue, non_constant_p,
3320 96 : overflow_p, jump_target);
3321 :
3322 0 : case IFN_DEFERRED_INIT:
3323 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3324 :
3325 120 : case IFN_BSWAP:
3326 120 : case IFN_BITREVERSE:
3327 120 : {
3328 120 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3329 : vc_prvalue, non_constant_p,
3330 : overflow_p, jump_target);
3331 120 : if (*jump_target)
3332 : return NULL_TREE;
3333 120 : if (*non_constant_p)
3334 : return t;
3335 60 : location_t loc = cp_expr_loc_or_input_loc (t);
3336 60 : if (TREE_CODE (arg) != INTEGER_CST
3337 60 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg))
3338 60 : || !TYPE_UNSIGNED (TREE_TYPE (arg))
3339 120 : || (CALL_EXPR_IFN (t) == IFN_BSWAP
3340 30 : && (TYPE_PRECISION (TREE_TYPE (arg)) % 8) != 0))
3341 : {
3342 0 : if (!ctx->quiet)
3343 0 : error_at (loc, "call to internal function %qE", t);
3344 0 : *non_constant_p = true;
3345 0 : return t;
3346 : }
3347 60 : return fold_build_builtin_bswapg_bitreverseg (loc, CALL_EXPR_IFN (t),
3348 60 : arg);
3349 : }
3350 :
3351 6634 : case IFN_VEC_CONVERT:
3352 6634 : {
3353 6634 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3354 : vc_prvalue, non_constant_p,
3355 : overflow_p, jump_target);
3356 6634 : if (*jump_target)
3357 : return NULL_TREE;
3358 6634 : if (TREE_CODE (arg) == VECTOR_CST)
3359 6237 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3360 : return r;
3361 : }
3362 : /* FALLTHRU */
3363 :
3364 402 : default:
3365 402 : if (!ctx->quiet)
3366 0 : error_at (cp_expr_loc_or_input_loc (t),
3367 : "call to internal function %qE", t);
3368 402 : *non_constant_p = true;
3369 402 : return t;
3370 : }
3371 :
3372 : /* Evaluate constant arguments using OPCODE and return a complex
3373 : number containing the result and the overflow bit. */
3374 207469 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3375 : non_constant_p, overflow_p,
3376 : jump_target);
3377 207469 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3378 : non_constant_p, overflow_p,
3379 : jump_target);
3380 207469 : if (*jump_target)
3381 : return NULL_TREE;
3382 207469 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3383 : {
3384 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3385 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3386 2 : tree result = fold_binary_loc (loc, opcode, type,
3387 : fold_convert_loc (loc, type, arg0),
3388 : fold_convert_loc (loc, type, arg1));
3389 2 : tree ovf
3390 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3391 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3392 2 : if (TREE_OVERFLOW (result))
3393 0 : TREE_OVERFLOW (result) = 0;
3394 :
3395 2 : return build_complex (TREE_TYPE (t), result, ovf);
3396 : }
3397 :
3398 207467 : *non_constant_p = true;
3399 207467 : return t;
3400 : }
3401 :
3402 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3403 :
3404 : static void
3405 5978815 : clear_no_implicit_zero (tree ctor)
3406 : {
3407 5978815 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3408 : {
3409 1739711 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3410 7365521 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3411 2314612 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3412 729274 : clear_no_implicit_zero (e.value);
3413 : }
3414 5978815 : }
3415 :
3416 : /* Complain about a const object OBJ being modified in a constant expression.
3417 : EXPR is the MODIFY_EXPR expression performing the modification. */
3418 :
3419 : static void
3420 63 : modifying_const_object_error (tree expr, tree obj)
3421 : {
3422 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3423 63 : auto_diagnostic_group d;
3424 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3425 63 : "a constant expression", TREE_OPERAND (expr, 0));
3426 :
3427 : /* Find the underlying object that was declared as const. */
3428 63 : location_t decl_loc = UNKNOWN_LOCATION;
3429 201 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3430 75 : switch (TREE_CODE (probe))
3431 : {
3432 39 : case BIT_FIELD_REF:
3433 39 : case COMPONENT_REF:
3434 39 : {
3435 39 : tree elt = TREE_OPERAND (probe, 1);
3436 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3437 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3438 39 : probe = TREE_OPERAND (probe, 0);
3439 : }
3440 39 : break;
3441 :
3442 0 : case ARRAY_REF:
3443 0 : case REALPART_EXPR:
3444 0 : case IMAGPART_EXPR:
3445 0 : probe = TREE_OPERAND (probe, 0);
3446 0 : break;
3447 :
3448 36 : default:
3449 36 : decl_loc = location_of (probe);
3450 36 : break;
3451 : }
3452 63 : inform (decl_loc, "originally declared %<const%> here");
3453 63 : }
3454 :
3455 : /* Return true if FNDECL is a replaceable global allocation function that
3456 : should be usable during constant expression evaluation. */
3457 :
3458 : static inline bool
3459 38706531 : cxx_replaceable_global_alloc_fn (tree fndecl)
3460 : {
3461 38706531 : return (cxx_dialect >= cxx20
3462 36642180 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3463 2241595 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3464 40947741 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3465 927491 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3466 : }
3467 :
3468 : /* Return true if FNDECL is a placement new function that should be
3469 : usable during constant expression evaluation of std::construct_at. */
3470 :
3471 : static inline bool
3472 37225942 : cxx_placement_new_fn (tree fndecl)
3473 : {
3474 37225942 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3475 : }
3476 :
3477 : /* Return true if FNDECL is std::construct_at. */
3478 :
3479 : static inline bool
3480 2361661 : is_std_construct_at (tree fndecl)
3481 : {
3482 2361661 : if (!decl_in_std_namespace_p (fndecl))
3483 : return false;
3484 :
3485 2319851 : tree name = DECL_NAME (fndecl);
3486 2319851 : return name && id_equal (name, "construct_at");
3487 : }
3488 :
3489 : /* Overload for the above taking constexpr_call*. */
3490 :
3491 : static inline bool
3492 2120680 : is_std_construct_at (const constexpr_call *call)
3493 : {
3494 2120680 : return (call
3495 1958745 : && call->fundef
3496 4079425 : && is_std_construct_at (call->fundef->decl));
3497 : }
3498 :
3499 : /* True if CTX is an instance of std::NAME class. */
3500 :
3501 : bool
3502 12248894 : is_std_class (tree ctx, const char *name)
3503 : {
3504 12248894 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3505 : return false;
3506 :
3507 12248272 : tree decl = TYPE_MAIN_DECL (ctx);
3508 12248272 : tree dname = DECL_NAME (decl);
3509 12248272 : if (dname == NULL_TREE || !id_equal (dname, name))
3510 : return false;
3511 :
3512 801938 : return decl_in_std_namespace_p (decl);
3513 : }
3514 :
3515 : /* True if CTX is an instance of std::allocator. */
3516 :
3517 : bool
3518 619515 : is_std_allocator (tree ctx)
3519 : {
3520 619515 : return is_std_class (ctx, "allocator");
3521 : }
3522 :
3523 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3524 :
3525 : static bool
3526 660630 : is_std_allocator_allocate (tree fndecl)
3527 : {
3528 660630 : tree name = DECL_NAME (fndecl);
3529 660630 : if (name == NULL_TREE
3530 660630 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3531 : return false;
3532 :
3533 617887 : return is_std_allocator (DECL_CONTEXT (fndecl));
3534 : }
3535 :
3536 : /* Overload for the above taking constexpr_call*. */
3537 :
3538 : static inline bool
3539 495048 : is_std_allocator_allocate (const constexpr_call *call)
3540 : {
3541 495048 : return (call
3542 298066 : && call->fundef
3543 793114 : && is_std_allocator_allocate (call->fundef->decl));
3544 : }
3545 :
3546 : /* Return true if FNDECL is std::source_location::current. */
3547 :
3548 : static inline bool
3549 54321 : is_std_source_location_current (tree fndecl)
3550 : {
3551 54321 : if (!decl_in_std_namespace_p (fndecl))
3552 : return false;
3553 :
3554 37488 : tree name = DECL_NAME (fndecl);
3555 37488 : if (name == NULL_TREE || !id_equal (name, "current"))
3556 : return false;
3557 :
3558 159 : tree ctx = DECL_CONTEXT (fndecl);
3559 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3560 : return false;
3561 :
3562 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3563 159 : return name && id_equal (name, "source_location");
3564 : }
3565 :
3566 : /* Overload for the above taking constexpr_call*. */
3567 :
3568 : static inline bool
3569 81467 : is_std_source_location_current (const constexpr_call *call)
3570 : {
3571 81467 : return (call
3572 54321 : && call->fundef
3573 135788 : && is_std_source_location_current (call->fundef->decl));
3574 : }
3575 :
3576 : /* Return true if FNDECL is __dynamic_cast. */
3577 :
3578 : static inline bool
3579 36915150 : cxx_dynamic_cast_fn_p (tree fndecl)
3580 : {
3581 36915150 : return (cxx_dialect >= cxx20
3582 34850794 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3583 6788 : && CP_DECL_CONTEXT (fndecl) == abi_node
3584 : /* Only consider implementation-detail __dynamic_cast calls that
3585 : correspond to a dynamic_cast, and ignore direct calls to
3586 : abi::__dynamic_cast. */
3587 36921938 : && DECL_ARTIFICIAL (fndecl));
3588 : }
3589 :
3590 : /* Often, we have an expression in the form of address + offset, e.g.
3591 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3592 :
3593 : static tree
3594 5840 : extract_obj_from_addr_offset (tree expr)
3595 : {
3596 5840 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3597 2786 : expr = TREE_OPERAND (expr, 0);
3598 5840 : STRIP_NOPS (expr);
3599 5840 : if (TREE_CODE (expr) == ADDR_EXPR)
3600 5829 : expr = TREE_OPERAND (expr, 0);
3601 5840 : return expr;
3602 : }
3603 :
3604 : /* Given a PATH like
3605 :
3606 : g.D.2181.D.2154.D.2102.D.2093
3607 :
3608 : find a component with type TYPE. Return NULL_TREE if not found, and
3609 : error_mark_node if the component is not accessible. If STOP is non-null,
3610 : this function will return NULL_TREE if STOP is found before TYPE. */
3611 :
3612 : static tree
3613 2831 : get_component_with_type (tree path, tree type, tree stop)
3614 : {
3615 7921 : while (true)
3616 : {
3617 5376 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3618 : /* Found it. */
3619 : return path;
3620 3761 : else if (stop
3621 3761 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3622 : stop)))
3623 : return NULL_TREE;
3624 3716 : else if (TREE_CODE (path) == COMPONENT_REF
3625 3716 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3626 : {
3627 : /* We need to check that the component we're accessing is in fact
3628 : accessible. */
3629 3716 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3630 3716 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3631 1171 : return error_mark_node;
3632 2545 : path = TREE_OPERAND (path, 0);
3633 : }
3634 : else
3635 : return NULL_TREE;
3636 : }
3637 : }
3638 :
3639 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3640 :
3641 : The declaration of __dynamic_cast is:
3642 :
3643 : void* __dynamic_cast (const void* __src_ptr,
3644 : const __class_type_info* __src_type,
3645 : const __class_type_info* __dst_type,
3646 : ptrdiff_t __src2dst);
3647 :
3648 : where src2dst has the following possible values
3649 :
3650 : >-1: src_type is a unique public non-virtual base of dst_type
3651 : dst_ptr + src2dst == src_ptr
3652 : -1: unspecified relationship
3653 : -2: src_type is not a public base of dst_type
3654 : -3: src_type is a multiple public non-virtual base of dst_type */
3655 :
3656 : static tree
3657 3173 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3658 : bool *non_constant_p, bool *overflow_p,
3659 : tree *jump_target)
3660 : {
3661 : /* T will be something like
3662 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3663 : dismantle it. */
3664 3173 : gcc_assert (call_expr_nargs (call) == 4);
3665 3173 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3666 3173 : tree obj = CALL_EXPR_ARG (call, 0);
3667 3173 : tree type = CALL_EXPR_ARG (call, 2);
3668 3173 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3669 3173 : location_t loc = cp_expr_loc_or_input_loc (call);
3670 :
3671 : /* Get the target type of the dynamic_cast. */
3672 3173 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3673 3173 : type = TREE_OPERAND (type, 0);
3674 3173 : type = TREE_TYPE (DECL_NAME (type));
3675 :
3676 : /* TYPE can only be either T* or T&. We can't know which of these it
3677 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3678 : and something like "(T*)(T&)(T*) x" in the second case.
3679 : This is true for the reference cases in C++ < 26 or when exceptions
3680 : aren't enabled, in that case we should diagnose errors. For C++26
3681 : with exceptions we should silently evaluate to null pointer and
3682 : let the callers call __cxa_bad_cast () later to throw an exception. */
3683 3173 : bool fail_for_non_constant_p = false;
3684 12899 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3685 : {
3686 9726 : if (cxx_dialect < cxx26 || !flag_exceptions)
3687 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3688 9726 : obj = TREE_OPERAND (obj, 0);
3689 : }
3690 :
3691 : /* Evaluate the object so that we know its dynamic type. */
3692 3173 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3693 : overflow_p, jump_target);
3694 3173 : if (*non_constant_p)
3695 : return call;
3696 3054 : if (*jump_target)
3697 : return NULL_TREE;
3698 :
3699 : /* For dynamic_cast from classes with virtual bases we can get something
3700 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3701 : d.D.1234 using cxx_fold_indirect_ref. */
3702 3054 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3703 : {
3704 1206 : tree objo = obj;
3705 1206 : STRIP_NOPS (objo);
3706 1206 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3707 : {
3708 1152 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3709 : obj, NULL, jump_target);
3710 1152 : if (objo)
3711 1152 : obj = build_fold_addr_expr (objo);
3712 : }
3713 : }
3714 :
3715 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3716 : but when HINT is > 0, it can also be something like
3717 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3718 3054 : obj = extract_obj_from_addr_offset (obj);
3719 3054 : const tree objtype = TREE_TYPE (obj);
3720 : /* If OBJ doesn't refer to a base field, we're done. */
3721 6029 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3722 3054 : ? TREE_OPERAND (obj, 1) : obj))
3723 3054 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3724 : {
3725 79 : if (fail_for_non_constant_p)
3726 : {
3727 55 : if (!ctx->quiet)
3728 : {
3729 2 : auto_diagnostic_group d;
3730 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3731 2 : inform (loc, "dynamic type %qT of its operand does "
3732 : "not have a base class of type %qT",
3733 : objtype, type);
3734 2 : }
3735 55 : *non_constant_p = true;
3736 : }
3737 79 : return integer_zero_node;
3738 : }
3739 :
3740 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3741 : or in a destructor ... if the operand of the dynamic_cast refers
3742 : to the object under construction or destruction, this object is
3743 : considered to be a most derived object that has the type of the
3744 : constructor or destructor's class. */
3745 2975 : tree vtable = build_vfield_ref (obj, objtype);
3746 2975 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3747 : non_constant_p, overflow_p,
3748 : jump_target);
3749 2975 : if (*non_constant_p)
3750 : return call;
3751 2798 : if (*jump_target)
3752 : return NULL_TREE;
3753 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3754 : so it's possible that we got a null pointer now. */
3755 2798 : if (integer_zerop (vtable))
3756 : {
3757 12 : if (!ctx->quiet)
3758 3 : error_at (loc, "virtual table pointer is used uninitialized");
3759 12 : *non_constant_p = true;
3760 12 : return integer_zero_node;
3761 : }
3762 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3763 2786 : vtable = extract_obj_from_addr_offset (vtable);
3764 2786 : const tree mdtype = DECL_CONTEXT (vtable);
3765 :
3766 : /* Given dynamic_cast<T>(v),
3767 :
3768 : [expr.dynamic.cast] If C is the class type to which T points or refers,
3769 : the runtime check logically executes as follows:
3770 :
3771 : If, in the most derived object pointed (referred) to by v, v points
3772 : (refers) to a public base class subobject of a C object, and if only
3773 : one object of type C is derived from the subobject pointed (referred)
3774 : to by v the result points (refers) to that C object.
3775 :
3776 : In this case, HINT >= 0 or -3. */
3777 2786 : if (hint >= 0 || hint == -3)
3778 : {
3779 : /* Look for a component with type TYPE. */
3780 687 : tree t = get_component_with_type (obj, type, mdtype);
3781 : /* If not accessible, give an error. */
3782 687 : if (t == error_mark_node)
3783 : {
3784 198 : if (fail_for_non_constant_p)
3785 : {
3786 108 : if (!ctx->quiet)
3787 : {
3788 12 : auto_diagnostic_group d;
3789 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3790 12 : inform (loc, "static type %qT of its operand is a "
3791 : "non-public base class of dynamic type %qT",
3792 : objtype, type);
3793 :
3794 12 : }
3795 108 : *non_constant_p = true;
3796 : }
3797 198 : return integer_zero_node;
3798 : }
3799 489 : else if (t)
3800 : /* The result points to the TYPE object. */
3801 444 : return cp_build_addr_expr (t, complain);
3802 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3803 : Fall through to the normal processing. */
3804 : }
3805 :
3806 : /* Otherwise, if v points (refers) to a public base class subobject of the
3807 : most derived object, and the type of the most derived object has a base
3808 : class, of type C, that is unambiguous and public, the result points
3809 : (refers) to the C subobject of the most derived object.
3810 :
3811 : But it can also be an invalid case. */
3812 :
3813 : /* Get the most derived object. */
3814 2144 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3815 2144 : if (obj == error_mark_node)
3816 : {
3817 973 : if (fail_for_non_constant_p)
3818 : {
3819 350 : if (!ctx->quiet)
3820 : {
3821 38 : auto_diagnostic_group d;
3822 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3823 38 : inform (loc, "static type %qT of its operand is a non-public"
3824 : " base class of dynamic type %qT", objtype, mdtype);
3825 38 : }
3826 350 : *non_constant_p = true;
3827 : }
3828 973 : return integer_zero_node;
3829 : }
3830 : else
3831 1171 : gcc_assert (obj);
3832 :
3833 : /* Check that the type of the most derived object has a base class
3834 : of type TYPE that is unambiguous and public. */
3835 1171 : base_kind b_kind;
3836 1171 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3837 1171 : if (!binfo || binfo == error_mark_node)
3838 : {
3839 441 : if (fail_for_non_constant_p)
3840 : {
3841 134 : if (!ctx->quiet)
3842 : {
3843 16 : auto_diagnostic_group d;
3844 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3845 16 : if (b_kind == bk_ambig)
3846 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3847 : "type %qT of its operand", type, mdtype);
3848 : else
3849 10 : inform (loc, "dynamic type %qT of its operand does not "
3850 : "have an unambiguous public base class %qT",
3851 : mdtype, type);
3852 16 : }
3853 134 : *non_constant_p = true;
3854 : }
3855 441 : return integer_zero_node;
3856 : }
3857 : /* If so, return the TYPE subobject of the most derived object. */
3858 730 : obj = convert_to_base_statically (obj, binfo);
3859 730 : return cp_build_addr_expr (obj, complain);
3860 : }
3861 :
3862 : /* Data structure used by replace_decl and replace_decl_r. */
3863 :
3864 : struct replace_decl_data
3865 : {
3866 : /* The _DECL we want to replace. */
3867 : tree decl;
3868 : /* The replacement for DECL. */
3869 : tree replacement;
3870 : /* Trees we've visited. */
3871 : hash_set<tree> *pset;
3872 : /* Whether we've performed any replacements. */
3873 : bool changed;
3874 : };
3875 :
3876 : /* Helper function for replace_decl, called through cp_walk_tree. */
3877 :
3878 : static tree
3879 16985308 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3880 : {
3881 16985308 : replace_decl_data *d = (replace_decl_data *) data;
3882 :
3883 : /* We could be replacing
3884 : &<retval>.bar -> &foo.bar
3885 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3886 : on the ADDR_EXPR around it. */
3887 16985308 : if (TREE_CODE (*tp) == ADDR_EXPR)
3888 : {
3889 1529036 : d->pset->add (*tp);
3890 1529036 : auto save_changed = d->changed;
3891 1529036 : d->changed = false;
3892 1529036 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3893 1529036 : if (d->changed)
3894 : {
3895 1849 : cxx_mark_addressable (*tp);
3896 1849 : recompute_tree_invariant_for_addr_expr (*tp);
3897 : }
3898 : else
3899 1527187 : d->changed = save_changed;
3900 1529036 : *walk_subtrees = 0;
3901 : }
3902 15456272 : else if (*tp == d->decl)
3903 : {
3904 2089 : *tp = unshare_expr (d->replacement);
3905 2089 : d->changed = true;
3906 2089 : *walk_subtrees = 0;
3907 : }
3908 15454183 : else if (TYPE_P (*tp)
3909 15454183 : || d->pset->add (*tp))
3910 2832839 : *walk_subtrees = 0;
3911 :
3912 16985308 : return NULL_TREE;
3913 : }
3914 :
3915 : /* Replace every occurrence of DECL with (an unshared copy of)
3916 : REPLACEMENT within the expression *TP. Returns true iff a
3917 : replacement was performed. */
3918 :
3919 : bool
3920 2264937 : replace_decl (tree *tp, tree decl, tree replacement)
3921 : {
3922 2264937 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3923 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3924 2264937 : hash_set<tree> pset;
3925 2264937 : replace_decl_data data = { decl, replacement, &pset, false };
3926 2264937 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3927 2264937 : return data.changed;
3928 2264937 : }
3929 :
3930 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3931 :
3932 : static tree
3933 44 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3934 : value_cat lval,
3935 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3936 : {
3937 44 : tree function = THUNK_TARGET (thunk_fndecl);
3938 :
3939 44 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3940 : {
3941 13 : if (!ctx->quiet)
3942 : {
3943 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3944 : {
3945 0 : error ("call to non-%<constexpr%> function %qD", function);
3946 0 : explain_invalid_constexpr_fn (function);
3947 : }
3948 : else
3949 : /* virtual_offset is only set for virtual bases, which make the
3950 : class non-literal, so we don't need to handle it here. */
3951 3 : error ("calling constexpr member function %qD through virtual "
3952 : "base subobject", function);
3953 : }
3954 13 : *non_constant_p = true;
3955 13 : return t;
3956 : }
3957 :
3958 31 : tree new_call = copy_node (t);
3959 31 : CALL_EXPR_FN (new_call) = function;
3960 31 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3961 :
3962 31 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3963 :
3964 31 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3965 : {
3966 : /* 'this'-adjusting thunk. */
3967 19 : tree this_arg = CALL_EXPR_ARG (t, 0);
3968 19 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3969 : this_arg, offset);
3970 19 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3971 : }
3972 : else
3973 : {
3974 : /* Return-adjusting thunk. As in expand_thunk, adjust a pointer only
3975 : if it is non-null. */
3976 12 : tree call = new_call;
3977 12 : if (TYPE_PTR_P (TREE_TYPE (call)))
3978 12 : call = save_expr (call);
3979 12 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (call), call, offset);
3980 12 : if (TYPE_PTR_P (TREE_TYPE (call)))
3981 12 : new_call = build_if_nonnull (call, new_call, tf_none);
3982 : }
3983 :
3984 31 : tree result = cxx_eval_constant_expression (ctx, new_call, lval,
3985 : non_constant_p, overflow_p,
3986 : jump_target);
3987 31 : if (!*non_constant_p
3988 27 : && !*overflow_p
3989 27 : && !*jump_target
3990 27 : && result != void_node
3991 27 : && INDIRECT_TYPE_P (TREE_TYPE (t))
3992 55 : && !(same_type_ignoring_top_level_qualifiers_p
3993 24 : (TREE_TYPE (result), TREE_TYPE (t))))
3994 15 : result = adjust_temp_type (TREE_TYPE (t), result);
3995 : return result;
3996 : }
3997 :
3998 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3999 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
4000 : 'tors to detect modifying const objects in a constexpr context. */
4001 :
4002 : static void
4003 11164812 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
4004 : bool readonly_p, bool *non_constant_p,
4005 : bool *overflow_p, tree *jump_target)
4006 : {
4007 22329624 : if (CLASS_TYPE_P (TREE_TYPE (object))
4008 22329624 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
4009 : {
4010 : /* Subobjects might not be stored in ctx->global->values but we
4011 : can get its CONSTRUCTOR by evaluating *this. */
4012 1041156 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
4013 : non_constant_p, overflow_p,
4014 : jump_target);
4015 1041156 : if (!*non_constant_p
4016 12164232 : && !throws (jump_target)
4017 2040576 : && TREE_CODE (e) == CONSTRUCTOR)
4018 999420 : TREE_READONLY (e) = readonly_p;
4019 : }
4020 11164812 : }
4021 :
4022 : /* Allocate an exception for OBJECT and throw it. */
4023 :
4024 : tree
4025 3164 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
4026 : tree object)
4027 : {
4028 3164 : tree type = TREE_TYPE (object);
4029 : /* This simulates a call to __cxa_allocate_exception. We need
4030 : (struct exception *) &heap -- memory on the heap so that
4031 : it can survive the stack being unwound. */
4032 3164 : tree arr = build_array_of_n_type (type, 1);
4033 3164 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
4034 3164 : DECL_NAME (var) = heap_identifier;
4035 3164 : ctx->global->put_value (var, NULL_TREE);
4036 :
4037 : /* *(struct exception *) &heap = exc{ ... } */
4038 3164 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
4039 3164 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
4040 3164 : bool non_constant_p = false, overflow_p = false;
4041 3164 : tree jump_target = NULL_TREE;
4042 3164 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
4043 : &overflow_p, &jump_target);
4044 3164 : if (non_constant_p)
4045 : {
4046 0 : if (!ctx->quiet)
4047 0 : error_at (loc, "couldn%'t throw %qT", type);
4048 : return NULL_TREE;
4049 : }
4050 :
4051 : /* Now we can __cxa_throw. */
4052 3164 : DECL_EXCEPTION_REFCOUNT (var)
4053 3164 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
4054 3164 : ++ctx->global->uncaught_exceptions;
4055 :
4056 3164 : return var;
4057 : }
4058 :
4059 : /* Subroutine of cxx_eval_constant_expression.
4060 : Evaluate the call expression tree T in the context of OLD_CALL expression
4061 : evaluation. */
4062 :
4063 : static tree
4064 206705676 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
4065 : value_cat lval,
4066 : bool *non_constant_p, bool *overflow_p,
4067 : tree *jump_target)
4068 : {
4069 206705676 : location_t loc = cp_expr_loc_or_input_loc (t);
4070 206705676 : tree fun = get_function_named_in_call (t);
4071 :
4072 206705676 : if (fun == NULL_TREE)
4073 388077 : return cxx_eval_internal_function (ctx, t, lval,
4074 : non_constant_p, overflow_p,
4075 388077 : jump_target);
4076 :
4077 206317599 : if (TREE_CODE (fun) != FUNCTION_DECL)
4078 : {
4079 : /* Might be a constexpr function pointer. */
4080 419338 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
4081 : non_constant_p, overflow_p,
4082 : jump_target);
4083 419338 : if (*jump_target)
4084 : return NULL_TREE;
4085 419338 : STRIP_NOPS (fun);
4086 419338 : if (TREE_CODE (fun) == ADDR_EXPR)
4087 31229 : fun = TREE_OPERAND (fun, 0);
4088 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
4089 : indirection, the called expression is a pointer into the
4090 : virtual table which should contain FDESC_EXPR. Extract the
4091 : FUNCTION_DECL from there. */
4092 : else if (TARGET_VTABLE_USES_DESCRIPTORS
4093 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
4094 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
4095 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
4096 : {
4097 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
4098 : if (VAR_P (d)
4099 : && DECL_VTABLE_OR_VTT_P (d)
4100 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
4101 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
4102 : && DECL_INITIAL (d)
4103 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
4104 : {
4105 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
4106 : TYPE_SIZE_UNIT (vtable_entry_type));
4107 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
4108 : if (idx >= 0)
4109 : {
4110 : tree fdesc
4111 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
4112 : if (TREE_CODE (fdesc) == FDESC_EXPR
4113 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
4114 : fun = TREE_OPERAND (fdesc, 0);
4115 : }
4116 : }
4117 : }
4118 : }
4119 206317599 : if (TREE_CODE (fun) != FUNCTION_DECL)
4120 : {
4121 388109 : if (!ctx->quiet && !*non_constant_p)
4122 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
4123 : "function", fun);
4124 388109 : *non_constant_p = true;
4125 388109 : return t;
4126 : }
4127 205929490 : tree orig_fun = fun;
4128 205929490 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
4129 24540169 : fun = DECL_CLONED_FUNCTION (fun);
4130 :
4131 205929490 : if (is_ubsan_builtin_p (fun))
4132 58 : return void_node;
4133 :
4134 205929432 : if (fndecl_built_in_p (fun))
4135 20720020 : return cxx_eval_builtin_function_call (ctx, t, fun,
4136 : lval, non_constant_p, overflow_p,
4137 20720018 : jump_target);
4138 185209412 : if (DECL_THUNK_P (fun))
4139 44 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
4140 44 : jump_target);
4141 185209368 : if (metafunction_p (fun))
4142 : {
4143 : /* To be able to evaluate a metafunction, we may have to instantiate
4144 : constexpr functions. If we're not allowed to instantiate, leave
4145 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4146 : otherwise we might fold those prematurely. See
4147 : g++.dg/reflect/p2996-17.C. */
4148 82784 : if (uid_sensitive_constexpr_evaluation_p ()
4149 82222 : || ctx->manifestly_const_eval == mce_unknown)
4150 : {
4151 20822 : *non_constant_p = true;
4152 20822 : return t;
4153 : }
4154 61962 : ctx->global->state_dependent = true;
4155 61962 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4156 : jump_target);
4157 61962 : if (*jump_target)
4158 : return NULL_TREE;
4159 58786 : if (*non_constant_p)
4160 : return t;
4161 57718 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4162 : non_constant_p, overflow_p,
4163 : jump_target);
4164 57718 : if (*jump_target)
4165 : return NULL_TREE;
4166 57718 : if (*non_constant_p)
4167 : return t;
4168 57712 : return e;
4169 : }
4170 185126584 : bool non_constexpr_call = false;
4171 185126584 : if (!maybe_constexpr_fn (fun))
4172 : {
4173 905049 : if (TREE_CODE (t) == CALL_EXPR
4174 897522 : && cxx_replaceable_global_alloc_fn (fun)
4175 1570376 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4176 331516 : || is_std_allocator_allocate (ctx->call)))
4177 : {
4178 495491 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4179 495491 : const int nargs = call_expr_nargs (t);
4180 495491 : tree arg0 = NULL_TREE;
4181 786702 : for (int i = 0; i < nargs; ++i)
4182 : {
4183 496560 : tree arg = CALL_EXPR_ARG (t, i);
4184 496560 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4185 : non_constant_p, overflow_p,
4186 : jump_target);
4187 496560 : if (*jump_target)
4188 : return NULL_TREE;
4189 : /* Deleting a non-constant pointer has a better error message
4190 : below. */
4191 496550 : if (new_op_p || i != 0)
4192 415950 : VERIFY_CONSTANT (arg);
4193 210611 : if (i == 0)
4194 : arg0 = arg;
4195 : }
4196 290142 : gcc_assert (arg0);
4197 290142 : if (new_op_p)
4198 : {
4199 209542 : if (!tree_fits_uhwi_p (arg0))
4200 : {
4201 : /* We should not get here; the VERIFY_CONSTANT above
4202 : should have already caught it. */
4203 0 : gcc_checking_assert (false);
4204 : if (!ctx->quiet)
4205 : error_at (loc, "cannot allocate array: size not constant");
4206 : *non_constant_p = true;
4207 : return t;
4208 : }
4209 419084 : tree type = build_array_type_nelts (char_type_node,
4210 209542 : tree_to_uhwi (arg0));
4211 209542 : tree var = build_decl (loc, VAR_DECL,
4212 209542 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4213 : & OVL_OP_FLAG_VEC)
4214 : ? heap_vec_uninit_identifier
4215 : : heap_uninit_identifier,
4216 209542 : type);
4217 209542 : DECL_ARTIFICIAL (var) = 1;
4218 209542 : ctx->global->heap_vars.safe_push (var);
4219 209542 : ctx->global->put_value (var, NULL_TREE);
4220 209542 : return fold_convert (ptr_type_node, build_address (var));
4221 : }
4222 : else
4223 : {
4224 80600 : STRIP_NOPS (arg0);
4225 80600 : if (TREE_CODE (arg0) == ADDR_EXPR
4226 80600 : && VAR_P (TREE_OPERAND (arg0, 0)))
4227 : {
4228 80600 : tree var = TREE_OPERAND (arg0, 0);
4229 80600 : if (DECL_NAME (var) == heap_uninit_identifier
4230 80600 : || DECL_NAME (var) == heap_identifier)
4231 : {
4232 80426 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4233 : & OVL_OP_FLAG_VEC)
4234 : {
4235 9 : if (!ctx->quiet)
4236 : {
4237 3 : auto_diagnostic_group d;
4238 3 : error_at (loc, "array deallocation of object "
4239 : "allocated with non-array "
4240 : "allocation");
4241 3 : inform (DECL_SOURCE_LOCATION (var),
4242 : "allocation performed here");
4243 3 : }
4244 9 : *non_constant_p = true;
4245 9 : return t;
4246 : }
4247 80417 : DECL_NAME (var) = heap_deleted_identifier;
4248 80417 : ctx->global->destroy_value (var);
4249 80417 : ctx->global->heap_dealloc_count++;
4250 80417 : return void_node;
4251 : }
4252 174 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4253 174 : || DECL_NAME (var) == heap_vec_identifier)
4254 : {
4255 150 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4256 : & OVL_OP_FLAG_VEC) == 0)
4257 : {
4258 9 : if (!ctx->quiet)
4259 : {
4260 3 : auto_diagnostic_group d;
4261 3 : error_at (loc, "non-array deallocation of "
4262 : "object allocated with array "
4263 : "allocation");
4264 3 : inform (DECL_SOURCE_LOCATION (var),
4265 : "allocation performed here");
4266 3 : }
4267 9 : *non_constant_p = true;
4268 9 : return t;
4269 : }
4270 141 : DECL_NAME (var) = heap_deleted_identifier;
4271 141 : ctx->global->destroy_value (var);
4272 141 : ctx->global->heap_dealloc_count++;
4273 141 : return void_node;
4274 : }
4275 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4276 : {
4277 15 : if (!ctx->quiet)
4278 6 : error_at (loc, "deallocation of already deallocated "
4279 : "storage");
4280 15 : *non_constant_p = true;
4281 15 : return t;
4282 : }
4283 : }
4284 9 : if (!ctx->quiet)
4285 3 : error_at (loc, "deallocation of storage that was "
4286 : "not previously allocated");
4287 9 : *non_constant_p = true;
4288 9 : return t;
4289 : }
4290 : }
4291 : /* Allow placement new in std::construct_at, just return the second
4292 : argument. */
4293 409558 : if (TREE_CODE (t) == CALL_EXPR
4294 402031 : && cxx_placement_new_fn (fun)
4295 590290 : && is_std_construct_at (ctx->call))
4296 : {
4297 45897 : const int nargs = call_expr_nargs (t);
4298 45897 : tree arg1 = NULL_TREE;
4299 137691 : for (int i = 0; i < nargs; ++i)
4300 : {
4301 91794 : tree arg = CALL_EXPR_ARG (t, i);
4302 91794 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4303 : non_constant_p, overflow_p,
4304 : jump_target);
4305 91794 : if (*jump_target)
4306 : return NULL_TREE;
4307 91794 : if (i == 1)
4308 : arg1 = arg;
4309 : else
4310 91794 : VERIFY_CONSTANT (arg);
4311 : }
4312 45897 : gcc_assert (arg1);
4313 : return arg1;
4314 : }
4315 363661 : else if (cxx_dynamic_cast_fn_p (fun))
4316 3173 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4317 3173 : jump_target);
4318 360488 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4319 27013 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4320 : non_constant_p, overflow_p,
4321 27013 : jump_target);
4322 :
4323 : /* Calls to non-constexpr functions can be diagnosed right away
4324 : before C++26, though in C++26 evaluation of the arguments might
4325 : throw and if caught it could be still constant expression.
4326 : So for C++26 this is diagnosed only after
4327 : cxx_bind_parameters_in_call. */
4328 333475 : if (cxx_dialect >= cxx26)
4329 : non_constexpr_call = true;
4330 : else
4331 : {
4332 292297 : if (!ctx->quiet)
4333 : {
4334 208 : if (!lambda_static_thunk_p (fun))
4335 208 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4336 208 : explain_invalid_constexpr_fn (fun);
4337 : }
4338 292297 : *non_constant_p = true;
4339 292297 : return t;
4340 : }
4341 : }
4342 :
4343 184262713 : constexpr_ctx new_ctx = *ctx;
4344 184262713 : ctx = &new_ctx;
4345 207314501 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4346 188249729 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4347 : {
4348 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4349 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4350 2797 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4351 2797 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4352 2797 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4353 2797 : ctx->global->put_value (new_ctx.object, ctor);
4354 : }
4355 : /* An immediate invocation is manifestly constant evaluated including the
4356 : arguments of the call, so use mce_true even for the argument
4357 : evaluation. */
4358 368525426 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4359 4793730 : new_ctx.manifestly_const_eval = mce_true;
4360 :
4361 : /* We used to shortcut trivial constructor/op= here, but nowadays
4362 : we can only get a trivial function here with -fno-elide-constructors. */
4363 184262849 : gcc_checking_assert (!trivial_fn_p (fun)
4364 : || !flag_elide_constructors
4365 : /* Or it's a call from maybe_thunk_body (111075). */
4366 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4367 : : AGGR_INIT_FROM_THUNK_P (t))
4368 : /* We don't elide constructors when processing
4369 : a noexcept-expression. */
4370 : || cp_noexcept_operand);
4371 :
4372 184262713 : bool non_constant_args = false;
4373 184262713 : constexpr_call new_call;
4374 184262713 : new_call.bindings
4375 184262713 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4376 : overflow_p, &non_constant_args,
4377 : jump_target);
4378 :
4379 : /* We build up the bindings list before we know whether we already have this
4380 : call cached. If we don't end up saving these bindings, ggc_free them when
4381 : this function exits. */
4382 184262713 : class free_bindings
4383 : {
4384 : tree *bindings;
4385 : public:
4386 184262713 : free_bindings (tree &b): bindings (&b) { }
4387 184262709 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4388 4656083 : void preserve () { bindings = NULL; }
4389 184262713 : } fb (new_call.bindings);
4390 :
4391 184262713 : if (*jump_target)
4392 : return NULL_TREE;
4393 184262629 : if (*non_constant_p)
4394 : return t;
4395 109972806 : if (non_constexpr_call)
4396 : {
4397 6983 : if (!ctx->quiet)
4398 : {
4399 436 : if (!lambda_static_thunk_p (fun))
4400 436 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4401 436 : explain_invalid_constexpr_fn (fun);
4402 : }
4403 6983 : *non_constant_p = true;
4404 6983 : return t;
4405 : }
4406 :
4407 : /* We can't defer instantiating the function any longer. */
4408 109965823 : if (!DECL_INITIAL (fun)
4409 3165421 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4410 109965823 : && !uid_sensitive_constexpr_evaluation_p ())
4411 : {
4412 2909911 : location_t save_loc = input_location;
4413 2909911 : input_location = loc;
4414 2909911 : ++function_depth;
4415 2909911 : if (ctx->manifestly_const_eval == mce_true)
4416 383111 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4417 2909911 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4418 2909897 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4419 : else
4420 14 : synthesize_method (fun);
4421 2909911 : --function_depth;
4422 2909911 : input_location = save_loc;
4423 : }
4424 :
4425 : /* If in direct recursive call, optimize definition search. */
4426 109965823 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4427 29271 : new_call.fundef = ctx->call->fundef;
4428 : else
4429 : {
4430 109936552 : new_call.fundef = retrieve_constexpr_fundef (fun);
4431 109936552 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4432 109352824 : || new_call.fundef->result == error_mark_node
4433 109337201 : || fun == current_function_decl)
4434 : {
4435 599357 : if (!ctx->quiet)
4436 : {
4437 : /* We need to check for current_function_decl here in case we're
4438 : being called during cp_fold_function, because at that point
4439 : DECL_INITIAL is set properly and we have a fundef but we
4440 : haven't lowered invisirefs yet (c++/70344). */
4441 187 : if (DECL_INITIAL (fun) == error_mark_node
4442 187 : || fun == current_function_decl)
4443 15 : error_at (loc, "%qD called in a constant expression before its "
4444 : "definition is complete", fun);
4445 172 : else if (DECL_INITIAL (fun))
4446 : {
4447 : /* The definition of fun was somehow unsuitable. But pretend
4448 : that lambda static thunks don't exist. */
4449 134 : if (!lambda_static_thunk_p (fun))
4450 122 : error_at (loc, "%qD called in a constant expression", fun);
4451 134 : explain_invalid_constexpr_fn (fun);
4452 : }
4453 : else
4454 38 : error_at (loc, "%qD used before its definition", fun);
4455 : }
4456 599357 : *non_constant_p = true;
4457 599357 : return t;
4458 : }
4459 : }
4460 :
4461 : /* Don't complain about problems evaluating an ill-formed function. */
4462 109366466 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4463 109366466 : if (f->language->erroneous)
4464 976 : new_ctx.quiet = true;
4465 :
4466 109366466 : int depth_ok = push_cx_call_context (t);
4467 :
4468 : /* Remember the object we are constructing or destructing. */
4469 109366466 : tree new_obj = NULL_TREE;
4470 218732932 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4471 : {
4472 : /* In a cdtor, it should be the first `this' argument.
4473 : At this point it has already been evaluated in the call
4474 : to cxx_bind_parameters_in_call. */
4475 13792947 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4476 13792947 : bool empty_base = false;
4477 13792947 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4478 : &empty_base, jump_target);
4479 13792947 : if (*jump_target)
4480 0 : return NULL_TREE;
4481 : /* If we're initializing an empty class, don't set constness, because
4482 : cxx_fold_indirect_ref will return the wrong object to set constness
4483 : of. */
4484 13792947 : if (empty_base)
4485 : new_obj = NULL_TREE;
4486 6887097 : else if (ctx->call && ctx->call->fundef
4487 24997147 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4488 : {
4489 3603185 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4490 3603185 : STRIP_NOPS (cur_obj);
4491 3603185 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4492 3600680 : cur_obj = TREE_OPERAND (cur_obj, 0);
4493 3603185 : if (new_obj == cur_obj)
4494 : /* We're calling the target constructor of a delegating
4495 : constructor, or accessing a base subobject through a
4496 : NOP_EXPR as part of a call to a base constructor, so
4497 : there is no new (sub)object. */
4498 13792947 : new_obj = NULL_TREE;
4499 : }
4500 : }
4501 :
4502 109366466 : tree result = NULL_TREE;
4503 :
4504 109366466 : constexpr_call *entry = NULL;
4505 109366466 : if (depth_ok && !non_constant_args && ctx->strict)
4506 : {
4507 36925659 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4508 36925659 : new_call.hash
4509 36925659 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4510 :
4511 : /* If we have seen this call before, we are done. */
4512 36925659 : maybe_initialize_constexpr_call_table ();
4513 36925659 : bool insert = depth_ok < constexpr_cache_depth;
4514 36925659 : constexpr_call **slot
4515 50151465 : = constexpr_call_table->find_slot (&new_call,
4516 : insert ? INSERT : NO_INSERT);
4517 36925659 : entry = slot ? *slot : NULL;
4518 32547066 : if (entry == NULL)
4519 : {
4520 : /* Only cache up to constexpr_cache_depth to limit memory use. */
4521 9034676 : if (insert)
4522 : {
4523 : /* We need to keep a pointer to the entry, not just the slot, as
4524 : the slot can move during evaluation of the body. */
4525 4656083 : *slot = entry = ggc_alloc<constexpr_call> ();
4526 4656083 : *entry = new_call;
4527 4656083 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4528 4656083 : fb.preserve ();
4529 :
4530 : /* Unshare args going into the hash table to separate them
4531 : from the caller's context, for better GC and to avoid
4532 : problems with verify_gimple. */
4533 4656083 : tree bound = new_call.bindings;
4534 7514032 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4535 : {
4536 2857949 : tree &arg = TREE_VEC_ELT (bound, i);
4537 2857949 : arg = unshare_expr_without_location (arg);
4538 : }
4539 : }
4540 : }
4541 : /* Calls that are in progress have their result set to unknown_type_node,
4542 : so that we can detect circular dependencies. Now that we only cache
4543 : up to constexpr_cache_depth this won't catch circular dependencies that
4544 : start deeper, but they'll hit the recursion or ops limit. */
4545 27890983 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4546 : {
4547 6 : if (!ctx->quiet)
4548 0 : error ("call has circular dependency");
4549 6 : *non_constant_p = true;
4550 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4551 : }
4552 : else
4553 27890977 : result = entry->result (ctx->manifestly_const_eval);
4554 : }
4555 :
4556 109366433 : if (!depth_ok)
4557 : {
4558 33 : if (!ctx->quiet)
4559 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4560 : "%<-fconstexpr-depth=%> to increase the maximum)",
4561 : max_constexpr_depth);
4562 33 : *non_constant_p = true;
4563 33 : result = error_mark_node;
4564 : }
4565 : else
4566 : {
4567 109366433 : bool cacheable = !!entry;
4568 109366433 : if (result && result != error_mark_node)
4569 : /* OK */;
4570 86602775 : else if (!DECL_SAVED_TREE (fun))
4571 : {
4572 : /* When at_eof >= 3, cgraph has started throwing away
4573 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4574 : late code generation for VEC_INIT_EXPR, which needs to be
4575 : completely reconsidered. */
4576 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4577 0 : *non_constant_p = true;
4578 : }
4579 86602775 : else if (tree copy = get_fundef_copy (new_call.fundef))
4580 : {
4581 86602766 : tree body, parms, res;
4582 86602766 : releasing_vec ctors;
4583 :
4584 : /* Reuse or create a new unshared copy of this function's body. */
4585 86602766 : body = TREE_PURPOSE (copy);
4586 86602766 : parms = TREE_VALUE (copy);
4587 86602766 : res = TREE_TYPE (copy);
4588 :
4589 : /* Associate the bindings with the remapped parms. */
4590 86602766 : tree bound = new_call.bindings;
4591 86602766 : tree remapped = parms;
4592 211937237 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4593 : {
4594 125334471 : tree arg = TREE_VEC_ELT (bound, i);
4595 125334471 : if (entry)
4596 : {
4597 : /* Unshare again so the callee doesn't change the
4598 : argument values in the hash table. XXX Could we unshare
4599 : lazily in cxx_eval_store_expression? */
4600 3098070 : arg = unshare_constructor (arg);
4601 3098070 : if (TREE_CODE (arg) == CONSTRUCTOR)
4602 878550 : vec_safe_push (ctors, arg);
4603 : }
4604 125334471 : ctx->global->put_value (remapped, arg);
4605 125334471 : remapped = DECL_CHAIN (remapped);
4606 : }
4607 86602766 : if (remapped)
4608 : {
4609 : /* We shouldn't have any parms without args, but fail gracefully
4610 : in error recovery. */
4611 6 : gcc_checking_assert (seen_error ());
4612 6 : *non_constant_p = true;
4613 : }
4614 : /* Add the RESULT_DECL to the values map, too. */
4615 86602766 : gcc_assert (!DECL_BY_REFERENCE (res));
4616 86602766 : ctx->global->put_value (res, NULL_TREE);
4617 :
4618 : /* Remember the current call we're evaluating. */
4619 86602766 : constexpr_ctx call_ctx = *ctx;
4620 86602766 : call_ctx.call = &new_call;
4621 86602766 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4622 86602766 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4623 86602766 : bool save_state_dependent = ctx->global->state_dependent;
4624 :
4625 : /* Make sure we fold std::is_constant_evaluated to true in an
4626 : immediate function. */
4627 173205532 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4628 2766574 : call_ctx.manifestly_const_eval = mce_true;
4629 :
4630 : /* If this is a constexpr destructor, the object's const and volatile
4631 : semantics are no longer in effect; see [class.dtor]p5. */
4632 97767578 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4633 1252989 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4634 : non_constant_p, overflow_p, jump_target);
4635 :
4636 : /* If this is a constructor, we are beginning the lifetime of the
4637 : object we are initializing. */
4638 86602766 : if (new_obj
4639 22329624 : && DECL_CONSTRUCTOR_P (fun)
4640 9911823 : && TREE_CODE (new_obj) == COMPONENT_REF
4641 89435468 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4642 : {
4643 8212 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4644 8212 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4645 8212 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4646 : new_obj, ctor);
4647 8212 : cxx_eval_constant_expression (ctx, activate,
4648 : lval, non_constant_p, overflow_p,
4649 : jump_target);
4650 8212 : ggc_free (activate);
4651 8212 : if (*jump_target)
4652 0 : return NULL_TREE;
4653 : }
4654 :
4655 86602766 : ctx->global->state_dependent = false;
4656 :
4657 86602766 : tree jmp_target = NULL_TREE;
4658 86602766 : cxx_eval_constant_expression (&call_ctx, body,
4659 : vc_discard, non_constant_p, overflow_p,
4660 : &jmp_target);
4661 :
4662 86602762 : if (!*non_constant_p && throws (&jmp_target))
4663 : {
4664 3019 : result = NULL_TREE;
4665 3019 : cacheable = false;
4666 3019 : *jump_target = jmp_target;
4667 : }
4668 86599743 : else if (!*non_constant_p && TREE_THIS_VOLATILE (fun))
4669 : {
4670 : /* Return from a [[noreturn]] function. */
4671 11 : if (!ctx->quiet)
4672 5 : error ("%<[[noreturn]]%> call returns");
4673 11 : *non_constant_p = true;
4674 : }
4675 173199464 : else if (DECL_CONSTRUCTOR_P (fun))
4676 : /* This can be null for a subobject constructor call, in
4677 : which case what we care about is the initialization
4678 : side-effects rather than the value. We could get at the
4679 : value by evaluating *this, but we don't bother; there's
4680 : no need to put such a call in the hash table. */
4681 12370163 : result = lval ? ctx->object : ctx->ctor;
4682 74229569 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4683 7376676 : result = void_node;
4684 : else
4685 : {
4686 66852893 : result = ctx->global->get_value (res);
4687 10959187 : if (result == NULL_TREE && !*non_constant_p
4688 66853113 : && !DECL_DESTRUCTOR_P (fun))
4689 : {
4690 110 : if (!ctx->quiet)
4691 6 : error ("%<constexpr%> call flows off the end "
4692 : "of the function");
4693 110 : *non_constant_p = true;
4694 : }
4695 : }
4696 :
4697 86602762 : if (ctx->global->state_dependent)
4698 65830 : cacheable = false;
4699 86602762 : ctx->global->state_dependent |= save_state_dependent;
4700 :
4701 : /* At this point, the object's constructor will have run, so
4702 : the object is no longer under construction, and its possible
4703 : 'const' semantics now apply. Make a note of this fact by
4704 : marking the CONSTRUCTOR TREE_READONLY. */
4705 97767574 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4706 9911823 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4707 : non_constant_p, overflow_p, jump_target);
4708 :
4709 : /* Remove the parms/result from the values map. */
4710 86602762 : destroy_value_checked (ctx, res, non_constant_p);
4711 298539999 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4712 125334475 : destroy_value_checked (ctx, parm, non_constant_p);
4713 :
4714 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4715 87481312 : while (!ctors->is_empty ())
4716 : {
4717 878550 : tree c = ctors->pop ();
4718 878550 : if (c != result)
4719 878550 : free_constructor (c);
4720 : }
4721 :
4722 : /* Make the unshared function copy we used available for re-use. */
4723 86602762 : save_fundef_copy (fun, copy);
4724 :
4725 : /* If the call allocated some heap object that hasn't been
4726 : deallocated during the call, or if it deallocated some heap
4727 : object it has not allocated, the call isn't really stateless
4728 : for the constexpr evaluation and should not be cached.
4729 : It is fine if the call allocates something and deallocates it
4730 : too. */
4731 86602762 : if (cacheable
4732 96381541 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4733 9774430 : || (save_heap_dealloc_count
4734 9774430 : != ctx->global->heap_dealloc_count)))
4735 : {
4736 4349 : tree heap_var;
4737 4349 : unsigned int i;
4738 4349 : if ((ctx->global->heap_vars.length ()
4739 4349 : - ctx->global->heap_dealloc_count)
4740 4349 : != save_heap_alloc_count - save_heap_dealloc_count)
4741 : cacheable = false;
4742 : else
4743 138527 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4744 : save_heap_alloc_count)
4745 136030 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4746 : {
4747 : cacheable = false;
4748 : break;
4749 : }
4750 : }
4751 :
4752 : /* Rewrite all occurrences of the function's RESULT_DECL with the
4753 : current object under construction. */
4754 86602762 : if (!*non_constant_p
4755 71954091 : && ctx->object
4756 17203245 : && CLASS_TYPE_P (TREE_TYPE (res))
4757 89789903 : && !is_empty_class (TREE_TYPE (res)))
4758 : {
4759 2264797 : if (!same_type_ignoring_top_level_qualifiers_p
4760 2264797 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4761 0 : *non_constant_p = true;
4762 2264797 : else if (replace_decl (&result, res, ctx->object))
4763 : {
4764 1791 : cacheable = false;
4765 1791 : result = cxx_eval_constant_expression (ctx, result, lval,
4766 : non_constant_p,
4767 : overflow_p,
4768 : jump_target);
4769 1791 : if (*jump_target)
4770 : {
4771 0 : cacheable = false;
4772 0 : result = NULL_TREE;
4773 : }
4774 : }
4775 : }
4776 :
4777 : /* Only cache a permitted result of a constant expression. */
4778 86600971 : if (cacheable && !reduced_constant_expression_p (result))
4779 : cacheable = false;
4780 :
4781 : /* Only cache a result without contract violations. */
4782 81566702 : if (cacheable && ctx->global->contract_statement)
4783 81862384 : cacheable = false;
4784 86602762 : }
4785 : else
4786 : /* Couldn't get a function copy to evaluate. */
4787 9 : *non_constant_p = true;
4788 :
4789 109366429 : if (result == error_mark_node)
4790 6 : *non_constant_p = true;
4791 109366429 : if (*non_constant_p || *overflow_p)
4792 14648836 : result = error_mark_node;
4793 94717593 : else if (!result)
4794 3943295 : result = void_node;
4795 109366429 : if (entry)
4796 : {
4797 65094128 : entry->result (ctx->manifestly_const_eval)
4798 32547064 : = cacheable ? result : error_mark_node;
4799 :
4800 32547064 : if (result != error_mark_node
4801 27577248 : && ctx->manifestly_const_eval == mce_unknown)
4802 : {
4803 : /* Evaluation succeeded and was independent of whether we're in a
4804 : manifestly constant-evaluated context, so we can also reuse
4805 : this result when evaluating this call with a fixed context. */
4806 1921554 : if (!entry->result (mce_true))
4807 713300 : entry->result (mce_true) = entry->result (mce_unknown);
4808 1921554 : if (!entry->result (mce_false))
4809 680172 : entry->result (mce_false) = entry->result (mce_unknown);
4810 : }
4811 : }
4812 : }
4813 :
4814 : /* The result of a constexpr function must be completely initialized.
4815 :
4816 : However, in C++20, a constexpr constructor doesn't necessarily have
4817 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4818 : in order to detect reading an uninitialized object in constexpr instead
4819 : of value-initializing it. (reduced_constant_expression_p is expected to
4820 : take care of clearing the flag.) */
4821 109366462 : if (TREE_CODE (result) == CONSTRUCTOR
4822 109366462 : && (cxx_dialect < cxx20
4823 21412882 : || !DECL_CONSTRUCTOR_P (fun)))
4824 5249541 : clear_no_implicit_zero (result);
4825 :
4826 109366462 : pop_cx_call_context ();
4827 :
4828 : /* A virtual call with a zero-offset covariant return needs no thunk, so
4829 : constant evaluation can evaluate the final overrider directly. The
4830 : result then has the overrider's return type rather than the static type
4831 : of the call. Adjust after caching so a direct call can reuse the result
4832 : with its original type. */
4833 109366462 : if (!*non_constant_p
4834 94717749 : && !*overflow_p
4835 94717593 : && !*jump_target
4836 94714574 : && DECL_VIRTUAL_P (fun)
4837 26066 : && result != void_node
4838 2173 : && INDIRECT_TYPE_P (TREE_TYPE (t))
4839 109367471 : && !(same_type_ignoring_top_level_qualifiers_p
4840 1009 : (TREE_TYPE (result), TREE_TYPE (t))))
4841 11 : result = adjust_temp_type (TREE_TYPE (t), result);
4842 :
4843 109366462 : return result;
4844 184262709 : }
4845 :
4846 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4847 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4848 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4849 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4850 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4851 :
4852 : bool
4853 1235867467 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4854 : {
4855 1235867467 : if (t == NULL_TREE)
4856 : return false;
4857 :
4858 1230902855 : switch (TREE_CODE (t))
4859 : {
4860 : case PTRMEM_CST:
4861 : case REFLECT_EXPR:
4862 : /* Even if we can't lower this yet, it's constant. */
4863 : return true;
4864 :
4865 : case OMP_DECLARE_MAPPER:
4866 : return true;
4867 :
4868 63886423 : case CONSTRUCTOR:
4869 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4870 63886423 : tree field;
4871 63886423 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4872 : /* A constant vector would be folded to VECTOR_CST.
4873 : A CONSTRUCTOR of scalar type means uninitialized. */
4874 : return false;
4875 63869846 : if (CONSTRUCTOR_NO_CLEARING (t))
4876 : {
4877 3504828 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4878 : {
4879 : /* There must be a valid constant initializer at every array
4880 : index. */
4881 5822 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4882 5822 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4883 5822 : tree cursor = min;
4884 95775 : for (auto &e: CONSTRUCTOR_ELTS (t))
4885 : {
4886 78426 : if (!reduced_constant_expression_p (e.value))
4887 : return false;
4888 78369 : if (array_index_cmp (cursor, e.index) != 0)
4889 : return false;
4890 78309 : if (TREE_CODE (e.index) == RANGE_EXPR)
4891 0 : cursor = TREE_OPERAND (e.index, 1);
4892 78309 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4893 0 : cursor
4894 0 : = int_const_binop (PLUS_EXPR, cursor,
4895 0 : size_int (RAW_DATA_LENGTH (e.value)));
4896 : else
4897 78309 : cursor = int_const_binop (PLUS_EXPR, cursor,
4898 78309 : size_one_node);
4899 : }
4900 5705 : if (find_array_ctor_elt (t, max) == -1)
4901 : return false;
4902 5617 : goto ok;
4903 : }
4904 3499006 : else if (cxx_dialect >= cxx20
4905 3499006 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4906 : {
4907 : /* A union can have at most one active member. A union with no
4908 : active member has no constituent values, so all constituent
4909 : values are constant. */
4910 : field = NULL_TREE;
4911 : }
4912 : else
4913 3495430 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4914 : }
4915 : else
4916 : field = NULL_TREE;
4917 601915247 : for (auto &e: CONSTRUCTOR_ELTS (t))
4918 : {
4919 : /* If VAL is null, we're in the middle of initializing this
4920 : element. */
4921 470253712 : if (!reduced_constant_expression_p (e.value,
4922 470253712 : (e.index
4923 470253676 : && (TREE_CODE (e.index)
4924 : == FIELD_DECL))
4925 59341928 : ? DECL_SIZE (e.index)
4926 : : NULL_TREE))
4927 : return false;
4928 : /* We want to remove initializers for empty fields in a struct to
4929 : avoid confusing output_constructor. */
4930 469677822 : if (is_empty_field (e.index)
4931 469677822 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4932 : return false;
4933 : /* Check for non-empty fields between initialized fields when
4934 : CONSTRUCTOR_NO_CLEARING. */
4935 468556475 : for (; field && e.index != field;
4936 383936 : field = next_subobject_field (DECL_CHAIN (field)))
4937 388668 : if (!is_really_empty_class (TREE_TYPE (field),
4938 : /*ignore_vptr*/false))
4939 : return false;
4940 468167807 : if (field)
4941 3984655 : field = next_subobject_field (DECL_CHAIN (field));
4942 : }
4943 : /* There could be a non-empty field at the end. */
4944 62064994 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4945 293707 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4946 : {
4947 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4948 : the parent FIELD_DECL (if any) for classes with virtual
4949 : bases. */
4950 6832 : if (cxx_dialect >= cxx26
4951 4334 : && sz
4952 7352 : && tree_int_cst_le (sz, bit_position (field)))
4953 : break;
4954 : return false;
4955 : }
4956 61771287 : ok:
4957 61777296 : if (CONSTRUCTOR_NO_CLEARING (t))
4958 : /* All the fields are initialized. */
4959 3371959 : CONSTRUCTOR_NO_CLEARING (t) = false;
4960 : return true;
4961 :
4962 1166696770 : default:
4963 : /* FIXME are we calling this too much? */
4964 1166696770 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4965 : }
4966 : }
4967 :
4968 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4969 : why and suggest what could be done about it. */
4970 :
4971 : static tree
4972 1435 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4973 : {
4974 1435 : bool ref_p = false;
4975 :
4976 : /* No need to look into types or unevaluated operands. */
4977 1435 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4978 : {
4979 0 : *walk_subtrees = false;
4980 0 : return NULL_TREE;
4981 : }
4982 :
4983 1435 : switch (TREE_CODE (*tp))
4984 : {
4985 104 : CASE_CONVERT:
4986 104 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4987 : break;
4988 85 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4989 85 : *tp = TREE_OPERAND (*tp, 0);
4990 217 : gcc_fallthrough ();
4991 217 : case ADDR_EXPR:
4992 217 : {
4993 217 : tree op = TREE_OPERAND (*tp, 0);
4994 217 : if (VAR_P (op)
4995 116 : && DECL_DECLARED_CONSTEXPR_P (op)
4996 39 : && !TREE_STATIC (op)
4997 : /* ??? We should also say something about temporaries. */
4998 253 : && !DECL_ARTIFICIAL (op))
4999 : {
5000 33 : if (ref_p)
5001 12 : inform (location_of (*tp), "reference to %qD is not a constant "
5002 : "expression", op);
5003 : else
5004 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
5005 : "expression", op);
5006 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
5007 33 : rich_location richloc (line_table, op_loc);
5008 33 : richloc.add_fixit_insert_before (op_loc, "static ");
5009 33 : inform (&richloc,
5010 : "address of non-static constexpr variable %qD may differ on "
5011 : "each invocation of the enclosing function; add %<static%> "
5012 : "to give it a constant address", op);
5013 33 : }
5014 : break;
5015 : }
5016 : default:
5017 : break;
5018 : }
5019 :
5020 : return NULL_TREE;
5021 : }
5022 :
5023 : /* Some expressions may have constant operands but are not constant
5024 : themselves, such as 1/0. Call this function to check for that
5025 : condition.
5026 :
5027 : We only call this in places that require an arithmetic constant, not in
5028 : places where we might have a non-constant expression that can be a
5029 : component of a constant expression, such as the address of a constexpr
5030 : variable that might be dereferenced later. */
5031 :
5032 : static bool
5033 664120854 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
5034 : bool *overflow_p)
5035 : {
5036 651125177 : if (!*non_constant_p && !reduced_constant_expression_p (t)
5037 667056319 : && t != void_node)
5038 : {
5039 2935046 : if (!allow_non_constant)
5040 : {
5041 392 : auto_diagnostic_group d;
5042 517 : error_at (cp_expr_loc_or_input_loc (t),
5043 : "%q+E is not a constant expression", t);
5044 392 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
5045 : nullptr);
5046 392 : }
5047 2935046 : *non_constant_p = true;
5048 : }
5049 664120854 : if (TREE_OVERFLOW_P (t))
5050 : {
5051 678 : if (!allow_non_constant)
5052 : {
5053 155 : permerror (input_location, "overflow in constant expression");
5054 : /* If we're being permissive (and are in an enforcing
5055 : context), ignore the overflow. */
5056 155 : if (flag_permissive)
5057 75 : return *non_constant_p;
5058 : }
5059 603 : *overflow_p = true;
5060 : }
5061 664120779 : return *non_constant_p;
5062 : }
5063 :
5064 : /* Check whether the shift operation with code CODE and type TYPE on LHS
5065 : and RHS is undefined. If it is, give an error with an explanation,
5066 : and return true; return false otherwise. */
5067 :
5068 : static bool
5069 80155632 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
5070 : enum tree_code code, tree type, tree lhs, tree rhs)
5071 : {
5072 80155632 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
5073 5575263 : || TREE_CODE (lhs) != INTEGER_CST
5074 5574987 : || TREE_CODE (rhs) != INTEGER_CST)
5075 : return false;
5076 :
5077 5574987 : tree lhstype = TREE_TYPE (lhs);
5078 5574987 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
5079 :
5080 : /* [expr.shift] The behavior is undefined if the right operand
5081 : is negative, or greater than or equal to the length in bits
5082 : of the promoted left operand. */
5083 5574987 : if (tree_int_cst_sgn (rhs) == -1)
5084 : {
5085 126 : if (!ctx->quiet)
5086 35 : permerror (loc, "right operand of shift expression %q+E is negative",
5087 : build2_loc (loc, code, type, lhs, rhs));
5088 126 : return (!flag_permissive || ctx->quiet);
5089 : }
5090 5574861 : if (compare_tree_int (rhs, uprec) >= 0)
5091 : {
5092 200 : if (!ctx->quiet)
5093 34 : permerror (loc, "right operand of shift expression %q+E is greater "
5094 : "than or equal to the precision %wu of the left operand",
5095 : build2_loc (loc, code, type, lhs, rhs), uprec);
5096 200 : return (!flag_permissive || ctx->quiet);
5097 : }
5098 :
5099 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
5100 : if E1 has a signed type and non-negative value, and E1x2^E2 is
5101 : representable in the corresponding unsigned type of the result type,
5102 : then that value, converted to the result type, is the resulting value;
5103 : otherwise, the behavior is undefined.
5104 : For C++20:
5105 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
5106 : 2^N, where N is the range exponent of the type of the result. */
5107 5574661 : if (code == LSHIFT_EXPR
5108 4663627 : && !TYPE_OVERFLOW_WRAPS (lhstype)
5109 2196835 : && cxx_dialect >= cxx11
5110 7751319 : && cxx_dialect < cxx20)
5111 : {
5112 54036 : if (tree_int_cst_sgn (lhs) == -1)
5113 : {
5114 74 : if (!ctx->quiet)
5115 18 : permerror (loc,
5116 : "left operand of shift expression %q+E is negative",
5117 : build2_loc (loc, code, type, lhs, rhs));
5118 74 : return (!flag_permissive || ctx->quiet);
5119 : }
5120 : /* For signed x << y the following:
5121 : (unsigned) x >> ((prec (lhs) - 1) - y)
5122 : if > 1, is undefined. The right-hand side of this formula
5123 : is the highest bit of the LHS that can be set (starting from 0),
5124 : so that the shift doesn't overflow. We then right-shift the LHS
5125 : to see whether any other bit is set making the original shift
5126 : undefined -- the result is not representable in the corresponding
5127 : unsigned type. */
5128 53962 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
5129 53962 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
5130 53962 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
5131 53962 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
5132 53962 : if (tree_int_cst_lt (integer_one_node, t))
5133 : {
5134 41 : if (!ctx->quiet)
5135 7 : permerror (loc, "shift expression %q+E overflows",
5136 : build2_loc (loc, code, type, lhs, rhs));
5137 41 : return (!flag_permissive || ctx->quiet);
5138 : }
5139 : }
5140 : return false;
5141 : }
5142 :
5143 : /* Subroutine of cxx_eval_constant_expression.
5144 : Attempt to reduce the unary expression tree T to a compile time value.
5145 : If successful, return the value. Otherwise issue a diagnostic
5146 : and return error_mark_node. */
5147 :
5148 : static tree
5149 25754657 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
5150 : bool /*lval*/,
5151 : bool *non_constant_p, bool *overflow_p,
5152 : tree *jump_target)
5153 : {
5154 25754657 : tree r;
5155 25754657 : tree orig_arg = TREE_OPERAND (t, 0);
5156 25754657 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
5157 : non_constant_p, overflow_p,
5158 : jump_target);
5159 25754655 : if (*jump_target)
5160 : return NULL_TREE;
5161 25754653 : VERIFY_CONSTANT (arg);
5162 20505351 : location_t loc = EXPR_LOCATION (t);
5163 20505351 : enum tree_code code = TREE_CODE (t);
5164 20505351 : tree type = TREE_TYPE (t);
5165 20505351 : r = fold_unary_loc (loc, code, type, arg);
5166 20505351 : if (r == NULL_TREE)
5167 : {
5168 17 : if (arg == orig_arg)
5169 : r = t;
5170 : else
5171 13 : r = build1_loc (loc, code, type, arg);
5172 : }
5173 20505351 : VERIFY_CONSTANT (r);
5174 : return r;
5175 : }
5176 :
5177 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5178 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5179 : generic folding should be used. */
5180 :
5181 : static tree
5182 10819707 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5183 : tree lhs, tree rhs, bool *non_constant_p,
5184 : bool *overflow_p, tree *jump_target)
5185 : {
5186 10819707 : STRIP_NOPS (lhs);
5187 10819707 : if (TREE_CODE (lhs) != ADDR_EXPR)
5188 : return NULL_TREE;
5189 :
5190 9956034 : lhs = TREE_OPERAND (lhs, 0);
5191 :
5192 : /* &A[i] p+ j => &A[i + j] */
5193 9956034 : if (TREE_CODE (lhs) == ARRAY_REF
5194 208338 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5195 208338 : && TREE_CODE (rhs) == INTEGER_CST
5196 208338 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5197 10164372 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5198 : {
5199 208338 : tree orig_type = TREE_TYPE (t);
5200 208338 : location_t loc = EXPR_LOCATION (t);
5201 208338 : tree type = TREE_TYPE (lhs);
5202 :
5203 208338 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5204 208338 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5205 208338 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5206 : non_constant_p, overflow_p,
5207 : jump_target);
5208 208338 : if (*non_constant_p)
5209 : return NULL_TREE;
5210 208326 : if (*jump_target)
5211 : return NULL_TREE;
5212 : /* Don't fold an out-of-bound access. */
5213 208326 : if (!tree_int_cst_le (t, nelts))
5214 : return NULL_TREE;
5215 208326 : rhs = cp_fold_convert (ssizetype, rhs);
5216 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5217 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5218 208326 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5219 208326 : rhs, TYPE_SIZE_UNIT (type))))
5220 : return NULL_TREE;
5221 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5222 : as signed. */
5223 208294 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5224 208294 : TYPE_SIZE_UNIT (type));
5225 208294 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5226 208294 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5227 : t, NULL_TREE, NULL_TREE);
5228 208294 : t = cp_build_addr_expr (t, tf_warning_or_error);
5229 208294 : t = cp_fold_convert (orig_type, t);
5230 208294 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5231 : non_constant_p, overflow_p,
5232 208294 : jump_target);
5233 : }
5234 :
5235 : return NULL_TREE;
5236 : }
5237 :
5238 : /* Try to fold expressions like
5239 : (struct S *) (&a[0].D.2378 + 12)
5240 : into
5241 : &MEM <struct T> [(void *)&a + 12B]
5242 : This is something normally done by gimple_fold_stmt_to_constant_1
5243 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5244 : dereference the address because some details are lost.
5245 : For pointer comparisons we want such folding though so that
5246 : match.pd address_compare optimization works. */
5247 :
5248 : static tree
5249 11994482 : cxx_maybe_fold_addr_pointer_plus (tree t)
5250 : {
5251 23988970 : while (CONVERT_EXPR_P (t)
5252 12700955 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5253 706467 : t = TREE_OPERAND (t, 0);
5254 11994482 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5255 : return NULL_TREE;
5256 10302838 : tree op0 = TREE_OPERAND (t, 0);
5257 10302838 : tree op1 = TREE_OPERAND (t, 1);
5258 10302838 : if (TREE_CODE (op1) != INTEGER_CST)
5259 : return NULL_TREE;
5260 10302838 : while (CONVERT_EXPR_P (op0)
5261 20602155 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5262 10299317 : op0 = TREE_OPERAND (op0, 0);
5263 10302838 : if (TREE_CODE (op0) != ADDR_EXPR)
5264 : return NULL_TREE;
5265 10302838 : op1 = fold_convert (ptr_type_node, op1);
5266 10302838 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5267 10302838 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5268 : }
5269 :
5270 : /* Subroutine of cxx_eval_constant_expression.
5271 : Like cxx_eval_unary_expression, except for binary expressions. */
5272 :
5273 : static tree
5274 112157476 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5275 : value_cat lval,
5276 : bool *non_constant_p, bool *overflow_p,
5277 : tree *jump_target)
5278 : {
5279 112157476 : tree r = NULL_TREE;
5280 112157476 : tree orig_lhs = TREE_OPERAND (t, 0);
5281 112157476 : tree orig_rhs = TREE_OPERAND (t, 1);
5282 112157476 : tree lhs, rhs;
5283 112157476 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5284 : non_constant_p, overflow_p,
5285 : jump_target);
5286 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5287 : subtraction. */
5288 112157474 : if (*non_constant_p)
5289 : return t;
5290 90296909 : if (*jump_target)
5291 : return NULL_TREE;
5292 90296814 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5293 : non_constant_p, overflow_p,
5294 : jump_target);
5295 90296814 : if (*non_constant_p)
5296 : return t;
5297 89132587 : if (*jump_target)
5298 : return NULL_TREE;
5299 :
5300 89132581 : location_t loc = EXPR_LOCATION (t);
5301 89132581 : enum tree_code code = TREE_CODE (t);
5302 89132581 : tree type = TREE_TYPE (t);
5303 :
5304 89132581 : if (code == EQ_EXPR || code == NE_EXPR)
5305 : {
5306 18207966 : bool is_code_eq = (code == EQ_EXPR);
5307 :
5308 18207966 : if (TREE_CODE (lhs) == PTRMEM_CST
5309 217 : && TREE_CODE (rhs) == PTRMEM_CST)
5310 : {
5311 76 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5312 76 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5313 76 : bool eq;
5314 76 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5315 76 : && TREE_CODE (lmem) == FIELD_DECL
5316 76 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5317 103 : && same_type_p (DECL_CONTEXT (lmem),
5318 : DECL_CONTEXT (rmem)))
5319 : /* If both refer to (possibly different) members of the same union
5320 : (12.3), they compare equal. */
5321 : eq = true;
5322 : else
5323 49 : eq = cp_tree_equal (lhs, rhs);
5324 76 : r = constant_boolean_node (eq == is_code_eq, type);
5325 76 : }
5326 18207890 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5327 18207749 : || TREE_CODE (rhs) == PTRMEM_CST)
5328 18207890 : && (null_member_pointer_value_p (lhs)
5329 141 : || null_member_pointer_value_p (rhs)))
5330 141 : r = constant_boolean_node (!is_code_eq, type);
5331 18207749 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5332 0 : lhs = cplus_expand_constant (lhs);
5333 18207749 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5334 0 : rhs = cplus_expand_constant (rhs);
5335 18207749 : else if (REFLECT_EXPR_P (lhs) && REFLECT_EXPR_P (rhs))
5336 : {
5337 3926 : const bool eq = compare_reflections (lhs, rhs);
5338 3926 : r = constant_boolean_node (eq == is_code_eq, type);
5339 : }
5340 : }
5341 0 : if (r == NULL_TREE
5342 89128438 : && TREE_CODE_CLASS (code) == tcc_comparison
5343 39050929 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5344 : {
5345 5997241 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5346 5003919 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5347 5997241 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5348 5298919 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5349 : }
5350 10819862 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5351 99952443 : && integer_zerop (lhs) && !integer_zerop (rhs))
5352 : {
5353 155 : if (!ctx->quiet)
5354 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5355 155 : *non_constant_p = true;
5356 155 : return t;
5357 : }
5358 89132426 : else if (code == POINTER_PLUS_EXPR)
5359 : {
5360 10819707 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5361 : overflow_p, jump_target);
5362 10819707 : if (*jump_target)
5363 : return NULL_TREE;
5364 : }
5365 78312719 : else if (code == SPACESHIP_EXPR)
5366 : {
5367 27261 : r = genericize_spaceship (loc, type, lhs, rhs);
5368 27261 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5369 27261 : overflow_p, jump_target);
5370 : }
5371 :
5372 89105165 : if (r == NULL_TREE)
5373 : {
5374 88892728 : if (ctx->manifestly_const_eval == mce_true
5375 67926397 : && (flag_constexpr_fp_except
5376 67926394 : || TREE_CODE (type) != REAL_TYPE))
5377 : {
5378 67872739 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5379 67872739 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5380 67872739 : }
5381 : else
5382 21019989 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5383 : }
5384 :
5385 88892728 : if (r == NULL_TREE
5386 8949631 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5387 100 : && TREE_CODE (lhs) == INTEGER_CST
5388 98 : && TREE_CODE (rhs) == INTEGER_CST
5389 98054796 : && wi::neg_p (wi::to_wide (rhs)))
5390 : {
5391 : /* For diagnostics and -fpermissive emulate previous behavior of
5392 : handling shifts by negative amount. */
5393 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5394 98 : if (nrhs)
5395 119 : r = fold_binary_loc (loc,
5396 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5397 : type, lhs, nrhs);
5398 : }
5399 :
5400 89105165 : if (r == NULL_TREE)
5401 : {
5402 8949533 : if (lhs == orig_lhs && rhs == orig_rhs)
5403 : r = t;
5404 : else
5405 3143675 : r = build2_loc (loc, code, type, lhs, rhs);
5406 : }
5407 80155632 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5408 433 : *non_constant_p = true;
5409 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5410 : a local array in a constexpr function. */
5411 89105165 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5412 71601102 : if (!ptr)
5413 71601102 : VERIFY_CONSTANT (r);
5414 : return r;
5415 : }
5416 :
5417 : /* Subroutine of cxx_eval_constant_expression.
5418 : Attempt to evaluate condition expressions. */
5419 :
5420 : static tree
5421 39226918 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5422 : value_cat lval,
5423 : bool *non_constant_p, bool *overflow_p,
5424 : tree *jump_target)
5425 : {
5426 39226918 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5427 : vc_prvalue,
5428 : non_constant_p, overflow_p,
5429 : jump_target);
5430 39226918 : if (*jump_target)
5431 : return NULL_TREE;
5432 39226894 : VERIFY_CONSTANT (val);
5433 36923451 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5434 : {
5435 : /* Evaluate the condition as if it was
5436 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5437 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5438 : without manifestly_const_eval even expressions or parts thereof which
5439 : will later be manifestly const_eval evaluated), otherwise fold it to
5440 : true. */
5441 1065700 : if (ctx->manifestly_const_eval == mce_unknown)
5442 : {
5443 961332 : *non_constant_p = true;
5444 961332 : return t;
5445 : }
5446 104368 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5447 : boolean_type_node);
5448 : }
5449 : /* Don't VERIFY_CONSTANT the other operands. */
5450 35962119 : const bool zero_p = integer_zerop (val);
5451 35962119 : if (zero_p)
5452 22855213 : val = TREE_OPERAND (t, 2);
5453 : else
5454 13106906 : val = TREE_OPERAND (t, 1);
5455 35962119 : if (TREE_CODE (t) == IF_STMT && !val)
5456 9912023 : val = void_node;
5457 :
5458 : /* P2564: If we aren't in immediate function context (including a manifestly
5459 : constant-evaluated expression), check any uses of immediate functions in
5460 : the arm we're discarding. But don't do this inside a call; we already
5461 : checked when parsing the function. */
5462 35962119 : if (ctx->manifestly_const_eval != mce_true
5463 5074010 : && !in_immediate_context ()
5464 4976544 : && !ctx->call
5465 36769894 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5466 807775 : ctx->manifestly_const_eval))
5467 : {
5468 7 : *non_constant_p = true;
5469 7 : return t;
5470 : }
5471 :
5472 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5473 : serve as the initializer for the same object as the outer TARGET_EXPR,
5474 : as in
5475 : A a = true ? A{} : A{};
5476 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5477 35962112 : if (TREE_CODE (val) == TARGET_EXPR)
5478 5336 : val = TARGET_EXPR_INITIAL (val);
5479 35962112 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5480 35962112 : overflow_p, jump_target);
5481 : }
5482 :
5483 : /* Subroutine of cxx_eval_constant_expression.
5484 : Attempt to evaluate vector condition expressions. Unlike
5485 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5486 : ternary arithmetics operation, where all 3 arguments have to be
5487 : evaluated as constants and then folding computes the result from
5488 : them. */
5489 :
5490 : static tree
5491 11755 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5492 : bool *non_constant_p, bool *overflow_p,
5493 : tree *jump_target)
5494 : {
5495 11755 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5496 : vc_prvalue,
5497 : non_constant_p, overflow_p,
5498 : jump_target);
5499 11755 : if (*jump_target)
5500 : return NULL_TREE;
5501 11755 : VERIFY_CONSTANT (arg1);
5502 8227 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5503 : vc_prvalue,
5504 : non_constant_p, overflow_p,
5505 : jump_target);
5506 8227 : if (*jump_target)
5507 : return NULL_TREE;
5508 8227 : VERIFY_CONSTANT (arg2);
5509 7097 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5510 : vc_prvalue,
5511 : non_constant_p, overflow_p,
5512 : jump_target);
5513 7097 : if (*jump_target)
5514 : return NULL_TREE;
5515 7097 : VERIFY_CONSTANT (arg3);
5516 7097 : location_t loc = EXPR_LOCATION (t);
5517 7097 : tree type = TREE_TYPE (t);
5518 7097 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5519 7097 : if (r == NULL_TREE)
5520 : {
5521 0 : if (arg1 == TREE_OPERAND (t, 0)
5522 0 : && arg2 == TREE_OPERAND (t, 1)
5523 0 : && arg3 == TREE_OPERAND (t, 2))
5524 : r = t;
5525 : else
5526 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5527 : }
5528 7097 : VERIFY_CONSTANT (r);
5529 : return r;
5530 : }
5531 :
5532 : /* Returns less than, equal to, or greater than zero if KEY is found to be
5533 : less than, to match, or to be greater than the constructor_elt's INDEX. */
5534 :
5535 : static int
5536 439349 : array_index_cmp (tree key, tree index)
5537 : {
5538 439349 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5539 :
5540 439349 : switch (TREE_CODE (index))
5541 : {
5542 436444 : case INTEGER_CST:
5543 436444 : return tree_int_cst_compare (key, index);
5544 2905 : case RANGE_EXPR:
5545 2905 : {
5546 2905 : tree lo = TREE_OPERAND (index, 0);
5547 2905 : tree hi = TREE_OPERAND (index, 1);
5548 2905 : if (tree_int_cst_lt (key, lo))
5549 : return -1;
5550 2615 : else if (tree_int_cst_lt (hi, key))
5551 : return 1;
5552 : else
5553 2615 : return 0;
5554 : }
5555 0 : default:
5556 0 : gcc_unreachable ();
5557 : }
5558 : }
5559 :
5560 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5561 : relative index OFF. */
5562 :
5563 : static tree
5564 29559 : raw_data_cst_elt (tree raw_data, unsigned int off)
5565 : {
5566 29559 : return build_int_cst (TREE_TYPE (raw_data),
5567 29559 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5568 59118 : ? (HOST_WIDE_INT)
5569 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
5570 : : (HOST_WIDE_INT)
5571 486 : RAW_DATA_SCHAR_ELT (raw_data, off));
5572 : }
5573 :
5574 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5575 : if none. If INSERT is true, insert a matching element rather than fail. */
5576 :
5577 : static HOST_WIDE_INT
5578 12421947 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5579 : {
5580 12421947 : if (tree_int_cst_sgn (dindex) < 0)
5581 : return -1;
5582 :
5583 12421947 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5584 12421947 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5585 12421947 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5586 :
5587 16691412 : unsigned HOST_WIDE_INT end = len;
5588 16691412 : unsigned HOST_WIDE_INT begin = 0;
5589 :
5590 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5591 : that the same is true of the other elements and index directly. */
5592 11866100 : if (end > 0)
5593 : {
5594 11637150 : tree cindex = (*elts)[end - 1].index;
5595 11637150 : if (cindex == NULL_TREE)
5596 : {
5597 : /* Verify that if the last index is missing, all indexes
5598 : are missing and there is no RAW_DATA_CST. */
5599 21720 : if (flag_checking)
5600 219837 : for (unsigned int j = 0; j < len - 1; ++j)
5601 198117 : gcc_assert ((*elts)[j].index == NULL_TREE
5602 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5603 21720 : if (i < end)
5604 21720 : return i;
5605 : else
5606 : {
5607 0 : begin = end;
5608 0 : if (i == end)
5609 : /* If the element is to be added right at the end,
5610 : make sure it is added with cleared index too. */
5611 4825312 : dindex = NULL_TREE;
5612 0 : else if (insert)
5613 : /* Otherwise, in order not to break the assumption
5614 : that CONSTRUCTOR either has all indexes or none,
5615 : we need to add indexes to all elements. */
5616 0 : for (unsigned int j = 0; j < len; ++j)
5617 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5618 : }
5619 : }
5620 11615430 : else if (TREE_CODE (cindex) == INTEGER_CST
5621 11615430 : && compare_tree_int (cindex, end - 1) == 0)
5622 : {
5623 11405026 : tree value = (*elts)[end - 1].value;
5624 11405026 : if (i < end)
5625 : {
5626 7575005 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5627 : begin = end - 1;
5628 : else
5629 7574915 : return i;
5630 : }
5631 3830021 : else if (TREE_CODE (value) == RAW_DATA_CST
5632 3862177 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5633 32156 : + RAW_DATA_LENGTH (value)))
5634 16078 : begin = end - 1;
5635 : else
5636 3813943 : begin = end;
5637 : }
5638 : }
5639 :
5640 : /* Otherwise, find a matching index by means of a binary search. */
5641 5030022 : while (begin != end)
5642 : {
5643 360977 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5644 360977 : constructor_elt &elt = (*elts)[middle];
5645 360977 : tree idx = elt.index;
5646 :
5647 360977 : int cmp = array_index_cmp (dindex, idx);
5648 360977 : if (cmp > 0
5649 66724 : && TREE_CODE (elt.value) == RAW_DATA_CST
5650 444785 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5651 83808 : + RAW_DATA_LENGTH (elt.value)))
5652 29417 : cmp = 0;
5653 360977 : if (cmp < 0)
5654 : end = middle;
5655 193574 : else if (cmp > 0)
5656 37307 : begin = middle + 1;
5657 : else
5658 : {
5659 156267 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5660 : {
5661 : /* We need to split the RAW_DATA_CST elt. */
5662 122 : constructor_elt e;
5663 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5664 244 : unsigned int off = (wi::to_offset (dindex)
5665 244 : - wi::to_offset (idx)).to_uhwi ();
5666 122 : tree value = elt.value;
5667 122 : unsigned int len = RAW_DATA_LENGTH (value);
5668 122 : if (off > 1 && len >= off + 3)
5669 22 : value = copy_node (elt.value);
5670 122 : if (off)
5671 : {
5672 33 : if (off > 1)
5673 33 : RAW_DATA_LENGTH (elt.value) = off;
5674 : else
5675 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5676 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5677 : build_int_cst (TREE_TYPE (elt.index),
5678 : off));
5679 33 : e.value = NULL_TREE;
5680 33 : ++middle;
5681 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5682 : }
5683 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5684 122 : if (len >= off + 2)
5685 : {
5686 111 : e.index = (*elts)[middle].index;
5687 111 : e.index = size_binop (PLUS_EXPR, e.index,
5688 : build_one_cst (TREE_TYPE (e.index)));
5689 111 : if (len >= off + 3)
5690 : {
5691 111 : RAW_DATA_LENGTH (value) -= off + 1;
5692 111 : RAW_DATA_POINTER (value) += off + 1;
5693 111 : e.value = value;
5694 : }
5695 : else
5696 0 : e.value = raw_data_cst_elt (value, off + 1);
5697 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5698 : }
5699 122 : return middle;
5700 : }
5701 35779 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5702 : {
5703 : /* We need to split the range. */
5704 349 : constructor_elt e;
5705 349 : tree lo = TREE_OPERAND (idx, 0);
5706 349 : tree hi = TREE_OPERAND (idx, 1);
5707 349 : tree value = elt.value;
5708 349 : dindex = fold_convert (sizetype, dindex);
5709 349 : if (tree_int_cst_lt (lo, dindex))
5710 : {
5711 : /* There are still some lower elts; shorten the range. */
5712 170 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5713 85 : size_one_node);
5714 85 : if (tree_int_cst_equal (lo, new_hi))
5715 : /* Only one element left, no longer a range. */
5716 33 : elt.index = lo;
5717 : else
5718 52 : TREE_OPERAND (idx, 1) = new_hi;
5719 : /* Append the element we want to insert. */
5720 85 : ++middle;
5721 85 : e.index = dindex;
5722 85 : e.value = unshare_constructor (value);
5723 85 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5724 : }
5725 : else
5726 : /* No lower elts, the range elt is now ours. */
5727 264 : elt.index = dindex;
5728 :
5729 349 : if (tree_int_cst_lt (dindex, hi))
5730 : {
5731 : /* There are still some higher elts; append a range. */
5732 482 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5733 241 : size_one_node);
5734 241 : if (tree_int_cst_equal (new_lo, hi))
5735 : e.index = hi;
5736 : else
5737 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5738 241 : e.value = unshare_constructor (value);
5739 241 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5740 : }
5741 : }
5742 156145 : return middle;
5743 : }
5744 : }
5745 :
5746 4669045 : if (insert)
5747 : {
5748 4539859 : constructor_elt e = { dindex, NULL_TREE };
5749 4539859 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5750 4539859 : return end;
5751 : }
5752 :
5753 : return -1;
5754 : }
5755 :
5756 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5757 : matching constructor_elt exists, then add one to CTOR.
5758 :
5759 : As an optimization, if POS_HINT is non-negative then it is used as a guess
5760 : for the (integer) index of the matching constructor_elt within CTOR.
5761 :
5762 : If POS_HINT is -2, it means do not insert. */
5763 :
5764 : static constructor_elt *
5765 42627997 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5766 : {
5767 : /* Check the hint first. */
5768 4624649 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5769 47252646 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5770 : return CONSTRUCTOR_ELT (ctor, pos_hint);
5771 :
5772 38003436 : bool insertp = (pos_hint != -2);
5773 38003436 : tree type = TREE_TYPE (ctor);
5774 38003436 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5775 : {
5776 27296 : gcc_assert (insertp);
5777 27296 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5778 27296 : return &CONSTRUCTOR_ELTS (ctor)->last();
5779 : }
5780 37976140 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5781 : {
5782 6236476 : if (TREE_CODE (index) == RANGE_EXPR)
5783 : {
5784 : /* Support for RANGE_EXPR index lookups is currently limited to
5785 : accessing an existing element via POS_HINT, or appending a new
5786 : element to the end of CTOR. ??? Support for other access
5787 : patterns may also be needed. */
5788 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5789 3 : if (vec_safe_length (elts))
5790 : {
5791 3 : tree lo = TREE_OPERAND (index, 0);
5792 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5793 : }
5794 3 : gcc_assert (insertp);
5795 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5796 3 : return &elts->last();
5797 : }
5798 :
5799 6236473 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5800 6236473 : if (i < 0)
5801 : {
5802 1806 : gcc_assert (!insertp);
5803 : return nullptr;
5804 : }
5805 6234667 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5806 6234667 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5807 : {
5808 : /* Split a range even if we aren't inserting new entries. */
5809 0 : gcc_assert (!insertp);
5810 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5811 0 : gcc_assert (i >= 0);
5812 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5813 : }
5814 6234667 : gcc_assert (cep->index == NULL_TREE
5815 : || TREE_CODE (cep->index) != RANGE_EXPR);
5816 : return cep;
5817 : }
5818 : else
5819 : {
5820 31739664 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5821 : && (same_type_ignoring_top_level_qualifiers_p
5822 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5823 :
5824 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5825 : Usually we meet initializers in that order, but it is
5826 : possible for base types to be placed not in program
5827 : order. */
5828 31739664 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5829 31739664 : unsigned HOST_WIDE_INT idx = 0;
5830 31739664 : constructor_elt *cep = NULL;
5831 :
5832 : /* Check if we're changing the active member of a union. */
5833 580489 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5834 32116765 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5835 12410 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5836 : /* If the bit offset of INDEX is larger than that of the last
5837 : constructor_elt, then we can just immediately append a new
5838 : constructor_elt to the end of CTOR. */
5839 48348651 : else if (CONSTRUCTOR_NELTS (ctor)
5840 44520599 : && tree_int_cst_compare (bit_position (index),
5841 43554414 : bit_position (CONSTRUCTOR_ELTS (ctor)
5842 21777207 : ->last().index)) > 0)
5843 : {
5844 6134405 : idx = CONSTRUCTOR_NELTS (ctor);
5845 6134405 : goto insert;
5846 : }
5847 :
5848 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5849 : appropriately. */
5850 :
5851 30399932 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5852 4794673 : idx++, fields = DECL_CHAIN (fields))
5853 : {
5854 20437475 : if (index == cep->index)
5855 15601699 : goto found;
5856 :
5857 : /* The field we're initializing must be on the field
5858 : list. Look to see if it is present before the
5859 : field the current ELT initializes. */
5860 51046145 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5861 46251472 : if (index == fields)
5862 41103 : goto insert;
5863 : }
5864 : /* We fell off the end of the CONSTRUCTOR, so insert a new
5865 : entry at the end. */
5866 :
5867 16137965 : insert:
5868 16137965 : if (!insertp)
5869 : return nullptr;
5870 :
5871 16137962 : {
5872 16137962 : constructor_elt ce = { index, NULL_TREE };
5873 :
5874 16137962 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5875 16137962 : cep = CONSTRUCTOR_ELT (ctor, idx);
5876 : }
5877 42627997 : found:;
5878 :
5879 : return cep;
5880 : }
5881 : }
5882 :
5883 : /* Under the control of CTX, issue a detailed diagnostic for
5884 : an out-of-bounds subscript INDEX into the expression ARRAY. */
5885 :
5886 : static void
5887 589 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5888 : {
5889 589 : if (!ctx->quiet)
5890 : {
5891 117 : tree arraytype = TREE_TYPE (array);
5892 :
5893 : /* Convert the unsigned array subscript to a signed integer to avoid
5894 : printing huge numbers for small negative values. */
5895 117 : tree sidx = fold_convert (ssizetype, index);
5896 117 : STRIP_ANY_LOCATION_WRAPPER (array);
5897 117 : if (DECL_P (array))
5898 : {
5899 76 : auto_diagnostic_group d;
5900 76 : if (TYPE_DOMAIN (arraytype))
5901 70 : error_at (loc, "array subscript value %qE is outside the bounds "
5902 : "of array %qD of type %qT", sidx, array, arraytype);
5903 : else
5904 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5905 : "type %qT with unknown bounds", sidx, array, arraytype);
5906 76 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5907 76 : }
5908 41 : else if (TYPE_DOMAIN (arraytype))
5909 38 : error_at (loc, "array subscript value %qE is outside the bounds "
5910 : "of array type %qT", sidx, arraytype);
5911 : else
5912 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5913 : "with unknown bounds", sidx, arraytype);
5914 : }
5915 589 : }
5916 :
5917 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5918 : a VECTOR_TYPE). */
5919 :
5920 : static tree
5921 20964055 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5922 : bool *non_constant_p, bool *overflow_p,
5923 : tree *jump_target)
5924 : {
5925 20964055 : tree nelts;
5926 20964055 : if (TREE_CODE (type) == ARRAY_TYPE)
5927 : {
5928 20964055 : if (TYPE_DOMAIN (type))
5929 20963795 : nelts = array_type_nelts_top (type);
5930 : else
5931 260 : nelts = size_zero_node;
5932 : }
5933 0 : else if (VECTOR_TYPE_P (type))
5934 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5935 : else
5936 0 : gcc_unreachable ();
5937 :
5938 : /* For VLAs, the number of elements won't be an integer constant. */
5939 20964055 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5940 : non_constant_p, overflow_p,
5941 : jump_target);
5942 20964055 : return nelts;
5943 : }
5944 :
5945 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5946 : STRING_CST STRING. */
5947 :
5948 : static tree
5949 1967286 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5950 : {
5951 1967286 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5952 1967286 : tree r;
5953 :
5954 1967286 : if (chars_per_elt == 1)
5955 1752795 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5956 : else
5957 : {
5958 214491 : const unsigned char *ptr
5959 214491 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5960 214491 : + index * chars_per_elt);
5961 214491 : r = native_interpret_expr (type, ptr, chars_per_elt);
5962 : }
5963 1967286 : return r;
5964 : }
5965 :
5966 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5967 : subscript, diagnose any problems with it, and return the result. */
5968 :
5969 : static tree
5970 21164452 : eval_and_check_array_index (const constexpr_ctx *ctx,
5971 : tree t, bool allow_one_past,
5972 : bool *non_constant_p, bool *overflow_p,
5973 : tree *jump_target)
5974 : {
5975 21164452 : location_t loc = cp_expr_loc_or_input_loc (t);
5976 21164452 : tree ary = TREE_OPERAND (t, 0);
5977 21164452 : t = TREE_OPERAND (t, 1);
5978 21164452 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5979 : non_constant_p, overflow_p,
5980 : jump_target);
5981 21164452 : if (*jump_target)
5982 : return NULL_TREE;
5983 21164452 : VERIFY_CONSTANT (index);
5984 :
5985 20963263 : if (!tree_fits_shwi_p (index)
5986 20963263 : || tree_int_cst_sgn (index) < 0)
5987 : {
5988 112 : diag_array_subscript (loc, ctx, ary, index);
5989 112 : *non_constant_p = true;
5990 112 : return t;
5991 : }
5992 :
5993 20963151 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5994 : overflow_p, jump_target);
5995 20963151 : if (*jump_target)
5996 : return NULL_TREE;
5997 20963151 : VERIFY_CONSTANT (nelts);
5998 20963088 : if (allow_one_past
5999 20963088 : ? !tree_int_cst_le (index, nelts)
6000 14167886 : : !tree_int_cst_lt (index, nelts))
6001 : {
6002 477 : diag_array_subscript (loc, ctx, ary, index);
6003 477 : *non_constant_p = true;
6004 477 : return t;
6005 : }
6006 :
6007 : return index;
6008 : }
6009 :
6010 : /* Subroutine of cxx_eval_constant_expression.
6011 : Attempt to reduce a reference to an array slot. */
6012 :
6013 : static tree
6014 15518954 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
6015 : value_cat lval,
6016 : bool *non_constant_p, bool *overflow_p,
6017 : tree *jump_target)
6018 : {
6019 15518954 : tree oldary = TREE_OPERAND (t, 0);
6020 15518954 : tree ary = cxx_eval_constant_expression (ctx, oldary,
6021 : lval,
6022 : non_constant_p, overflow_p,
6023 : jump_target);
6024 15518954 : if (*non_constant_p)
6025 : return t;
6026 15276847 : if (*jump_target)
6027 : return NULL_TREE;
6028 15276847 : if (!lval
6029 8480237 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
6030 144212 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
6031 15421059 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
6032 144212 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
6033 144212 : ary = TREE_OPERAND (ary, 0);
6034 :
6035 15276847 : tree oldidx = TREE_OPERAND (t, 1);
6036 15276847 : tree index = eval_and_check_array_index (ctx, t, lval,
6037 : non_constant_p, overflow_p,
6038 : jump_target);
6039 15276847 : if (*non_constant_p)
6040 : return t;
6041 15075075 : if (*jump_target)
6042 : return NULL_TREE;
6043 :
6044 15075075 : if (lval && ary == oldary && index == oldidx)
6045 : return t;
6046 10045041 : else if (lval == vc_discard)
6047 : return t;
6048 10045041 : else if (lval)
6049 1764914 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
6050 :
6051 8280127 : unsigned len = 0, elem_nchars = 1;
6052 8280127 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
6053 8280127 : if (TREE_CODE (ary) == CONSTRUCTOR)
6054 6179769 : len = CONSTRUCTOR_NELTS (ary);
6055 2100358 : else if (TREE_CODE (ary) == STRING_CST)
6056 : {
6057 1956160 : elem_nchars = (TYPE_PRECISION (elem_type)
6058 1956160 : / TYPE_PRECISION (char_type_node));
6059 1956160 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
6060 : }
6061 144198 : else if (TREE_CODE (ary) == VECTOR_CST)
6062 : /* We don't create variable-length VECTOR_CSTs. */
6063 144198 : len = VECTOR_CST_NELTS (ary).to_constant ();
6064 : else
6065 : {
6066 : /* We can't do anything with other tree codes, so use
6067 : VERIFY_CONSTANT to complain and fail. */
6068 0 : VERIFY_CONSTANT (ary);
6069 0 : gcc_unreachable ();
6070 : }
6071 :
6072 8280127 : bool found;
6073 8280127 : HOST_WIDE_INT i = 0;
6074 8280127 : if (TREE_CODE (ary) == CONSTRUCTOR)
6075 : {
6076 6179769 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
6077 6179769 : found = (ix >= 0);
6078 6179769 : if (found)
6079 6052477 : i = ix;
6080 : }
6081 : else
6082 : {
6083 2100358 : i = tree_to_shwi (index);
6084 2100358 : found = (i < len);
6085 : }
6086 :
6087 8280127 : if (found)
6088 : {
6089 8152384 : tree r;
6090 8152384 : if (TREE_CODE (ary) == CONSTRUCTOR)
6091 : {
6092 6052477 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
6093 6052477 : if (TREE_CODE (r) == RAW_DATA_CST)
6094 : {
6095 29437 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
6096 29437 : gcc_checking_assert (ridx);
6097 29437 : unsigned int off
6098 29437 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
6099 29437 : r = raw_data_cst_elt (r, off);
6100 : }
6101 : }
6102 2099907 : else if (TREE_CODE (ary) == VECTOR_CST)
6103 144198 : r = VECTOR_CST_ELT (ary, i);
6104 : else
6105 1955709 : r = extract_string_elt (ary, elem_nchars, i);
6106 :
6107 2129344 : if (r)
6108 : /* Don't VERIFY_CONSTANT here. */
6109 : return r;
6110 :
6111 : /* Otherwise the element doesn't have a value yet. */
6112 : }
6113 :
6114 : /* Not found. */
6115 :
6116 127743 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
6117 90 : return build_constructor (elem_type, NULL);
6118 :
6119 127653 : if (TREE_CODE (ary) == CONSTRUCTOR
6120 127653 : && CONSTRUCTOR_NO_CLEARING (ary))
6121 : {
6122 : /* 'ary' is part of the aggregate initializer we're currently
6123 : building; if there's no initializer for this element yet,
6124 : that's an error. */
6125 51 : if (!ctx->quiet)
6126 16 : error ("accessing uninitialized array element");
6127 51 : *non_constant_p = true;
6128 51 : return t;
6129 : }
6130 :
6131 : /* If it's within the array bounds but doesn't have an explicit
6132 : initializer, it's initialized from {}. But use build_value_init
6133 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
6134 127602 : tree val;
6135 127602 : constexpr_ctx new_ctx;
6136 127602 : if (CP_AGGREGATE_TYPE_P (elem_type))
6137 : {
6138 500 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
6139 500 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
6140 : }
6141 : else
6142 127102 : val = build_value_init (elem_type, tf_warning_or_error);
6143 :
6144 : /* Create a new constructor only if we don't already have a suitable one. */
6145 127602 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
6146 128134 : && (!ctx->ctor
6147 45 : || !same_type_ignoring_top_level_qualifiers_p
6148 45 : (elem_type, TREE_TYPE (ctx->ctor))));
6149 : if (new_ctor)
6150 : {
6151 523 : new_ctx = *ctx;
6152 : /* We clear the object here. We used to replace it with T, but that
6153 : caused problems (101371, 108158); and anyway, T is the initializer,
6154 : not the target object. */
6155 523 : new_ctx.object = NULL_TREE;
6156 523 : new_ctx.ctor = build_constructor (elem_type, NULL);
6157 523 : ctx = &new_ctx;
6158 : }
6159 127602 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6160 : overflow_p, jump_target);
6161 127602 : if (new_ctor && t != ctx->ctor)
6162 502 : free_constructor (ctx->ctor);
6163 : return t;
6164 : }
6165 :
6166 : /* Subroutine of cxx_eval_constant_expression.
6167 : Attempt to reduce a field access of a value of class type. */
6168 :
6169 : static tree
6170 115800318 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
6171 : value_cat lval,
6172 : bool *non_constant_p, bool *overflow_p,
6173 : tree *jump_target)
6174 : {
6175 115800318 : unsigned HOST_WIDE_INT i;
6176 115800318 : tree field;
6177 115800318 : tree value;
6178 115800318 : tree part = TREE_OPERAND (t, 1);
6179 115800318 : tree orig_whole = TREE_OPERAND (t, 0);
6180 115800318 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6181 : lval,
6182 : non_constant_p, overflow_p,
6183 : jump_target);
6184 115800318 : if (*non_constant_p)
6185 : return t;
6186 89791526 : if (*jump_target)
6187 : return NULL_TREE;
6188 89791520 : if (INDIRECT_REF_P (whole)
6189 89791520 : && integer_zerop (TREE_OPERAND (whole, 0)))
6190 : {
6191 177 : if (!ctx->quiet)
6192 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6193 177 : *non_constant_p = true;
6194 177 : return t;
6195 : }
6196 :
6197 89791343 : if (TREE_CODE (whole) == PTRMEM_CST)
6198 1651 : whole = cplus_expand_constant (whole);
6199 89791343 : if (whole == orig_whole)
6200 : return t;
6201 60757180 : if (lval == vc_discard)
6202 : return t;
6203 60757156 : if (lval)
6204 21576572 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6205 : whole, part, NULL_TREE);
6206 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6207 : CONSTRUCTOR. */
6208 39180584 : if (TREE_CODE (whole) != CONSTRUCTOR)
6209 : {
6210 0 : if (!ctx->quiet)
6211 0 : error ("%qE is not a constant expression", orig_whole);
6212 0 : *non_constant_p = true;
6213 0 : return t;
6214 : }
6215 39179090 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6216 39182260 : && DECL_MUTABLE_P (part))
6217 : {
6218 144 : if (!ctx->quiet)
6219 16 : error ("mutable %qD is not usable in a constant expression", part);
6220 144 : *non_constant_p = true;
6221 144 : return t;
6222 : }
6223 :
6224 39180440 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6225 58967285 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6226 : {
6227 : /* Use name match for PMF fields, as a variant will have a
6228 : different FIELD_DECL with a different type. */
6229 58741387 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6230 : : field == part)
6231 : {
6232 38954542 : if (value == void_node)
6233 6 : goto uninit;
6234 38954536 : if (value)
6235 : {
6236 38954518 : STRIP_ANY_LOCATION_WRAPPER (value);
6237 38954518 : return value;
6238 : }
6239 : else
6240 : /* We're in the middle of initializing it. */
6241 : break;
6242 : }
6243 : }
6244 225916 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6245 : {
6246 112 : if (CONSTRUCTOR_NELTS (whole) > 0)
6247 : {
6248 : /* DR 1188 says we don't have to deal with this. */
6249 97 : if (!ctx->quiet)
6250 : {
6251 20 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6252 20 : if (cep->value == NULL_TREE)
6253 9 : error ("accessing uninitialized member %qD", part);
6254 : else
6255 11 : error ("accessing %qD member instead of active %qD member "
6256 : "in constant expression", part, cep->index);
6257 : }
6258 97 : *non_constant_p = true;
6259 97 : return t;
6260 : }
6261 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6262 : {
6263 : /* Value-initialized union, check if looking at the first member. */
6264 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6265 12 : if (first != part)
6266 : {
6267 9 : if (!ctx->quiet)
6268 3 : error ("accessing %qD member instead of initialized %qD "
6269 : "member in constant expression", part, first);
6270 9 : *non_constant_p = true;
6271 9 : return t;
6272 : }
6273 : }
6274 : }
6275 :
6276 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6277 : classes never get represented; throw together a value now. */
6278 225810 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6279 209233 : return build_constructor (TREE_TYPE (t), NULL);
6280 :
6281 16577 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6282 :
6283 16577 : if (CONSTRUCTOR_NO_CLEARING (whole))
6284 : {
6285 112 : uninit:
6286 : /* 'whole' is part of the aggregate initializer we're currently
6287 : building; if there's no initializer for this member yet, that's an
6288 : error. */
6289 118 : if (!ctx->quiet)
6290 23 : error ("accessing uninitialized member %qD", part);
6291 118 : *non_constant_p = true;
6292 118 : return t;
6293 : }
6294 :
6295 : /* If there's no explicit init for this field, it's value-initialized. */
6296 :
6297 16465 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6298 : {
6299 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6300 : and copy the flags. */
6301 15869 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6302 15869 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6303 15869 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6304 15869 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6305 15869 : return value;
6306 : }
6307 :
6308 596 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6309 596 : return cxx_eval_constant_expression (ctx, value,
6310 : lval,
6311 : non_constant_p, overflow_p,
6312 596 : jump_target);
6313 : }
6314 :
6315 : /* Subroutine of cxx_eval_constant_expression.
6316 : Attempt to reduce a field access of a value of class type that is
6317 : expressed as a BIT_FIELD_REF. */
6318 :
6319 : static tree
6320 40745 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6321 : value_cat lval,
6322 : bool *non_constant_p, bool *overflow_p,
6323 : tree *jump_target)
6324 : {
6325 40745 : tree orig_whole = TREE_OPERAND (t, 0);
6326 40745 : tree retval, fldval, utype, mask;
6327 40745 : bool fld_seen = false;
6328 40745 : HOST_WIDE_INT istart, isize;
6329 40745 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6330 : lval,
6331 : non_constant_p, overflow_p,
6332 : jump_target);
6333 40745 : tree start, field, value;
6334 40745 : unsigned HOST_WIDE_INT i;
6335 :
6336 40745 : if (*jump_target)
6337 : return NULL_TREE;
6338 40745 : if (whole == orig_whole)
6339 : return t;
6340 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6341 : CONSTRUCTOR. */
6342 40596 : if (!*non_constant_p
6343 40596 : && TREE_CODE (whole) != VECTOR_CST
6344 0 : && TREE_CODE (whole) != CONSTRUCTOR)
6345 : {
6346 0 : if (!ctx->quiet)
6347 0 : error ("%qE is not a constant expression", orig_whole);
6348 0 : *non_constant_p = true;
6349 : }
6350 40596 : if (*non_constant_p)
6351 : return t;
6352 :
6353 40596 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6354 : {
6355 40596 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6356 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6357 : return r;
6358 0 : if (!ctx->quiet)
6359 0 : error ("%qE is not a constant expression", orig_whole);
6360 0 : *non_constant_p = true;
6361 0 : return t;
6362 : }
6363 :
6364 0 : start = TREE_OPERAND (t, 2);
6365 0 : istart = tree_to_shwi (start);
6366 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6367 0 : utype = TREE_TYPE (t);
6368 0 : if (!TYPE_UNSIGNED (utype))
6369 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6370 0 : retval = build_int_cst (utype, 0);
6371 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6372 : {
6373 0 : tree bitpos = bit_position (field);
6374 0 : STRIP_ANY_LOCATION_WRAPPER (value);
6375 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6376 : return value;
6377 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6378 0 : && TREE_CODE (value) == INTEGER_CST
6379 0 : && tree_fits_shwi_p (bitpos)
6380 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
6381 : {
6382 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6383 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6384 0 : HOST_WIDE_INT shift;
6385 0 : if (bit >= istart && bit + sz <= istart + isize)
6386 : {
6387 0 : fldval = fold_convert (utype, value);
6388 0 : mask = build_int_cst_type (utype, -1);
6389 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6390 : size_int (TYPE_PRECISION (utype) - sz));
6391 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6392 : size_int (TYPE_PRECISION (utype) - sz));
6393 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6394 0 : shift = bit - istart;
6395 0 : if (BYTES_BIG_ENDIAN)
6396 : shift = TYPE_PRECISION (utype) - shift - sz;
6397 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6398 : size_int (shift));
6399 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6400 0 : fld_seen = true;
6401 : }
6402 : }
6403 : }
6404 0 : if (fld_seen)
6405 0 : return fold_convert (TREE_TYPE (t), retval);
6406 0 : gcc_unreachable ();
6407 : return error_mark_node;
6408 : }
6409 :
6410 : /* Helper for cxx_eval_bit_cast.
6411 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6412 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6413 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6414 : data members of reference type. */
6415 :
6416 : static bool
6417 56266 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6418 : tree orig_type)
6419 : {
6420 57266 : if (TREE_CODE (type) == UNION_TYPE)
6421 : {
6422 63 : if (!ctx->quiet)
6423 : {
6424 21 : if (type == orig_type)
6425 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6426 : "a union type", "__builtin_bit_cast", type);
6427 : else
6428 15 : error_at (loc, "%qs is not a constant expression because %qT "
6429 : "contains a union type", "__builtin_bit_cast",
6430 : orig_type);
6431 : }
6432 : return true;
6433 : }
6434 : if (TREE_CODE (type) == POINTER_TYPE)
6435 : {
6436 57 : if (!ctx->quiet)
6437 : {
6438 18 : if (type == orig_type)
6439 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6440 : "a pointer type", "__builtin_bit_cast", type);
6441 : else
6442 12 : error_at (loc, "%qs is not a constant expression because %qT "
6443 : "contains a pointer type", "__builtin_bit_cast",
6444 : orig_type);
6445 : }
6446 : return true;
6447 : }
6448 : if (TREE_CODE (type) == REFERENCE_TYPE)
6449 : {
6450 0 : if (!ctx->quiet)
6451 : {
6452 0 : if (type == orig_type)
6453 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6454 : "a reference type", "__builtin_bit_cast", type);
6455 : else
6456 0 : error_at (loc, "%qs is not a constant expression because %qT "
6457 : "contains a reference type", "__builtin_bit_cast",
6458 : orig_type);
6459 : }
6460 : return true;
6461 : }
6462 39307 : if (TYPE_PTRMEM_P (type))
6463 : {
6464 36 : if (!ctx->quiet)
6465 : {
6466 12 : if (type == orig_type)
6467 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6468 : "a pointer to member type", "__builtin_bit_cast",
6469 : type);
6470 : else
6471 0 : error_at (loc, "%qs is not a constant expression because %qT "
6472 : "contains a pointer to member type",
6473 : "__builtin_bit_cast", orig_type);
6474 : }
6475 : return true;
6476 : }
6477 57110 : if (TYPE_VOLATILE (type))
6478 : {
6479 0 : if (!ctx->quiet)
6480 : {
6481 0 : if (type == orig_type)
6482 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6483 : "volatile", "__builtin_bit_cast", type);
6484 : else
6485 0 : error_at (loc, "%qs is not a constant expression because %qT "
6486 : "contains a volatile subobject",
6487 : "__builtin_bit_cast", orig_type);
6488 : }
6489 : return true;
6490 : }
6491 57110 : if (TREE_CODE (type) == RECORD_TYPE)
6492 2293972 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6493 2254773 : if (TREE_CODE (field) == FIELD_DECL
6494 2254773 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6495 : return true;
6496 57020 : if (TREE_CODE (type) == ARRAY_TYPE)
6497 1000 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6498 : return false;
6499 : }
6500 :
6501 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6502 : std::byte members of CONSTRUCTOR (recursively) if they contain
6503 : some indeterminate bits (as set in MASK), remove the ctor elts,
6504 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6505 : bits in MASK. */
6506 :
6507 : static void
6508 9631 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6509 : {
6510 9631 : if (TREE_CODE (t) != CONSTRUCTOR)
6511 : return;
6512 :
6513 : unsigned i, j = 0;
6514 : tree index, value;
6515 23564 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6516 : {
6517 13933 : tree type = TREE_TYPE (value);
6518 13933 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6519 27427 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6520 : {
6521 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6522 : {
6523 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6524 135 : gcc_assert (fldsz != 0);
6525 135 : HOST_WIDE_INT pos = int_byte_position (index);
6526 135 : HOST_WIDE_INT bpos
6527 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6528 135 : bpos %= BITS_PER_UNIT;
6529 135 : HOST_WIDE_INT end
6530 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6531 135 : gcc_assert (end == 1 || end == 2);
6532 135 : unsigned char *p = mask + pos;
6533 135 : unsigned char mask_save[2];
6534 135 : mask_save[0] = mask[pos];
6535 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6536 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6537 : sorry_at (loc, "PDP11 bit-field handling unsupported"
6538 : " in %qs", "__builtin_bit_cast");
6539 135 : else if (BYTES_BIG_ENDIAN)
6540 : {
6541 : /* Big endian. */
6542 : if (bpos + fldsz <= BITS_PER_UNIT)
6543 : *p &= ~(((1 << fldsz) - 1)
6544 : << (BITS_PER_UNIT - bpos - fldsz));
6545 : else
6546 : {
6547 : gcc_assert (bpos);
6548 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6549 : p++;
6550 : fldsz -= BITS_PER_UNIT - bpos;
6551 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6552 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6553 : }
6554 : }
6555 : else
6556 : {
6557 : /* Little endian. */
6558 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6559 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6560 : else
6561 : {
6562 0 : gcc_assert (bpos);
6563 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6564 0 : p++;
6565 0 : fldsz -= BITS_PER_UNIT - bpos;
6566 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6567 0 : *p &= ~((1 << fldsz) - 1);
6568 : }
6569 : }
6570 135 : if (mask_save[0] != mask[pos]
6571 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6572 : {
6573 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6574 36 : continue;
6575 : }
6576 : }
6577 : }
6578 13663 : else if (is_byte_access_type_not_plain_char (type))
6579 : {
6580 228 : HOST_WIDE_INT pos;
6581 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6582 132 : pos = tree_to_shwi (index);
6583 : else
6584 96 : pos = int_byte_position (index);
6585 228 : if (mask[pos])
6586 : {
6587 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6588 48 : mask[pos] = 0;
6589 48 : continue;
6590 : }
6591 : }
6592 13849 : if (TREE_CODE (value) == CONSTRUCTOR)
6593 : {
6594 7292 : HOST_WIDE_INT pos;
6595 7292 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6596 144 : pos = tree_to_shwi (index)
6597 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6598 : else
6599 7220 : pos = int_byte_position (index);
6600 7292 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6601 : }
6602 13849 : if (i != j)
6603 : {
6604 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6605 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6606 : }
6607 13849 : ++j;
6608 : }
6609 19262 : if (CONSTRUCTOR_NELTS (t) != j)
6610 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6611 : }
6612 :
6613 : /* Subroutine of cxx_eval_constant_expression.
6614 : Attempt to evaluate a BIT_CAST_EXPR. */
6615 :
6616 : static tree
6617 4019 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6618 : bool *overflow_p, tree *jump_target)
6619 : {
6620 4019 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6621 4019 : TREE_TYPE (t))
6622 12709 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6623 3938 : EXPR_LOCATION (t)),
6624 3938 : TREE_TYPE (TREE_OPERAND (t, 0)),
6625 3938 : TREE_TYPE (TREE_OPERAND (t, 0))))
6626 : {
6627 156 : *non_constant_p = true;
6628 156 : return t;
6629 : }
6630 :
6631 3863 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6632 : non_constant_p, overflow_p,
6633 : jump_target);
6634 3863 : if (*non_constant_p)
6635 : return t;
6636 2814 : if (*jump_target)
6637 : return NULL_TREE;
6638 :
6639 2814 : location_t loc = EXPR_LOCATION (t);
6640 2814 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6641 : {
6642 : if (!ctx->quiet)
6643 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6644 : "__builtin_bit_cast");
6645 : *non_constant_p = true;
6646 : return t;
6647 : }
6648 :
6649 2814 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6650 : {
6651 0 : if (!ctx->quiet)
6652 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6653 : "type is too large", "__builtin_bit_cast");
6654 0 : *non_constant_p = true;
6655 0 : return t;
6656 : }
6657 :
6658 2814 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6659 2814 : if (len < 0 || (int) len != len)
6660 : {
6661 0 : if (!ctx->quiet)
6662 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6663 : "type is too large", "__builtin_bit_cast");
6664 0 : *non_constant_p = true;
6665 0 : return t;
6666 : }
6667 :
6668 2814 : unsigned char buf[64];
6669 2814 : unsigned char *ptr, *mask;
6670 2814 : size_t alen = (size_t) len * 2;
6671 2814 : if (alen <= sizeof (buf))
6672 : ptr = buf;
6673 : else
6674 329 : ptr = XNEWVEC (unsigned char, alen);
6675 2814 : mask = ptr + (size_t) len;
6676 : /* At the beginning consider everything indeterminate. */
6677 2814 : memset (mask, ~0, (size_t) len);
6678 :
6679 2814 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6680 : {
6681 0 : if (!ctx->quiet)
6682 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6683 : "argument cannot be encoded", "__builtin_bit_cast");
6684 0 : *non_constant_p = true;
6685 0 : if (ptr != buf)
6686 0 : XDELETE (ptr);
6687 : return t;
6688 : }
6689 :
6690 2814 : tree r = NULL_TREE;
6691 2814 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6692 : {
6693 475 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6694 475 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6695 : {
6696 46 : gcc_assert (len == 1);
6697 46 : if (mask[0])
6698 : {
6699 24 : memset (mask, 0, len);
6700 24 : r = build_constructor (TREE_TYPE (r), NULL);
6701 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6702 : }
6703 : }
6704 : }
6705 2339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6706 : {
6707 2339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6708 2339 : if (r != NULL_TREE)
6709 : {
6710 2339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6711 2339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6712 2339 : if (CHECKING_P)
6713 : {
6714 2339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6715 : non_constant_p, overflow_p,
6716 : jump_target);
6717 2339 : gcc_checking_assert (e == r && !*jump_target);
6718 : r = e;
6719 : }
6720 : }
6721 : }
6722 :
6723 2814 : if (r != NULL_TREE)
6724 : {
6725 92029 : for (int i = 0; i < len; i++)
6726 89305 : if (mask[i])
6727 : {
6728 90 : if (!ctx->quiet)
6729 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6730 : "__builtin_bit_cast", i);
6731 90 : *non_constant_p = true;
6732 90 : r = t;
6733 90 : break;
6734 : }
6735 2814 : if (ptr != buf)
6736 329 : XDELETE (ptr);
6737 : return r;
6738 : }
6739 :
6740 0 : if (!ctx->quiet)
6741 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6742 : "argument cannot be interpreted", "__builtin_bit_cast");
6743 0 : *non_constant_p = true;
6744 0 : if (ptr != buf)
6745 0 : XDELETE (ptr);
6746 : return t;
6747 : }
6748 :
6749 : /* Subroutine of cxx_eval_constant_expression.
6750 : Evaluate a short-circuited logical expression T in the context
6751 : of a given constexpr CALL. BAILOUT_VALUE is the value for
6752 : early return. CONTINUE_VALUE is used here purely for
6753 : sanity check purposes. */
6754 :
6755 : static tree
6756 18460668 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6757 : tree bailout_value, tree continue_value,
6758 : bool *non_constant_p, bool *overflow_p,
6759 : tree *jump_target)
6760 : {
6761 18460668 : tree r;
6762 18460668 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6763 : vc_prvalue, non_constant_p,
6764 : overflow_p, jump_target);
6765 18460666 : if (*jump_target)
6766 : return NULL_TREE;
6767 18460657 : VERIFY_CONSTANT (lhs);
6768 13678739 : if (tree_int_cst_equal (lhs, bailout_value))
6769 : return lhs;
6770 11637262 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6771 11637262 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6772 : vc_prvalue, non_constant_p,
6773 : overflow_p, jump_target);
6774 11637262 : if (*jump_target)
6775 : return NULL_TREE;
6776 11637262 : VERIFY_CONSTANT (r);
6777 : return r;
6778 : }
6779 :
6780 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6781 : CONSTRUCTOR elements to initialize (part of) an object containing that
6782 : field. Return a pointer to the constructor_elt corresponding to the
6783 : initialization of the field. */
6784 :
6785 : static constructor_elt *
6786 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6787 : {
6788 0 : tree aggr = TREE_OPERAND (ref, 0);
6789 0 : tree field = TREE_OPERAND (ref, 1);
6790 0 : HOST_WIDE_INT i;
6791 0 : constructor_elt *ce;
6792 :
6793 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6794 :
6795 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6796 : {
6797 0 : constructor_elt *base_ce
6798 0 : = base_field_constructor_elt (v, aggr);
6799 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6800 : }
6801 :
6802 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6803 0 : if (ce->index == field)
6804 0 : return ce;
6805 :
6806 0 : gcc_unreachable ();
6807 : return NULL;
6808 : }
6809 :
6810 : /* Some of the expressions fed to the constexpr mechanism are calls to
6811 : constructors, which have type void. In that case, return the type being
6812 : initialized by the constructor. */
6813 :
6814 : static tree
6815 600241379 : initialized_type (tree t)
6816 : {
6817 601899291 : if (TYPE_P (t))
6818 : return t;
6819 601898827 : tree type = TREE_TYPE (t);
6820 601898827 : if (TREE_CODE (t) == CALL_EXPR)
6821 : {
6822 : /* A constructor call has void type, so we need to look deeper. */
6823 74052177 : tree fn = get_function_named_in_call (t);
6824 74045698 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6825 147971464 : && DECL_CXX_CONSTRUCTOR_P (fn))
6826 4089234 : type = DECL_CONTEXT (fn);
6827 : }
6828 527846650 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6829 1657912 : return initialized_type (TREE_OPERAND (t, 1));
6830 526188738 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6831 882707 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6832 600240915 : return cv_unqualified (type);
6833 : }
6834 :
6835 : /* We're about to initialize element INDEX of an array or class from VALUE.
6836 : Set up NEW_CTX appropriately by adjusting .object to refer to the
6837 : subobject and creating a new CONSTRUCTOR if the element is itself
6838 : a class or array. */
6839 :
6840 : static void
6841 4194738 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6842 : tree index, tree &value)
6843 : {
6844 4194738 : new_ctx = *ctx;
6845 :
6846 4194738 : if (index && TREE_CODE (index) != INTEGER_CST
6847 3757491 : && TREE_CODE (index) != FIELD_DECL
6848 3 : && TREE_CODE (index) != RANGE_EXPR)
6849 : /* This won't have an element in the new CONSTRUCTOR. */
6850 : return;
6851 :
6852 4194738 : tree type = initialized_type (value);
6853 4194738 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6854 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6855 : return;
6856 :
6857 1692561 : tree ctxtype = NULL_TREE;
6858 1692561 : if (ctx->ctor)
6859 1692544 : ctxtype = TREE_TYPE (ctx->ctor);
6860 17 : else if (ctx->object)
6861 14 : ctxtype = TREE_TYPE (ctx->object);
6862 : else
6863 : {
6864 : /* This can happen if the enclosing object is also an empty subobject
6865 : (c++/125315). */
6866 3 : gcc_checking_assert (is_empty_field (index));
6867 : return;
6868 : }
6869 :
6870 1692558 : if (VECTOR_TYPE_P (type)
6871 7417 : && VECTOR_TYPE_P (ctxtype)
6872 2596 : && index == NULL_TREE)
6873 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6874 : vector is constructed from smaller vectors, doesn't get its own
6875 : CONSTRUCTOR either. */
6876 : return;
6877 :
6878 : /* The sub-aggregate initializer might contain a placeholder;
6879 : update object to refer to the subobject and ctor to refer to
6880 : the (newly created) sub-initializer. */
6881 1689962 : if (ctx->object)
6882 : {
6883 1530011 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6884 : /* There's no well-defined subobject for this index. */
6885 3 : new_ctx.object = NULL_TREE;
6886 : else
6887 1530008 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6888 : }
6889 :
6890 1689962 : if (is_empty_field (index)
6891 1689962 : && TREE_CODE (ctxtype) != UNION_TYPE)
6892 : /* Leave ctor null for an empty subobject of a non-union class, they aren't
6893 : represented in the result of evaluation. */
6894 1515091 : new_ctx.ctor = NULL_TREE;
6895 : else
6896 : {
6897 174871 : tree elt = build_constructor (type, NULL);
6898 174871 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6899 174871 : new_ctx.ctor = elt;
6900 : }
6901 :
6902 1689962 : if (TREE_CODE (value) == TARGET_EXPR)
6903 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6904 119760 : value = TARGET_EXPR_INITIAL (value);
6905 : }
6906 :
6907 : /* We're about to process an initializer for a class or array TYPE. Make
6908 : sure that CTX is set up appropriately. */
6909 :
6910 : static void
6911 3385983 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6912 : {
6913 : /* We don't bother building a ctor for an empty base subobject. */
6914 3385983 : if (is_empty_class (type))
6915 : return;
6916 :
6917 : /* We're in the middle of an initializer that might involve placeholders;
6918 : our caller should have created a CONSTRUCTOR for us to put the
6919 : initializer into. We will either return that constructor or T. */
6920 1889113 : gcc_assert (ctx->ctor);
6921 1889113 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6922 : (type, TREE_TYPE (ctx->ctor)));
6923 : /* We used to check that ctx->ctor was empty, but that isn't the case when
6924 : the object is zero-initialized before calling the constructor. */
6925 1889113 : if (ctx->object)
6926 : {
6927 1674216 : tree otype = TREE_TYPE (ctx->object);
6928 1674216 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6929 : /* Handle flexible array members. */
6930 : || (TREE_CODE (otype) == ARRAY_TYPE
6931 : && TYPE_DOMAIN (otype) == NULL_TREE
6932 : && TREE_CODE (type) == ARRAY_TYPE
6933 : && (same_type_ignoring_top_level_qualifiers_p
6934 : (TREE_TYPE (type), TREE_TYPE (otype)))));
6935 : }
6936 1889113 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6937 : || ctx->global->get_value (ctx->object) == ctx->ctor);
6938 : }
6939 :
6940 : /* Subroutine of cxx_eval_constant_expression.
6941 : The expression tree T denotes a C-style array or a C-style
6942 : aggregate. Reduce it to a constant expression. */
6943 :
6944 : static tree
6945 3385079 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6946 : value_cat lval,
6947 : bool *non_constant_p, bool *overflow_p,
6948 : tree *jump_target)
6949 : {
6950 3385079 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6951 3385079 : bool changed = false;
6952 3385079 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6953 3385079 : tree type = TREE_TYPE (t);
6954 :
6955 3385079 : constexpr_ctx new_ctx;
6956 3385079 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6957 : {
6958 : /* We don't really need the ctx->ctor business for a PMF or
6959 : vector, but it's simpler to use the same code. */
6960 56001 : new_ctx = *ctx;
6961 56001 : new_ctx.ctor = build_constructor (type, NULL);
6962 56001 : new_ctx.object = NULL_TREE;
6963 56001 : ctx = &new_ctx;
6964 3385079 : };
6965 3385079 : verify_ctor_sanity (ctx, type);
6966 3385079 : vec<constructor_elt, va_gc> **p = nullptr;
6967 3385079 : if (ctx->ctor)
6968 : {
6969 3385062 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6970 6766675 : vec_alloc (*p, vec_safe_length (v));
6971 3385062 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6972 16083 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6973 : }
6974 :
6975 3385079 : unsigned i;
6976 3385079 : tree index, value;
6977 3385079 : bool constant_p = true;
6978 3385079 : bool side_effects_p = false;
6979 6849403 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6980 : {
6981 4188301 : tree orig_value = value;
6982 4188301 : init_subob_ctx (ctx, new_ctx, index, value);
6983 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6984 4188301 : bool no_slot = new_ctx.ctor == NULL_TREE;
6985 4188301 : int pos_hint = -1;
6986 4188301 : if (!ctx->ctor)
6987 : /* The enclosing object could be an empty subobject so we have no
6988 : CONSTRUCTOR (c++/125336). */
6989 17 : gcc_checking_assert (is_empty_class (type));
6990 4188284 : else if (new_ctx.ctor != ctx->ctor && !no_slot)
6991 : {
6992 : /* If we built a new CONSTRUCTOR, attach it now so that other
6993 : initializers can refer to it. */
6994 168733 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6995 168733 : cep->value = new_ctx.ctor;
6996 168733 : pos_hint = cep - (*p)->begin();
6997 168733 : }
6998 4019551 : else if (TREE_CODE (type) == UNION_TYPE)
6999 : /* Otherwise if we're constructing a non-aggregate union member, set
7000 : the active union member now so that we can later detect and diagnose
7001 : if its initializer attempts to activate another member. */
7002 1089 : get_or_insert_ctor_field (ctx->ctor, index);
7003 4188301 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
7004 : lval,
7005 : non_constant_p, overflow_p,
7006 : jump_target);
7007 4188301 : if (*jump_target)
7008 : return NULL_TREE;
7009 : /* Don't VERIFY_CONSTANT here. */
7010 4188295 : if (ctx->quiet && *non_constant_p)
7011 : break;
7012 3464324 : if (elt != orig_value)
7013 1243946 : changed = true;
7014 :
7015 3464324 : if (!TREE_CONSTANT (elt))
7016 953804 : constant_p = false;
7017 3464324 : if (TREE_SIDE_EFFECTS (elt))
7018 281 : side_effects_p = true;
7019 3464324 : if (index && TREE_CODE (index) == COMPONENT_REF)
7020 : {
7021 : /* This is an initialization of a vfield inside a base
7022 : subaggregate that we already initialized; push this
7023 : initialization into the previous initialization. */
7024 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
7025 0 : inner->value = elt;
7026 0 : changed = true;
7027 0 : }
7028 3464324 : else if (no_slot)
7029 : /* This is an initializer for an empty field; now that we've
7030 : checked that it's constant, we can ignore it. */
7031 : changed = true;
7032 1950399 : else if (ctx->ctor)
7033 : {
7034 1950399 : if (TREE_CODE (type) == UNION_TYPE
7035 1950399 : && (*p)->last().index != index)
7036 : /* The initializer erroneously changed the active union member that
7037 : we're initializing. */
7038 7 : gcc_assert (*non_constant_p);
7039 : else
7040 : {
7041 : /* The initializer might have mutated the underlying CONSTRUCTOR,
7042 : so recompute the location of the target constructer_elt. */
7043 1950392 : constructor_elt *cep
7044 1950392 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
7045 1950392 : cep->value = elt;
7046 : }
7047 :
7048 : /* Adding or replacing an element might change the ctor's flags. */
7049 1950399 : TREE_CONSTANT (ctx->ctor) = constant_p;
7050 1950399 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
7051 : }
7052 : }
7053 3385073 : if (*non_constant_p)
7054 : return t;
7055 2661035 : if (!changed)
7056 : {
7057 267517 : if (VECTOR_TYPE_P (type))
7058 14408 : t = fold (t);
7059 : return t;
7060 : }
7061 2393518 : t = ctx->ctor;
7062 2393518 : if (!t)
7063 8 : t = build_constructor (type, NULL);
7064 : /* We're done building this CONSTRUCTOR, so now we can interpret an
7065 : element without an explicit initializer as value-initialized. */
7066 2393518 : CONSTRUCTOR_NO_CLEARING (t) = false;
7067 2393518 : TREE_CONSTANT (t) = constant_p;
7068 2393518 : TREE_SIDE_EFFECTS (t) = side_effects_p;
7069 2393518 : if (VECTOR_TYPE_P (type))
7070 11444 : t = fold (t);
7071 : return t;
7072 : }
7073 :
7074 : /* Subroutine of cxx_eval_constant_expression.
7075 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
7076 : initialization of a non-static data member of array type. Reduce it to a
7077 : CONSTRUCTOR.
7078 :
7079 : Note that apart from value-initialization (when VALUE_INIT is true),
7080 : this is only intended to support value-initialization and the
7081 : initializations done by defaulted constructors for classes with
7082 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
7083 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
7084 : for the copy/move constructor. */
7085 :
7086 : static tree
7087 904 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
7088 : bool value_init, value_cat lval,
7089 : bool *non_constant_p, bool *overflow_p,
7090 : tree *jump_target)
7091 : {
7092 904 : tree elttype = TREE_TYPE (atype);
7093 904 : verify_ctor_sanity (ctx, atype);
7094 904 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
7095 904 : bool pre_init = false;
7096 904 : unsigned HOST_WIDE_INT i;
7097 904 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7098 :
7099 904 : if (init && TREE_CODE (init) == CONSTRUCTOR)
7100 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
7101 0 : non_constant_p, overflow_p, jump_target);
7102 :
7103 : /* We already checked access when building the VEC_INIT_EXPR. */
7104 904 : deferring_access_check_sentinel acs (dk_deferred);
7105 :
7106 : /* For the default constructor, build up a call to the default
7107 : constructor of the element type. We only need to handle class types
7108 : here, as for a constructor to be constexpr, all members must be
7109 : initialized, which for a defaulted default constructor means they must
7110 : be of a class type with a constexpr default constructor. */
7111 904 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7112 : /* We only do this at the lowest level. */;
7113 850 : else if (value_init)
7114 : {
7115 336 : init = build_value_init (elttype, complain);
7116 336 : pre_init = true;
7117 : }
7118 514 : else if (!init)
7119 : {
7120 402 : releasing_vec argvec;
7121 402 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7122 : &argvec, elttype, LOOKUP_NORMAL,
7123 : complain);
7124 402 : init = build_aggr_init_expr (elttype, init);
7125 402 : pre_init = true;
7126 402 : }
7127 :
7128 904 : bool zeroed_out = false;
7129 904 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
7130 : {
7131 : /* We're initializing an array object that had been zero-initialized
7132 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
7133 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
7134 : initializers we append to it. */
7135 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
7136 156 : zeroed_out = true;
7137 156 : vec_safe_truncate (*p, 0);
7138 : }
7139 :
7140 904 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
7141 : overflow_p, jump_target);
7142 904 : if (*jump_target)
7143 : return NULL_TREE;
7144 904 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
7145 6674 : for (i = 0; i < max; ++i)
7146 : {
7147 6437 : tree idx = build_int_cst (size_type_node, i);
7148 6437 : tree eltinit;
7149 6437 : bool reuse = false;
7150 6437 : constexpr_ctx new_ctx;
7151 6901 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
7152 6437 : bool no_slot = new_ctx.ctor == NULL_TREE;
7153 6437 : if (new_ctx.ctor != ctx->ctor && !no_slot)
7154 : {
7155 6138 : if (zeroed_out)
7156 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
7157 6138 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
7158 : }
7159 6437 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7160 : {
7161 : /* A multidimensional array; recurse. */
7162 186 : if (value_init || init == NULL_TREE)
7163 : {
7164 176 : eltinit = NULL_TREE;
7165 176 : reuse = i == 0;
7166 : }
7167 : else
7168 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7169 186 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
7170 : value_init, lval, non_constant_p,
7171 : overflow_p, jump_target);
7172 : }
7173 6251 : else if (pre_init)
7174 : {
7175 : /* Initializing an element using value or default initialization
7176 : we just pre-built above. */
7177 5973 : if (init == void_node)
7178 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
7179 10 : return ctx->ctor;
7180 5963 : eltinit = init;
7181 5963 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7182 : /* Clarify what object is being initialized (118285). */
7183 5665 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7184 5963 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7185 : non_constant_p, overflow_p,
7186 : jump_target);
7187 5963 : reuse = i == 0;
7188 : }
7189 : else
7190 : {
7191 : /* Copying an element. */
7192 278 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7193 278 : if (!lvalue_p (init))
7194 232 : eltinit = move (eltinit);
7195 278 : eltinit = (perform_implicit_conversion_flags
7196 278 : (elttype, eltinit, complain,
7197 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7198 271 : if (CLASS_TYPE_P (elttype)
7199 271 : && new_ctx.object
7200 526 : && !error_operand_p (eltinit))
7201 : /* Clarify what object is being initialized (118285). */
7202 244 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7203 278 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7204 : non_constant_p, overflow_p,
7205 : jump_target);
7206 : }
7207 6427 : if (*jump_target)
7208 : return NULL_TREE;
7209 6427 : if (*non_constant_p)
7210 : break;
7211 6249 : if (no_slot)
7212 : {
7213 : /* This is an initializer for an empty subobject; now that we've
7214 : checked that it's constant, we can ignore it. */
7215 0 : gcc_checking_assert (i == 0);
7216 : break;
7217 : }
7218 6249 : else if (new_ctx.ctor != ctx->ctor)
7219 : {
7220 : /* We appended this element above; update the value. */
7221 5957 : gcc_assert ((*p)->last().index == idx);
7222 5957 : (*p)->last().value = eltinit;
7223 : }
7224 : else
7225 292 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7226 : /* Reuse the result of cxx_eval_constant_expression call
7227 : from the first iteration to all others if it is a constant
7228 : initializer that doesn't require relocations. */
7229 6249 : if (reuse
7230 6249 : && max > 1
7231 6249 : && (eltinit == NULL_TREE
7232 632 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7233 632 : == null_pointer_node)))
7234 : {
7235 479 : if (new_ctx.ctor != ctx->ctor)
7236 201 : eltinit = new_ctx.ctor;
7237 479 : tree range = build2 (RANGE_EXPR, size_type_node,
7238 : build_int_cst (size_type_node, 1),
7239 479 : build_int_cst (size_type_node, max - 1));
7240 479 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7241 479 : break;
7242 : }
7243 5770 : else if (i == 0)
7244 234 : vec_safe_reserve (*p, max);
7245 : }
7246 :
7247 894 : if (!*non_constant_p)
7248 : {
7249 716 : init = ctx->ctor;
7250 716 : CONSTRUCTOR_NO_CLEARING (init) = false;
7251 : }
7252 894 : return init;
7253 : }
7254 :
7255 : static tree
7256 790 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7257 : value_cat lval,
7258 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7259 : {
7260 790 : tree atype = TREE_TYPE (t);
7261 790 : tree init = VEC_INIT_EXPR_INIT (t);
7262 790 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7263 790 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7264 : ;
7265 83 : else if (CONSTRUCTOR_NELTS (init) == 0
7266 83 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7267 : {
7268 : /* Handle {} as value-init. */
7269 : init = NULL_TREE;
7270 : value_init = true;
7271 : }
7272 : else
7273 : {
7274 : /* This is a more complicated case, like needing to loop over trailing
7275 : elements; call build_vec_init and evaluate the result. */
7276 72 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7277 72 : constexpr_ctx new_ctx = *ctx;
7278 72 : if (!ctx->object)
7279 : {
7280 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7281 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7282 57 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7283 57 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7284 57 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7285 57 : ctx->global->put_value (new_ctx.object, ctor);
7286 57 : ctx = &new_ctx;
7287 : }
7288 72 : init = expand_vec_init_expr (ctx->object, t, complain);
7289 72 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7290 : overflow_p, jump_target);
7291 : }
7292 718 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7293 : lval, non_constant_p, overflow_p, jump_target);
7294 718 : if (*non_constant_p)
7295 : return t;
7296 : else
7297 566 : return r;
7298 : }
7299 :
7300 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7301 : where the desired type is an array of unknown bounds because the variable
7302 : has had its bounds deduced since the wrapping expression was created. */
7303 :
7304 : static bool
7305 444686378 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7306 : {
7307 444686378 : while (TREE_CODE (type1) == ARRAY_TYPE
7308 144752 : && TREE_CODE (type2) == ARRAY_TYPE
7309 444686382 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7310 : {
7311 2 : type1 = TREE_TYPE (type1);
7312 2 : type2 = TREE_TYPE (type2);
7313 : }
7314 444686378 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7315 : }
7316 :
7317 : /* Try to determine the currently active union member for an expression
7318 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7319 : otherwise return NULL_TREE. */
7320 :
7321 : static tree
7322 86 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7323 : {
7324 86 : constexpr_ctx new_ctx = *ctx;
7325 86 : new_ctx.quiet = true;
7326 86 : bool non_constant_p = false, overflow_p = false;
7327 86 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7328 : &non_constant_p,
7329 : &overflow_p, jump_target);
7330 86 : if (*jump_target)
7331 : return NULL_TREE;
7332 86 : if (TREE_CODE (ctor) == CONSTRUCTOR
7333 32 : && CONSTRUCTOR_NELTS (ctor) == 1
7334 20 : && CONSTRUCTOR_ELT (ctor, 0)->index
7335 106 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7336 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7337 : return NULL_TREE;
7338 : }
7339 :
7340 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7341 :
7342 : static tree
7343 15210973 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7344 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7345 : tree *jump_target)
7346 : {
7347 24659604 : tree optype = TREE_TYPE (op);
7348 24659604 : unsigned HOST_WIDE_INT const_nunits;
7349 24659604 : if (off == 0 && similar_type_p (optype, type))
7350 : return op;
7351 15207180 : else if (cxx_dialect >= cxx26
7352 9319744 : && VAR_P (op)
7353 5315268 : && DECL_VTABLE_OR_VTT_P (op)
7354 15646 : && same_type_ignoring_top_level_qualifiers_p (type,
7355 : ptrdiff_type_node)
7356 15208525 : && POINTER_TYPE_P (strip_array_types (optype)))
7357 : {
7358 : /* We often read some virtual table elements using ptrdiff_t rather
7359 : than pointer type. */
7360 1345 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7361 : strip_array_types (optype),
7362 : op, off, empty_base,
7363 : jump_target))
7364 1345 : return fold_convert (type, ret);
7365 : }
7366 15205835 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7367 15205835 : && similar_type_p (type, TREE_TYPE (optype)))
7368 : {
7369 : /* *(foo *)&complexfoo => __real__ complexfoo */
7370 0 : if (off == 0)
7371 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7372 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7373 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7374 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7375 : }
7376 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7377 15205835 : else if (VECTOR_TYPE_P (optype)
7378 0 : && similar_type_p (type, TREE_TYPE (optype))
7379 15205835 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7380 : {
7381 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7382 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7383 0 : if (off < max_offset && off % part_width == 0)
7384 : {
7385 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7386 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7387 0 : TYPE_SIZE (type), index);
7388 : }
7389 : }
7390 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7391 15205835 : else if (TREE_CODE (optype) == ARRAY_TYPE
7392 9448631 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7393 24654466 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7394 : {
7395 9448631 : tree type_domain = TYPE_DOMAIN (optype);
7396 9448631 : tree min_val = size_zero_node;
7397 9448631 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7398 9448426 : min_val = TYPE_MIN_VALUE (type_domain);
7399 9448631 : unsigned HOST_WIDE_INT el_sz
7400 9448631 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7401 9448631 : unsigned HOST_WIDE_INT idx = off / el_sz;
7402 9448631 : unsigned HOST_WIDE_INT rem = off % el_sz;
7403 9448631 : if (tree_fits_uhwi_p (min_val))
7404 : {
7405 9448631 : tree index = size_int (idx + tree_to_uhwi (min_val));
7406 9448631 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7407 : NULL_TREE, NULL_TREE);
7408 9448631 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7409 9448631 : empty_base, jump_target);
7410 : }
7411 : }
7412 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7413 5757204 : else if (TREE_CODE (optype) == RECORD_TYPE
7414 5757204 : || TREE_CODE (optype) == UNION_TYPE)
7415 : {
7416 5753218 : if (TREE_CODE (optype) == UNION_TYPE)
7417 : /* For unions prefer the currently active member. */
7418 86 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7419 : {
7420 20 : unsigned HOST_WIDE_INT el_sz
7421 20 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7422 20 : if (off < el_sz)
7423 : {
7424 20 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7425 : op, field, NULL_TREE);
7426 20 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7427 : off, empty_base,
7428 : jump_target))
7429 : return ret;
7430 : }
7431 : }
7432 :
7433 : /* Handle conversion to "as base" type. */
7434 5753204 : if (CLASS_TYPE_P (optype)
7435 11506244 : && CLASSTYPE_AS_BASE (optype) == type)
7436 : return op;
7437 :
7438 : /* Handle conversion to an empty base class, which is represented with a
7439 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7440 : which is likely to be a waste of time (109678). */
7441 5747124 : if (is_empty_class (type)
7442 5414946 : && CLASS_TYPE_P (optype)
7443 11162064 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
7444 : {
7445 2632541 : if (empty_base)
7446 2632541 : *empty_base = true;
7447 : return op;
7448 : }
7449 :
7450 3114583 : for (tree field = TYPE_FIELDS (optype);
7451 44688635 : field; field = DECL_CHAIN (field))
7452 44649716 : if (TREE_CODE (field) == FIELD_DECL
7453 3122941 : && TREE_TYPE (field) != error_mark_node
7454 47772657 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
7455 : {
7456 3122937 : tree pos = byte_position (field);
7457 3122937 : if (!tree_fits_uhwi_p (pos))
7458 0 : continue;
7459 3122937 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
7460 3122937 : unsigned HOST_WIDE_INT el_sz;
7461 3122937 : if (DECL_FIELD_IS_BASE (field)
7462 787237 : && CLASS_TYPE_P (optype)
7463 3910174 : && CLASSTYPE_VBASECLASSES (optype))
7464 7765 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
7465 : else
7466 3115172 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7467 3122937 : if (upos <= off && off < upos + el_sz)
7468 : {
7469 3114490 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7470 : op, field, NULL_TREE);
7471 3114490 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7472 : off - upos,
7473 : empty_base,
7474 : jump_target))
7475 : return ret;
7476 : }
7477 : }
7478 : }
7479 :
7480 : return NULL_TREE;
7481 : }
7482 :
7483 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7484 : match. We want to be less strict for simple *& folding; if we have a
7485 : non-const temporary that we access through a const pointer, that should
7486 : work. We handle this here rather than change fold_indirect_ref_1
7487 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7488 : don't really make sense outside of constant expression evaluation. Also
7489 : we want to allow folding to COMPONENT_REF, which could cause trouble
7490 : with TBAA in fold_indirect_ref_1. */
7491 :
7492 : static tree
7493 196098018 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7494 : tree op0, bool *empty_base, tree *jump_target)
7495 : {
7496 196098018 : tree sub = op0;
7497 196098018 : tree subtype;
7498 :
7499 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7500 404136434 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7501 527841667 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7502 : {
7503 133155789 : if (TREE_CODE (sub) == NOP_EXPR
7504 133155789 : && REINTERPRET_CAST_P (sub))
7505 : return NULL_TREE;
7506 133155789 : sub = TREE_OPERAND (sub, 0);
7507 : }
7508 :
7509 196098018 : subtype = TREE_TYPE (sub);
7510 196098018 : if (!INDIRECT_TYPE_P (subtype))
7511 : return NULL_TREE;
7512 :
7513 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7514 : the innermost component into the offset until it would make the
7515 : offset positive, so that cxx_fold_indirect_ref_1 can identify
7516 : more folding opportunities. */
7517 208193087 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7518 12095118 : if (cxx_dialect >= cxx26)
7519 : {
7520 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7521 : to access virtual base members. */
7522 7238256 : tree nobj = obj;
7523 7238256 : while (TREE_CODE (nobj) == COMPONENT_REF
7524 7274322 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7525 36066 : nobj = TREE_OPERAND (nobj, 0);
7526 7238256 : if (nobj != obj
7527 28174 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7528 7266430 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7529 2537 : while (obj != nobj)
7530 : {
7531 1424 : tree field = TREE_OPERAND (obj, 1);
7532 1424 : tree pos = byte_position (field);
7533 1424 : off = int_const_binop (PLUS_EXPR, off, pos);
7534 1424 : obj = TREE_OPERAND (obj, 0);
7535 : }
7536 : }
7537 15205552 : while (TREE_CODE (obj) == COMPONENT_REF
7538 : /* We need to preserve union member accesses so that we can
7539 : later properly diagnose accessing the wrong member. */
7540 6426970 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7541 21416530 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7542 : {
7543 3299043 : tree field = TREE_OPERAND (obj, 1);
7544 3299043 : tree pos = byte_position (field);
7545 3299043 : if (integer_zerop (off) && integer_nonzerop (pos))
7546 : /* If the offset is already 0, keep going as long as the
7547 : component is at position 0. */
7548 : break;
7549 3110434 : off = int_const_binop (PLUS_EXPR, off, pos);
7550 3110434 : obj = TREE_OPERAND (obj, 0);
7551 : }
7552 12095118 : };
7553 :
7554 196097969 : if (TREE_CODE (sub) == ADDR_EXPR)
7555 : {
7556 87605602 : tree op = TREE_OPERAND (sub, 0);
7557 87605602 : tree optype = TREE_TYPE (op);
7558 :
7559 : /* *&CONST_DECL -> to the value of the const decl. */
7560 87605602 : if (TREE_CODE (op) == CONST_DECL)
7561 0 : return DECL_INITIAL (op);
7562 : /* *&p => p; make sure to handle *&"str"[cst] here. */
7563 87605602 : if (similar_type_p (optype, type))
7564 : {
7565 83700136 : tree fop = fold_read_from_constant_string (op);
7566 83700136 : if (fop)
7567 : return fop;
7568 : else
7569 83492461 : return op;
7570 : }
7571 : else
7572 : {
7573 3905466 : tree off = integer_zero_node;
7574 3905466 : canonicalize_obj_off (op, off);
7575 3905466 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7576 : tree_to_uhwi (off), empty_base,
7577 : jump_target);
7578 : }
7579 : }
7580 108492367 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7581 108492367 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7582 : {
7583 8784854 : tree op00 = TREE_OPERAND (sub, 0);
7584 8784854 : tree off = TREE_OPERAND (sub, 1);
7585 :
7586 8784854 : STRIP_NOPS (op00);
7587 8784854 : if (TREE_CODE (op00) == ADDR_EXPR)
7588 : {
7589 8189652 : tree obj = TREE_OPERAND (op00, 0);
7590 8189652 : canonicalize_obj_off (obj, off);
7591 8189652 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7592 : tree_to_uhwi (off), empty_base,
7593 : jump_target);
7594 : }
7595 : }
7596 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7597 99707513 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7598 99707513 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7599 : {
7600 0 : tree type_domain;
7601 0 : tree min_val = size_zero_node;
7602 0 : tree newsub
7603 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7604 : jump_target);
7605 0 : if (*jump_target)
7606 : return NULL_TREE;
7607 0 : if (newsub)
7608 : sub = newsub;
7609 : else
7610 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7611 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7612 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7613 0 : min_val = TYPE_MIN_VALUE (type_domain);
7614 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7615 0 : NULL_TREE);
7616 : }
7617 :
7618 : return NULL_TREE;
7619 : }
7620 :
7621 : static tree
7622 103606845 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7623 : value_cat lval,
7624 : bool *non_constant_p, bool *overflow_p,
7625 : tree *jump_target)
7626 : {
7627 103606845 : tree orig_op0 = TREE_OPERAND (t, 0);
7628 103606845 : bool empty_base = false;
7629 :
7630 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7631 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7632 :
7633 103606845 : if (TREE_CODE (t) == MEM_REF
7634 103606845 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7635 : {
7636 9 : gcc_assert (ctx->quiet);
7637 9 : *non_constant_p = true;
7638 9 : return t;
7639 : }
7640 :
7641 : /* First try to simplify it directly. */
7642 103606836 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7643 : orig_op0, &empty_base, jump_target);
7644 103606836 : if (*jump_target)
7645 : return NULL_TREE;
7646 103606836 : if (!r)
7647 : {
7648 : /* If that didn't work, evaluate the operand first. */
7649 99605695 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7650 : vc_prvalue, non_constant_p,
7651 : overflow_p, jump_target);
7652 99605695 : if (*jump_target)
7653 : return NULL_TREE;
7654 : /* Don't VERIFY_CONSTANT here. */
7655 99605683 : if (*non_constant_p)
7656 : return t;
7657 :
7658 78676318 : if (!lval && integer_zerop (op0))
7659 : {
7660 73 : if (!ctx->quiet)
7661 12 : error ("dereferencing a null pointer");
7662 73 : *non_constant_p = true;
7663 73 : return t;
7664 : }
7665 :
7666 78676245 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7667 : &empty_base, jump_target);
7668 78676245 : if (*jump_target)
7669 : return NULL_TREE;
7670 78676245 : if (r == NULL_TREE)
7671 : {
7672 : /* We couldn't fold to a constant value. Make sure it's not
7673 : something we should have been able to fold. */
7674 701073 : tree sub = op0;
7675 701073 : STRIP_NOPS (sub);
7676 701073 : if (TREE_CODE (sub) == ADDR_EXPR)
7677 : {
7678 1948 : gcc_assert (!similar_type_p
7679 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7680 : /* DR 1188 says we don't have to deal with this. */
7681 1948 : if (!ctx->quiet)
7682 : {
7683 9 : auto_diagnostic_group d;
7684 15 : error_at (cp_expr_loc_or_input_loc (t),
7685 : "accessing value of %qT object through a %qT "
7686 : "glvalue in a constant expression",
7687 9 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7688 9 : tree ob = build_fold_indirect_ref (sub);
7689 9 : if (DECL_P (ob))
7690 : {
7691 9 : if (DECL_ARTIFICIAL (ob))
7692 2 : inform (DECL_SOURCE_LOCATION (ob),
7693 2 : "%qT object created here", TREE_TYPE (ob));
7694 : else
7695 7 : inform (DECL_SOURCE_LOCATION (ob),
7696 : "%q#D declared here", ob);
7697 : }
7698 9 : }
7699 1948 : *non_constant_p = true;
7700 1948 : return t;
7701 : }
7702 :
7703 699125 : if (lval == vc_glvalue && op0 != orig_op0)
7704 84229 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7705 614896 : if (!lval)
7706 500261 : VERIFY_CONSTANT (t);
7707 : return t;
7708 : }
7709 : }
7710 :
7711 81976313 : r = cxx_eval_constant_expression (ctx, r,
7712 : lval, non_constant_p, overflow_p,
7713 : jump_target);
7714 81976311 : if (*jump_target)
7715 : return NULL_TREE;
7716 81976311 : if (*non_constant_p)
7717 : return t;
7718 :
7719 : /* If we're pulling out the value of an empty base, just return an empty
7720 : CONSTRUCTOR. */
7721 72933030 : if (empty_base && !lval)
7722 : {
7723 19629 : r = build_constructor (TREE_TYPE (t), NULL);
7724 19629 : TREE_CONSTANT (r) = true;
7725 : }
7726 :
7727 : return r;
7728 : }
7729 :
7730 : /* Complain about R, a DECL that is accessed outside its lifetime. */
7731 :
7732 : static void
7733 45 : outside_lifetime_error (location_t loc, tree r)
7734 : {
7735 45 : auto_diagnostic_group d;
7736 45 : if (DECL_NAME (r) == heap_deleted_identifier)
7737 : {
7738 : /* Provide a more accurate message for deleted variables. */
7739 14 : error_at (loc, "use of allocated storage after deallocation "
7740 : "in a constant expression");
7741 14 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7742 : }
7743 : else
7744 : {
7745 31 : error_at (loc, "accessing %qE outside its lifetime", r);
7746 31 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7747 : }
7748 45 : }
7749 :
7750 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7751 : FUNDEF_P is true if we're checking a constexpr function body.
7752 : Shared between potential_constant_expression and
7753 : cxx_eval_constant_expression. */
7754 :
7755 : static void
7756 354 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7757 : {
7758 354 : auto_diagnostic_group d;
7759 354 : tree type = TREE_TYPE (r);
7760 354 : if (DECL_NAME (r) == heap_uninit_identifier
7761 354 : || DECL_NAME (r) == heap_identifier
7762 351 : || DECL_NAME (r) == heap_vec_uninit_identifier
7763 705 : || DECL_NAME (r) == heap_vec_identifier)
7764 : {
7765 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7766 : "storage is not usable in a constant expression"))
7767 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7768 : return;
7769 : }
7770 351 : if (DECL_NAME (r) == heap_deleted_identifier)
7771 : {
7772 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7773 : "deallocation in a constant expression"))
7774 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7775 : return;
7776 : }
7777 351 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7778 : "a constant expression", r))
7779 : return;
7780 : /* Avoid error cascade. */
7781 344 : if (DECL_INITIAL (r) == error_mark_node)
7782 : return;
7783 330 : if (DECL_DECLARED_CONSTEXPR_P (r))
7784 3 : inform (DECL_SOURCE_LOCATION (r),
7785 : "%qD used in its own initializer", r);
7786 327 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7787 : {
7788 225 : if (!CP_TYPE_CONST_P (type))
7789 196 : inform (DECL_SOURCE_LOCATION (r),
7790 : "%q#D is not const", r);
7791 29 : else if (CP_TYPE_VOLATILE_P (type))
7792 0 : inform (DECL_SOURCE_LOCATION (r),
7793 : "%q#D is volatile", r);
7794 29 : else if (!DECL_INITIAL (r)
7795 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7796 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7797 29 : inform (DECL_SOURCE_LOCATION (r),
7798 : "%qD was not initialized with a constant "
7799 : "expression", r);
7800 : else
7801 0 : gcc_unreachable ();
7802 : }
7803 102 : else if (TYPE_REF_P (type))
7804 9 : inform (DECL_SOURCE_LOCATION (r),
7805 : "%qD was not initialized with a constant "
7806 : "expression", r);
7807 : else
7808 : {
7809 93 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7810 93 : inform (DECL_SOURCE_LOCATION (r),
7811 : "%qD was not declared %<constexpr%>", r);
7812 : else
7813 0 : inform (DECL_SOURCE_LOCATION (r),
7814 : "%qD does not have integral or enumeration type",
7815 : r);
7816 : }
7817 354 : }
7818 :
7819 : /* Subroutine of cxx_eval_constant_expression.
7820 : Like cxx_eval_unary_expression, except for trinary expressions. */
7821 :
7822 : static tree
7823 43640 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7824 : value_cat lval,
7825 : bool *non_constant_p, bool *overflow_p,
7826 : tree *jump_target)
7827 : {
7828 43640 : int i;
7829 43640 : tree args[3];
7830 43640 : tree val;
7831 :
7832 168053 : for (i = 0; i < 3; i++)
7833 : {
7834 126582 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7835 : lval,
7836 : non_constant_p, overflow_p,
7837 : jump_target);
7838 126582 : if (*jump_target)
7839 : return NULL_TREE;
7840 126582 : VERIFY_CONSTANT (args[i]);
7841 : }
7842 :
7843 41471 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7844 : args[0], args[1], args[2]);
7845 41471 : if (val == NULL_TREE)
7846 : return t;
7847 41471 : VERIFY_CONSTANT (val);
7848 : return val;
7849 : }
7850 :
7851 : /* True if T was declared in a function declared to be constexpr, and
7852 : therefore potentially constant in C++14. */
7853 :
7854 : bool
7855 89558766 : var_in_constexpr_fn (tree t)
7856 : {
7857 89558766 : tree ctx = DECL_CONTEXT (t);
7858 89558766 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7859 170581247 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7860 : }
7861 :
7862 : /* True if a function might be constexpr: either a function that was
7863 : declared constexpr, or a C++17 lambda op(). */
7864 :
7865 : bool
7866 736040298 : maybe_constexpr_fn (tree t)
7867 : {
7868 736040298 : return (DECL_DECLARED_CONSTEXPR_P (t)
7869 225667611 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7870 953101377 : || (flag_implicit_constexpr
7871 96 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7872 : }
7873 :
7874 : /* True if T was declared in a function that might be constexpr: either a
7875 : function that was declared constexpr, or a C++17 lambda op(). */
7876 :
7877 : bool
7878 66762267 : var_in_maybe_constexpr_fn (tree t)
7879 : {
7880 133523887 : return (DECL_FUNCTION_SCOPE_P (t)
7881 122022548 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7882 : }
7883 :
7884 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7885 : build_over_call we implement trivial copy of a class with tail padding using
7886 : assignment of character arrays, which is valid in normal code, but not in
7887 : constexpr evaluation. We don't need to worry about clobbering tail padding
7888 : in constexpr evaluation, so strip the type punning. */
7889 :
7890 : static void
7891 103344626 : maybe_simplify_trivial_copy (tree &target, tree &init)
7892 : {
7893 103344626 : if (TREE_CODE (target) == MEM_REF
7894 36160 : && TREE_CODE (init) == MEM_REF
7895 35992 : && TREE_TYPE (target) == TREE_TYPE (init)
7896 35992 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7897 103380618 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7898 : {
7899 35992 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7900 35992 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7901 : }
7902 103344626 : }
7903 :
7904 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7905 : of constant type. This does not check for 'mutable', so the
7906 : caller is expected to be mindful of that. */
7907 :
7908 : static bool
7909 419 : cref_has_const_field (tree ref)
7910 : {
7911 488 : while (TREE_CODE (ref) == COMPONENT_REF)
7912 : {
7913 479 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7914 : return true;
7915 69 : ref = TREE_OPERAND (ref, 0);
7916 : }
7917 : return false;
7918 : }
7919 :
7920 : /* Return true if we are modifying something that is const during constant
7921 : expression evaluation. CODE is the code of the statement, OBJ is the
7922 : object in question, MUTABLE_P is true if one of the subobjects were
7923 : declared mutable. */
7924 :
7925 : static bool
7926 107077228 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7927 : {
7928 : /* If this is initialization, there's no problem. */
7929 107077228 : if (code != MODIFY_EXPR)
7930 : return false;
7931 :
7932 : /* [basic.type.qualifier] "A const object is an object of type
7933 : const T or a non-mutable subobject of a const object." */
7934 29968262 : if (mutable_p)
7935 : return false;
7936 :
7937 29967520 : if (TREE_READONLY (obj))
7938 : return true;
7939 :
7940 29912757 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7941 : {
7942 : /* Although a COMPONENT_REF may have a const type, we should
7943 : only consider it modifying a const object when any of the
7944 : field components is const. This can happen when using
7945 : constructs such as const_cast<const T &>(m), making something
7946 : const even though it wasn't declared const. */
7947 33637 : if (TREE_CODE (obj) == COMPONENT_REF)
7948 419 : return cref_has_const_field (obj);
7949 : else
7950 : return true;
7951 : }
7952 :
7953 : return false;
7954 : }
7955 :
7956 : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7957 :
7958 : static tree
7959 103344626 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7960 : value_cat lval,
7961 : bool *non_constant_p, bool *overflow_p,
7962 : tree *jump_target)
7963 : {
7964 103344626 : constexpr_ctx new_ctx = *ctx;
7965 :
7966 103344626 : tree init = TREE_OPERAND (t, 1);
7967 :
7968 : /* First we figure out where we're storing to. */
7969 103344626 : tree target = TREE_OPERAND (t, 0);
7970 :
7971 103344626 : maybe_simplify_trivial_copy (target, init);
7972 :
7973 103344626 : tree type = TREE_TYPE (target);
7974 103344626 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7975 72549497 : if (preeval && !TREE_CLOBBER_P (init))
7976 : {
7977 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7978 71484957 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7979 41640616 : && TREE_CODE (init) == CALL_EXPR
7980 6798310 : && CALL_EXPR_FN (init) == NULL_TREE
7981 71484965 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7982 8 : return void_node;
7983 :
7984 : /* Evaluate the value to be stored without knowing what object it will be
7985 : stored in, so that any side-effects happen first. */
7986 71484949 : if (!SCALAR_TYPE_P (type))
7987 205376 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7988 71484949 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7989 : non_constant_p, overflow_p,
7990 : jump_target);
7991 71484945 : if (*jump_target)
7992 : return NULL_TREE;
7993 71484677 : if (*non_constant_p)
7994 : return t;
7995 : }
7996 :
7997 94179530 : bool evaluated = false;
7998 94179530 : if (lval == vc_glvalue)
7999 : {
8000 : /* If we want to return a reference to the target, we need to evaluate it
8001 : as a whole; otherwise, only evaluate the innermost piece to avoid
8002 : building up unnecessary *_REFs. */
8003 0 : target = cxx_eval_constant_expression (ctx, target, lval,
8004 : non_constant_p, overflow_p,
8005 : jump_target);
8006 0 : evaluated = true;
8007 0 : if (*jump_target)
8008 : return NULL_TREE;
8009 0 : if (*non_constant_p)
8010 : return t;
8011 : }
8012 :
8013 : /* Find the underlying variable. */
8014 94179530 : releasing_vec refs;
8015 94179530 : tree object = NULL_TREE;
8016 : /* If we're modifying a const object, save it. */
8017 94179530 : tree const_object_being_modified = NULL_TREE;
8018 94179530 : bool mutable_p = false;
8019 : /* If we see a union, we can't ignore clobbers. */
8020 94179530 : int seen_union = 0;
8021 318876269 : for (tree probe = target; object == NULL_TREE; )
8022 : {
8023 224697194 : switch (TREE_CODE (probe))
8024 : {
8025 36338472 : case BIT_FIELD_REF:
8026 36338472 : case COMPONENT_REF:
8027 36338472 : case ARRAY_REF:
8028 36338472 : {
8029 36338472 : tree ob = TREE_OPERAND (probe, 0);
8030 36338472 : tree elt = TREE_OPERAND (probe, 1);
8031 36338472 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
8032 : mutable_p = true;
8033 36338472 : if (TREE_CODE (probe) == ARRAY_REF)
8034 : {
8035 5887605 : elt = eval_and_check_array_index (ctx, probe, false,
8036 : non_constant_p, overflow_p,
8037 : jump_target);
8038 5887605 : if (*jump_target)
8039 69 : return NULL_TREE;
8040 5887605 : if (*non_constant_p)
8041 : return t;
8042 : }
8043 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
8044 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
8045 : the array isn't const. Instead, check "a" in the next iteration;
8046 : that will detect modifying "const int a[10]". */
8047 30450867 : else if (evaluated
8048 12898153 : && modifying_const_object_p (TREE_CODE (t), probe,
8049 : mutable_p)
8050 30451481 : && const_object_being_modified == NULL_TREE)
8051 : const_object_being_modified = probe;
8052 :
8053 : /* Track named member accesses for unions to validate modifications
8054 : that change active member. */
8055 36338403 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
8056 17552714 : vec_safe_push (refs, probe);
8057 : else
8058 18785689 : vec_safe_push (refs, NULL_TREE);
8059 :
8060 36338403 : vec_safe_push (refs, elt);
8061 36338403 : vec_safe_push (refs, TREE_TYPE (probe));
8062 36338403 : probe = ob;
8063 36338403 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
8064 581842 : ++seen_union;
8065 : }
8066 36338403 : break;
8067 :
8068 24 : case REALPART_EXPR:
8069 24 : gcc_assert (refs->is_empty ());
8070 24 : vec_safe_push (refs, NULL_TREE);
8071 24 : vec_safe_push (refs, probe);
8072 24 : vec_safe_push (refs, TREE_TYPE (probe));
8073 24 : probe = TREE_OPERAND (probe, 0);
8074 24 : break;
8075 :
8076 25 : case IMAGPART_EXPR:
8077 25 : gcc_assert (refs->is_empty ());
8078 25 : vec_safe_push (refs, NULL_TREE);
8079 25 : vec_safe_push (refs, probe);
8080 25 : vec_safe_push (refs, TREE_TYPE (probe));
8081 25 : probe = TREE_OPERAND (probe, 0);
8082 25 : break;
8083 :
8084 188358673 : default:
8085 188358673 : if (evaluated)
8086 : object = probe;
8087 : else
8088 : {
8089 94179598 : tree pvar = tree_strip_any_location_wrapper (probe);
8090 94179598 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
8091 : {
8092 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
8093 : active union member. */
8094 101 : probe = DECL_VALUE_EXPR (pvar);
8095 101 : break;
8096 : }
8097 94179497 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
8098 : non_constant_p, overflow_p,
8099 : jump_target);
8100 94179497 : evaluated = true;
8101 94179497 : if (*jump_target)
8102 : return NULL_TREE;
8103 94179497 : if (*non_constant_p)
8104 : return t;
8105 : }
8106 : break;
8107 : }
8108 : }
8109 :
8110 94179075 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
8111 94179075 : && const_object_being_modified == NULL_TREE)
8112 94179075 : const_object_being_modified = object;
8113 :
8114 94179075 : if (DECL_P (object)
8115 94177189 : && TREE_CLOBBER_P (init)
8116 95266493 : && DECL_NAME (object) == heap_deleted_identifier)
8117 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
8118 : message later when operator delete is called. */
8119 15 : return void_node;
8120 :
8121 : /* And then find/build up our initializer for the path to the subobject
8122 : we're initializing. */
8123 94179060 : tree *valp;
8124 94179060 : if (DECL_P (object))
8125 94177174 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8126 : else
8127 : valp = NULL;
8128 94179060 : if (!valp)
8129 : {
8130 : /* A constant-expression cannot modify objects from outside the
8131 : constant-expression. */
8132 11470 : if (!ctx->quiet)
8133 : {
8134 29 : auto_diagnostic_group d;
8135 29 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
8136 : {
8137 0 : error ("modification of allocated storage after deallocation "
8138 : "is not a constant expression");
8139 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
8140 : }
8141 29 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
8142 : {
8143 14 : if (TREE_CLOBBER_P (init))
8144 6 : error ("destroying %qE outside its lifetime", object);
8145 : else
8146 8 : error ("modification of %qE outside its lifetime "
8147 : "is not a constant expression", object);
8148 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
8149 : }
8150 : else
8151 : {
8152 15 : if (TREE_CLOBBER_P (init))
8153 6 : error ("destroying %qE from outside current evaluation "
8154 : "is not a constant expression", object);
8155 : else
8156 9 : error ("modification of %qE from outside current evaluation "
8157 : "is not a constant expression", object);
8158 : }
8159 29 : }
8160 11470 : *non_constant_p = true;
8161 11470 : return t;
8162 : }
8163 :
8164 : /* Handle explicit end-of-lifetime. */
8165 94167590 : if (TREE_CLOBBER_P (init))
8166 : {
8167 1087352 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
8168 1087352 : && refs->is_empty ())
8169 : {
8170 303482 : ctx->global->destroy_value (object);
8171 303482 : return void_node;
8172 : }
8173 :
8174 771077 : if (!seen_union && !*valp
8175 794581 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
8176 10670 : return void_node;
8177 :
8178 : /* Ending the lifetime of a const object is OK. */
8179 : const_object_being_modified = NULL_TREE;
8180 : }
8181 :
8182 93853438 : type = TREE_TYPE (object);
8183 93853438 : bool no_zero_init = true;
8184 93853438 : bool zero_padding_bits = false;
8185 :
8186 187706876 : auto_vec<tree *> ctors;
8187 187706876 : releasing_vec indexes;
8188 187706876 : auto_vec<int> index_pos_hints;
8189 93853438 : bool activated_union_member_p = false;
8190 93853438 : bool empty_base = false;
8191 129870046 : while (!refs->is_empty ())
8192 : {
8193 36307324 : if (*valp == NULL_TREE)
8194 : {
8195 3020357 : *valp = build_constructor (type, NULL);
8196 3020357 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8197 3020357 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8198 : }
8199 33286967 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8200 33286967 : TREE_CODE (*valp) == STRING_CST)
8201 : {
8202 : /* An array was initialized with a string constant, and now
8203 : we're writing into one of its elements. Explode the
8204 : single initialization into a set of element
8205 : initializations. */
8206 11423 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8207 :
8208 11423 : tree string = *valp;
8209 11423 : tree elt_type = TREE_TYPE (type);
8210 11423 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8211 11423 : / TYPE_PRECISION (char_type_node));
8212 11423 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8213 11423 : tree ary_ctor = build_constructor (type, NULL);
8214 :
8215 11423 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8216 34423 : for (unsigned ix = 0; ix != num_elts; ix++)
8217 : {
8218 11577 : constructor_elt elt =
8219 : {
8220 11577 : build_int_cst (size_type_node, ix),
8221 11577 : extract_string_elt (string, chars_per_elt, ix)
8222 11577 : };
8223 11577 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8224 : }
8225 :
8226 11423 : *valp = ary_ctor;
8227 : }
8228 :
8229 36307324 : enum tree_code code = TREE_CODE (type);
8230 36307324 : tree reftype = refs->pop();
8231 36307324 : tree index = refs->pop();
8232 36307324 : bool is_access_expr = refs->pop() != NULL_TREE;
8233 :
8234 36307324 : if (code == COMPLEX_TYPE)
8235 : {
8236 49 : if (TREE_CODE (*valp) == COMPLEX_CST)
8237 45 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8238 45 : TREE_IMAGPART (*valp));
8239 4 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8240 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8241 6 : && CONSTRUCTOR_NO_CLEARING (*valp))
8242 : {
8243 2 : tree r = build_constructor (reftype, NULL);
8244 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8245 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8246 : }
8247 49 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8248 49 : ctors.safe_push (valp);
8249 49 : vec_safe_push (indexes, index);
8250 49 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8251 49 : gcc_checking_assert (refs->is_empty ());
8252 : type = reftype;
8253 286929 : break;
8254 : }
8255 :
8256 : /* If the value of object is already zero-initialized, any new ctors for
8257 : subobjects will also be zero-initialized. Similarly with zeroing of
8258 : padding bits. */
8259 36307275 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8260 36307275 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8261 :
8262 36307275 : if (code == RECORD_TYPE && is_empty_field (index))
8263 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8264 : have no data and might have an offset lower than previously declared
8265 : fields, which confuses the middle-end. The code below will notice
8266 : that we don't have a CONSTRUCTOR for our inner target and just
8267 : return init. */
8268 : {
8269 : empty_base = true;
8270 : break;
8271 : }
8272 :
8273 : /* If a union is zero-initialized, its first non-static named data member
8274 : is zero-initialized (and therefore active). */
8275 36020395 : if (code == UNION_TYPE
8276 36020395 : && !no_zero_init
8277 36020395 : && CONSTRUCTOR_NELTS (*valp) == 0)
8278 4073 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8279 4073 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8280 :
8281 : /* Check for implicit change of active member for a union. */
8282 :
8283 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8284 : now allow initializing an array element to activate the array. */
8285 36072662 : auto only_array_refs = [](const releasing_vec &refs)
8286 : {
8287 52272 : for (unsigned i = 1; i < refs->length(); i += 3)
8288 35 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8289 : return false;
8290 : return true;
8291 : };
8292 :
8293 36020395 : if (code == UNION_TYPE
8294 580328 : && (CONSTRUCTOR_NELTS (*valp) == 0
8295 379050 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8296 : /* An INIT_EXPR of the last member in an access chain is always OK,
8297 : but still check implicit change of members earlier on; see
8298 : cpp2a/constexpr-union6.C. */
8299 36234077 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8300 : {
8301 161445 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8302 161445 : tree inner = strip_array_types (reftype);
8303 :
8304 161445 : if (has_active_member && cxx_dialect < cxx20)
8305 : {
8306 58 : if (!ctx->quiet)
8307 19 : error_at (cp_expr_loc_or_input_loc (t),
8308 : "change of the active member of a union "
8309 : "from %qD to %qD is not a constant expression "
8310 : "before C++20",
8311 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8312 : index);
8313 58 : *non_constant_p = true;
8314 : }
8315 161387 : else if (!is_access_expr
8316 46014 : || (TREE_CLOBBER_P (init)
8317 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8318 207401 : || (TREE_CODE (t) == MODIFY_EXPR
8319 46002 : && CLASS_TYPE_P (inner)
8320 19 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8321 : {
8322 : /* Diagnose changing active union member after initialization
8323 : without a valid member access expression, as described in
8324 : [class.union.general] p5. */
8325 115382 : if (!ctx->quiet)
8326 : {
8327 33 : auto_diagnostic_group d;
8328 33 : if (has_active_member)
8329 24 : error_at (cp_expr_loc_or_input_loc (t),
8330 : "accessing %qD member instead of initialized "
8331 : "%qD member in constant expression",
8332 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8333 : else
8334 9 : error_at (cp_expr_loc_or_input_loc (t),
8335 : "accessing uninitialized member %qD",
8336 : index);
8337 33 : if (is_access_expr)
8338 3 : inform (DECL_SOURCE_LOCATION (index),
8339 : "%qD does not implicitly begin its lifetime "
8340 : "because %qT does not have a non-deleted "
8341 : "trivial default constructor, use "
8342 : "%<std::construct_at%> instead",
8343 : index, inner);
8344 : else
8345 30 : inform (DECL_SOURCE_LOCATION (index),
8346 : "initializing %qD requires a member access "
8347 : "expression as the left operand of the assignment",
8348 : index);
8349 33 : }
8350 115382 : *non_constant_p = true;
8351 : }
8352 46005 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8353 : {
8354 : /* Diagnose changing the active union member while the union
8355 : is in the process of being initialized. */
8356 18 : if (!ctx->quiet)
8357 6 : error_at (cp_expr_loc_or_input_loc (t),
8358 : "change of the active member of a union "
8359 : "from %qD to %qD during initialization",
8360 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8361 : index);
8362 18 : *non_constant_p = true;
8363 : }
8364 : no_zero_init = true;
8365 : }
8366 :
8367 : /* Ending the lifetime of the active union member means the union no
8368 : longer has an active member. */
8369 580328 : if (code == UNION_TYPE && refs->is_empty ()
8370 98950 : && TREE_CLOBBER_P (init)
8371 36028446 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8372 : {
8373 1978 : vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
8374 3787 : return void_node;
8375 : }
8376 :
8377 36018417 : ctors.safe_push (valp);
8378 36018417 : vec_safe_push (indexes, index);
8379 :
8380 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8381 : uninitialized. */
8382 34114297 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8383 1772965 : && CONSTRUCTOR_NO_CLEARING (*valp)
8384 37598385 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8385 36018417 : constructor_elt *cep
8386 36018417 : = get_or_insert_ctor_field (*valp, index, pos);
8387 36018417 : if (cep == nullptr)
8388 1809 : return void_node;
8389 36016608 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8390 :
8391 36016608 : if (code == UNION_TYPE)
8392 : {
8393 578350 : activated_union_member_p = true;
8394 578350 : --seen_union;
8395 : }
8396 :
8397 36016608 : valp = &cep->value;
8398 36016608 : type = reftype;
8399 : }
8400 :
8401 : /* Change an "as-base" clobber to the real type;
8402 : we don't need to worry about padding in constexpr. */
8403 93849651 : tree itype = initialized_type (init);
8404 93849651 : if (IS_FAKE_BASE_TYPE (itype))
8405 5058 : itype = TYPE_CONTEXT (itype);
8406 :
8407 : /* For initialization of an empty base, the original target will be
8408 : *(base*)this, evaluation of which resolves to the object
8409 : argument, which has the derived type rather than the base type. */
8410 187412422 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8411 93562771 : (itype, type)))
8412 : {
8413 19002 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8414 : empty_base = true;
8415 : }
8416 :
8417 : /* Detect modifying a constant object in constexpr evaluation.
8418 : We have found a const object that is being modified. Figure out
8419 : if we need to issue an error. Consider
8420 :
8421 : struct A {
8422 : int n;
8423 : constexpr A() : n(1) { n = 2; } // #1
8424 : };
8425 : struct B {
8426 : const A a;
8427 : constexpr B() { a.n = 3; } // #2
8428 : };
8429 : constexpr B b{};
8430 :
8431 : #1 is OK, since we're modifying an object under construction, but
8432 : #2 is wrong, since "a" is const and has been fully constructed.
8433 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8434 : which means that the object is read-only. For the example above, the
8435 : *ctors stack at the point of #2 will look like:
8436 :
8437 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
8438 : ctors[1] = {.n=2} TREE_READONLY = 1
8439 :
8440 : and we're modifying "b.a", so we search the stack and see if the
8441 : constructor for "b.a" has already run. */
8442 93849651 : if (const_object_being_modified)
8443 : {
8444 86882 : bool fail = false;
8445 86882 : tree const_objtype
8446 86882 : = strip_array_types (TREE_TYPE (const_object_being_modified));
8447 86882 : if (!CLASS_TYPE_P (const_objtype))
8448 : fail = true;
8449 : else
8450 : {
8451 : /* [class.ctor]p5 "A constructor can be invoked for a const,
8452 : volatile, or const volatile object. const and volatile
8453 : semantics are not applied on an object under construction.
8454 : They come into effect when the constructor for the most
8455 : derived object ends." */
8456 260845 : for (tree *elt : ctors)
8457 87291 : if (same_type_ignoring_top_level_qualifiers_p
8458 87291 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
8459 : {
8460 86777 : fail = TREE_READONLY (*elt);
8461 86777 : break;
8462 : }
8463 : }
8464 86777 : if (fail)
8465 : {
8466 198 : if (!ctx->quiet)
8467 63 : modifying_const_object_error (t, const_object_being_modified);
8468 198 : *non_constant_p = true;
8469 198 : return t;
8470 : }
8471 : }
8472 :
8473 93849453 : if (!preeval)
8474 : {
8475 : /* We're handling an INIT_EXPR of class type, so the value of the
8476 : initializer can depend on the object it's initializing. */
8477 :
8478 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
8479 : wants to modify it. */
8480 30773958 : if (*valp == NULL_TREE)
8481 : {
8482 29675612 : *valp = build_constructor (type, NULL);
8483 29675612 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8484 29675612 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8485 : }
8486 30773958 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8487 30773958 : new_ctx.object = target;
8488 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8489 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8490 : expansion of those trees uses ctx instead. */
8491 30773958 : if (TREE_CODE (init) == TARGET_EXPR)
8492 3283078 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8493 3283078 : init = tinit;
8494 30773958 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8495 : non_constant_p, overflow_p,
8496 : jump_target);
8497 30773958 : if (*jump_target)
8498 : return NULL_TREE;
8499 : /* The hash table might have moved since the get earlier, and the
8500 : initializer might have mutated the underlying CONSTRUCTORs, so we must
8501 : recompute VALP. */
8502 30773780 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8503 35247277 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8504 : {
8505 4473497 : ctors[i] = valp;
8506 4473497 : constructor_elt *cep
8507 4473497 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8508 4473497 : valp = &cep->value;
8509 : }
8510 : }
8511 :
8512 93849275 : if (*non_constant_p)
8513 : return t;
8514 :
8515 : /* Don't share a CONSTRUCTOR that might be changed later. */
8516 91991451 : init = unshare_constructor (init);
8517 :
8518 91991451 : gcc_checking_assert (!*valp
8519 : || *valp == void_node
8520 : || (same_type_ignoring_top_level_qualifiers_p
8521 : (TREE_TYPE (*valp), type)));
8522 91991451 : if (empty_base)
8523 : {
8524 : /* Just evaluate the initializer and return, since there's no actual data
8525 : to store, and we didn't build a CONSTRUCTOR. */
8526 294412 : if (!*valp)
8527 : {
8528 : /* But do make sure we have something in *valp. */
8529 0 : *valp = build_constructor (type, nullptr);
8530 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8531 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8532 : }
8533 : }
8534 91697039 : else if (TREE_CLOBBER_P (init))
8535 : {
8536 769413 : if (AGGREGATE_TYPE_P (type))
8537 : {
8538 541537 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
8539 541494 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8540 : else
8541 43 : *valp = build_constructor (type, nullptr);
8542 541537 : TREE_CONSTANT (*valp) = true;
8543 541537 : TREE_SIDE_EFFECTS (*valp) = false;
8544 541537 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8545 541537 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8546 : }
8547 : else
8548 227876 : *valp = void_node;
8549 : }
8550 90927626 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8551 28910187 : && TREE_CODE (init) == CONSTRUCTOR)
8552 : {
8553 : /* An outer ctx->ctor might be pointing to *valp, so replace
8554 : its contents. */
8555 4739225 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8556 4739225 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8557 4739225 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8558 14217675 : CONSTRUCTOR_NO_CLEARING (*valp)
8559 4739225 : = CONSTRUCTOR_NO_CLEARING (init);
8560 14217675 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8561 4739225 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8562 : }
8563 : else
8564 86188401 : *valp = init;
8565 :
8566 : /* After initialization, 'const' semantics apply to the value of the
8567 : object. Make a note of this fact by marking the CONSTRUCTOR
8568 : TREE_READONLY. */
8569 91991451 : if (TREE_CODE (t) == INIT_EXPR
8570 69522712 : && !empty_base
8571 69228300 : && TREE_CODE (*valp) == CONSTRUCTOR
8572 96557538 : && TYPE_READONLY (type))
8573 : {
8574 35681 : tree target_type = TREE_TYPE (target);
8575 35681 : if (IS_FAKE_BASE_TYPE (target_type))
8576 0 : target_type = TYPE_CONTEXT (target_type);
8577 35681 : if (INDIRECT_REF_P (target)
8578 50753 : && (is_this_parameter
8579 15072 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8580 : /* We've just initialized '*this' (perhaps via the target
8581 : constructor of a delegating constructor). Leave it up to the
8582 : caller that set 'this' to set TREE_READONLY appropriately. */
8583 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8584 : (target_type, type) || empty_base);
8585 : else
8586 35658 : TREE_READONLY (*valp) = true;
8587 : }
8588 :
8589 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8590 : CONSTRUCTORs, if any. */
8591 91991451 : bool c = TREE_CONSTANT (init);
8592 91991451 : bool s = TREE_SIDE_EFFECTS (init);
8593 91991451 : if (!indexes->is_empty ())
8594 : {
8595 20417744 : tree last = indexes->last ();
8596 20417744 : if (TREE_CODE (last) == REALPART_EXPR
8597 20417744 : || TREE_CODE (last) == IMAGPART_EXPR)
8598 : {
8599 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8600 : possible. */
8601 49 : tree *cexpr = ctors.last ();
8602 49 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8603 49 : TREE_OPERAND (*cexpr, 0),
8604 49 : TREE_OPERAND (*cexpr, 1)))
8605 47 : *cexpr = c;
8606 : else
8607 : {
8608 4 : TREE_CONSTANT (*cexpr)
8609 2 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8610 2 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8611 4 : TREE_SIDE_EFFECTS (*cexpr)
8612 4 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8613 2 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8614 : }
8615 49 : c = TREE_CONSTANT (*cexpr);
8616 49 : s = TREE_SIDE_EFFECTS (*cexpr);
8617 : }
8618 : }
8619 91991451 : if (!c || s || activated_union_member_p)
8620 63746903 : for (tree *elt : ctors)
8621 : {
8622 12515414 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8623 0 : continue;
8624 12515414 : if (!c)
8625 10628129 : TREE_CONSTANT (*elt) = false;
8626 12515414 : if (s)
8627 0 : TREE_SIDE_EFFECTS (*elt) = true;
8628 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8629 : this union. */
8630 12515414 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8631 462694 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8632 : }
8633 :
8634 91991451 : if (lval)
8635 : return target;
8636 : else
8637 588206 : return init;
8638 94179530 : }
8639 :
8640 : /* Evaluate a ++ or -- expression. */
8641 :
8642 : static tree
8643 8696244 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8644 : value_cat lval,
8645 : bool *non_constant_p, bool *overflow_p,
8646 : tree *jump_target)
8647 : {
8648 8696244 : enum tree_code code = TREE_CODE (t);
8649 8696244 : tree type = TREE_TYPE (t);
8650 8696244 : tree op = TREE_OPERAND (t, 0);
8651 8696244 : tree offset = TREE_OPERAND (t, 1);
8652 8696244 : gcc_assert (TREE_CONSTANT (offset));
8653 :
8654 : /* OFFSET is constant, but perhaps not constant enough. We need to
8655 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8656 8696244 : offset = fold_simple (offset);
8657 :
8658 : /* The operand as an lvalue. */
8659 8696244 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8660 : non_constant_p, overflow_p,
8661 : jump_target);
8662 8696244 : if (*jump_target)
8663 : return NULL_TREE;
8664 :
8665 : /* The operand as an rvalue. */
8666 8696244 : tree val
8667 8696244 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8668 : non_constant_p, overflow_p,
8669 : jump_target);
8670 8696244 : if (*jump_target)
8671 : return NULL_TREE;
8672 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8673 : a local array in a constexpr function. */
8674 8696244 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8675 3516132 : if (!ptr)
8676 3516132 : VERIFY_CONSTANT (val);
8677 :
8678 : /* The modified value. */
8679 8636985 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8680 8636985 : tree mod;
8681 8636985 : if (INDIRECT_TYPE_P (type))
8682 : {
8683 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8684 5180112 : offset = convert_to_ptrofftype (offset);
8685 5180112 : if (!inc)
8686 80485 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8687 5180112 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8688 : }
8689 3456873 : else if (c_promoting_integer_type_p (type)
8690 39611 : && !TYPE_UNSIGNED (type)
8691 3457083 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8692 : {
8693 210 : offset = fold_convert (integer_type_node, offset);
8694 210 : mod = fold_convert (integer_type_node, val);
8695 241 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8696 : mod, offset);
8697 210 : mod = fold_convert (type, t);
8698 210 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8699 9 : TREE_OVERFLOW (mod) = false;
8700 : }
8701 : else
8702 3898136 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8703 8636985 : if (!ptr)
8704 3456873 : VERIFY_CONSTANT (mod);
8705 :
8706 : /* Storing the modified value. */
8707 14465548 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8708 : MODIFY_EXPR, type, op, mod);
8709 8636985 : mod = cxx_eval_constant_expression (ctx, store, lval,
8710 : non_constant_p, overflow_p,
8711 : jump_target);
8712 8636985 : ggc_free (store);
8713 8636985 : if (*jump_target)
8714 : return NULL_TREE;
8715 8636985 : if (*non_constant_p)
8716 : return t;
8717 :
8718 : /* And the value of the expression. */
8719 8563210 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8720 : /* Prefix ops are lvalues, but the caller might want an rvalue;
8721 : lval has already been taken into account in the store above. */
8722 : return mod;
8723 : else
8724 : /* Postfix ops are rvalues. */
8725 418511 : return val;
8726 : }
8727 :
8728 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8729 : STMT matches *jump_target. If we're looking for a case label and we see
8730 : the default label, note it in ctx->css_state. */
8731 :
8732 : static bool
8733 503430 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8734 : {
8735 503430 : switch (TREE_CODE (*jump_target))
8736 : {
8737 145 : case LABEL_DECL:
8738 145 : if (TREE_CODE (stmt) == LABEL_EXPR
8739 145 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8740 52 : return true;
8741 : break;
8742 :
8743 501368 : case INTEGER_CST:
8744 501368 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8745 : {
8746 501341 : gcc_assert (ctx->css_state != NULL);
8747 501341 : if (!CASE_LOW (stmt))
8748 : {
8749 : /* default: should appear just once in a SWITCH_EXPR
8750 : body (excluding nested SWITCH_EXPR). */
8751 76278 : gcc_assert (*ctx->css_state != css_default_seen);
8752 : /* When evaluating SWITCH_EXPR body for the second time,
8753 : return true for the default: label. */
8754 76278 : if (*ctx->css_state == css_default_processing)
8755 : return true;
8756 39015 : *ctx->css_state = css_default_seen;
8757 : }
8758 425063 : else if (CASE_HIGH (stmt))
8759 : {
8760 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8761 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8762 : return true;
8763 : }
8764 425043 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8765 : return true;
8766 : }
8767 : break;
8768 :
8769 : case BREAK_STMT:
8770 : case CONTINUE_STMT:
8771 : /* These two are handled directly in cxx_eval_loop_expr by testing
8772 : breaks (jump_target) or continues (jump_target). */
8773 : break;
8774 :
8775 : case VAR_DECL:
8776 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8777 : and other places by testing throws (jump_target). */
8778 : break;
8779 :
8780 0 : default:
8781 0 : gcc_unreachable ();
8782 : }
8783 : return false;
8784 : }
8785 :
8786 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8787 : semantics, for switch, break, continue, and return. */
8788 :
8789 : static tree
8790 48396260 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8791 : bool *non_constant_p, bool *overflow_p,
8792 : tree *jump_target)
8793 : {
8794 : /* In a statement-expression we want to return the last value.
8795 : For empty statement expression return void_node. */
8796 48396260 : tree r = void_node;
8797 127071929 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8798 : {
8799 104471062 : tree stmt = *i;
8800 :
8801 : /* We've found a continue, so skip everything until we reach
8802 : the label its jumping to. */
8803 104471062 : if (continues (jump_target))
8804 : {
8805 2062 : if (label_matches (ctx, jump_target, stmt))
8806 : /* Found it. */
8807 52 : *jump_target = NULL_TREE;
8808 : else
8809 2010 : continue;
8810 : }
8811 104469052 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8812 11273147 : continue;
8813 :
8814 93195905 : value_cat lval = vc_discard;
8815 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8816 131170541 : if (tsi_one_before_end_p (i)
8817 37975862 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8818 : lval = vc_prvalue;
8819 :
8820 93195905 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8821 : non_constant_p, overflow_p,
8822 : jump_target);
8823 93195901 : if (*non_constant_p)
8824 : break;
8825 83187098 : if (returns (jump_target)
8826 67489048 : || breaks (jump_target)
8827 78675669 : || throws (jump_target))
8828 : break;
8829 : }
8830 48396256 : return r;
8831 : }
8832 :
8833 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8834 : semantics; continue semantics are covered by cxx_eval_statement_list. */
8835 :
8836 : static tree
8837 6651765 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8838 : bool *non_constant_p, bool *overflow_p,
8839 : tree *jump_target)
8840 : {
8841 6651765 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8842 6651765 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8843 6651765 : unsigned cond_cleanup_depth = 0;
8844 6651765 : int count = 0;
8845 6651765 : switch (TREE_CODE (t))
8846 : {
8847 3237 : case LOOP_EXPR:
8848 3237 : body = LOOP_EXPR_BODY (t);
8849 3237 : break;
8850 5353842 : case DO_STMT:
8851 5353842 : body = DO_BODY (t);
8852 5353842 : cond = DO_COND (t);
8853 5353842 : break;
8854 365586 : case WHILE_STMT:
8855 365586 : body = WHILE_BODY (t);
8856 365586 : cond = WHILE_COND (t);
8857 365586 : cond_prep = WHILE_COND_PREP (t);
8858 365586 : cond_cleanup = WHILE_COND_CLEANUP (t);
8859 365586 : count = -1;
8860 365586 : break;
8861 929100 : case FOR_STMT:
8862 929100 : if (FOR_INIT_STMT (t))
8863 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8864 : non_constant_p, overflow_p, jump_target);
8865 929100 : if (*non_constant_p)
8866 : return NULL_TREE;
8867 929100 : body = FOR_BODY (t);
8868 929100 : cond = FOR_COND (t);
8869 929100 : expr = FOR_EXPR (t);
8870 929100 : cond_prep = FOR_COND_PREP (t);
8871 929100 : cond_cleanup = FOR_COND_CLEANUP (t);
8872 929100 : count = -1;
8873 929100 : break;
8874 0 : default:
8875 0 : gcc_unreachable ();
8876 : }
8877 6651765 : if (cond_prep)
8878 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8879 31362182 : auto cleanup_cond = [&] {
8880 : /* Clean up the condition variable after each iteration. */
8881 24710417 : if (cond_cleanup_depth && !*non_constant_p)
8882 : {
8883 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8884 105 : tree s = BIND_EXPR_BODY (cond_prep);
8885 105 : unsigned i;
8886 210 : for (i = cond_cleanup_depth; i; --i)
8887 : {
8888 105 : tree_stmt_iterator iter = tsi_last (s);
8889 105 : s = tsi_stmt (iter);
8890 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8891 105 : s = CLEANUP_BODY (s);
8892 : }
8893 105 : tree c;
8894 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8895 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8896 : overflow_p, jump_target);
8897 105 : }
8898 24710417 : if (cond_prep)
8899 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8900 222 : decl; decl = DECL_CHAIN (decl))
8901 111 : destroy_value_checked (ctx, decl, non_constant_p);
8902 6651765 : };
8903 19388609 : do
8904 : {
8905 19388609 : if (count != -1)
8906 : {
8907 18093923 : if (body)
8908 18093923 : cxx_eval_constant_expression (ctx, body, vc_discard,
8909 : non_constant_p, overflow_p,
8910 : jump_target);
8911 18093923 : if (breaks (jump_target))
8912 : {
8913 35271 : *jump_target = NULL_TREE;
8914 35271 : break;
8915 : }
8916 :
8917 18058652 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8918 570 : *jump_target = NULL_TREE;
8919 :
8920 18058652 : if (expr)
8921 5451331 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8922 : non_constant_p, overflow_p,
8923 : jump_target);
8924 18058652 : cleanup_cond ();
8925 : }
8926 :
8927 19353338 : if (cond_prep)
8928 : {
8929 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8930 222 : decl; decl = DECL_CHAIN (decl))
8931 111 : ctx->global->clear_value (decl);
8932 111 : if (cond_cleanup)
8933 : {
8934 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8935 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8936 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8937 : at the end of those STATEMENT_LISTs. */
8938 111 : cond_cleanup_depth = 0;
8939 111 : tree s = BIND_EXPR_BODY (cond_prep);
8940 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8941 222 : depth; --depth)
8942 : {
8943 111 : for (tree_stmt_iterator i = tsi_start (s);
8944 321 : !tsi_end_p (i); ++i)
8945 : {
8946 321 : tree stmt = *i;
8947 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8948 0 : continue;
8949 321 : if (tsi_one_before_end_p (i))
8950 : {
8951 : /* The last statement in the STATEMENT_LIST
8952 : has to be a CLEANUP_STMT (verified in
8953 : finish_loop_cond_prep). We want to
8954 : evaluate just its CLEANUP_BODY part but not
8955 : CLEANUP_EXPR part just yet. */
8956 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8957 : /* If the CLEANUP_STMT is not actually to be
8958 : evaluated, don't increment cond_cleanup_depth
8959 : so that we don't evaluate the CLEANUP_EXPR
8960 : for it later either. */
8961 105 : if (*jump_target)
8962 : {
8963 : depth = 1;
8964 : break;
8965 : }
8966 105 : ++cond_cleanup_depth;
8967 : /* If not in the innermost one, next iteration
8968 : will handle CLEANUP_BODY similarly. */
8969 105 : if (depth > 1)
8970 : {
8971 0 : s = CLEANUP_BODY (stmt);
8972 0 : break;
8973 : }
8974 : /* The innermost one can be evaluated normally. */
8975 105 : cxx_eval_constant_expression (ctx,
8976 105 : CLEANUP_BODY (stmt),
8977 : vc_discard,
8978 : non_constant_p,
8979 : overflow_p,
8980 : jump_target);
8981 105 : break;
8982 : }
8983 : /* And so should be evaluated statements which aren't
8984 : last in the STATEMENT_LIST. */
8985 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8986 : non_constant_p, overflow_p,
8987 : jump_target);
8988 216 : if (*non_constant_p
8989 210 : || returns (jump_target)
8990 210 : || breaks (jump_target)
8991 210 : || continues (jump_target)
8992 531 : || throws (jump_target))
8993 : {
8994 : depth = 1;
8995 : break;
8996 : }
8997 : }
8998 : }
8999 : }
9000 : else
9001 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
9002 : vc_discard, non_constant_p,
9003 : overflow_p, jump_target);
9004 : }
9005 :
9006 19353338 : if (cond)
9007 : {
9008 19346019 : tree res
9009 19346019 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9010 : non_constant_p, overflow_p,
9011 : jump_target);
9012 19346019 : if (res)
9013 : {
9014 19290720 : if (verify_constant (res, ctx->quiet, non_constant_p,
9015 : overflow_p))
9016 : break;
9017 19105610 : if (integer_zerop (res))
9018 : break;
9019 : }
9020 : else
9021 55299 : gcc_assert (*jump_target);
9022 : }
9023 :
9024 12794016 : if (++count >= constexpr_loop_limit)
9025 : {
9026 27 : if (!ctx->quiet)
9027 9 : error_at (cp_expr_loc_or_input_loc (t),
9028 : "%<constexpr%> loop iteration count exceeds limit of %d "
9029 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
9030 : constexpr_loop_limit);
9031 27 : *non_constant_p = true;
9032 27 : break;
9033 : }
9034 : }
9035 38325045 : while (!returns (jump_target)
9036 12737067 : && !breaks (jump_target)
9037 12737067 : && !continues (jump_target)
9038 12737160 : && (!switches (jump_target) || count == 0)
9039 12737037 : && !throws (jump_target)
9040 25588133 : && !*non_constant_p);
9041 :
9042 6651765 : cleanup_cond ();
9043 :
9044 6651765 : return NULL_TREE;
9045 : }
9046 :
9047 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
9048 : semantics. */
9049 :
9050 : static tree
9051 49991 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
9052 : bool *non_constant_p, bool *overflow_p,
9053 : tree *jump_target)
9054 : {
9055 49991 : tree cond
9056 49991 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
9057 49991 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9058 : non_constant_p, overflow_p,
9059 : jump_target);
9060 49991 : if (*jump_target)
9061 : return NULL_TREE;
9062 49991 : VERIFY_CONSTANT (cond);
9063 49698 : if (TREE_CODE (cond) != INTEGER_CST)
9064 : {
9065 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
9066 : switch condition even if it's constant enough for other things
9067 : (c++/113545). */
9068 0 : gcc_checking_assert (ctx->quiet);
9069 0 : *non_constant_p = true;
9070 0 : return t;
9071 : }
9072 :
9073 49698 : *jump_target = cond;
9074 :
9075 49698 : tree body
9076 49698 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
9077 49698 : constexpr_ctx new_ctx = *ctx;
9078 49698 : constexpr_switch_state css = css_default_not_seen;
9079 49698 : new_ctx.css_state = &css;
9080 49698 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
9081 : non_constant_p, overflow_p, jump_target);
9082 87001 : if (switches (jump_target) && css == css_default_seen)
9083 : {
9084 : /* If the SWITCH_EXPR body has default: label, process it once again,
9085 : this time instructing label_matches to return true for default:
9086 : label on switches (jump_target). */
9087 37263 : css = css_default_processing;
9088 37263 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
9089 : non_constant_p, overflow_p, jump_target);
9090 : }
9091 49698 : if (breaks (jump_target) || switches (jump_target))
9092 21419 : *jump_target = NULL_TREE;
9093 : return NULL_TREE;
9094 : }
9095 :
9096 : /* Find the object of TYPE under initialization in CTX. */
9097 :
9098 : static tree
9099 16714 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
9100 : {
9101 16714 : if (!ctx)
9102 : return NULL_TREE;
9103 :
9104 : /* Prefer the outermost matching object, but don't cross
9105 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
9106 16318 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
9107 574 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
9108 : return outer_ob;
9109 :
9110 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
9111 : can is a minor optimization. */
9112 16140 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
9113 300 : return ctx->ctor;
9114 :
9115 15840 : if (!ctx->object)
9116 : return NULL_TREE;
9117 :
9118 : /* Since an object cannot have a field of its own type, we can search outward
9119 : from ctx->object to find the unique containing object of TYPE. */
9120 : tree ob = ctx->object;
9121 15816 : while (ob)
9122 : {
9123 15816 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
9124 : break;
9125 219 : if (handled_component_p (ob))
9126 217 : ob = TREE_OPERAND (ob, 0);
9127 : else
9128 : ob = NULL_TREE;
9129 : }
9130 :
9131 : /* Like in replace_placeholders_r. */
9132 15599 : return unshare_expr (ob);
9133 : }
9134 :
9135 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
9136 : true, we're checking a constexpr function body. */
9137 :
9138 : static void
9139 35 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
9140 : {
9141 35 : auto_diagnostic_group d;
9142 35 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
9143 : "constant expression"))
9144 35 : inform (loc, "only unevaluated inline assembly is allowed in a "
9145 : "%<constexpr%> function in C++20");
9146 35 : }
9147 :
9148 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
9149 : context; maybe complain about that. */
9150 :
9151 : static void
9152 92325528 : maybe_warn_about_constant_value (location_t loc, tree decl)
9153 : {
9154 92325528 : static bool explained = false;
9155 92325528 : auto_diagnostic_group d;
9156 92325528 : if (cxx_dialect >= cxx17
9157 92156441 : && warn_interference_size
9158 92156441 : && !OPTION_SET_P (param_destruct_interfere_size)
9159 92156441 : && DECL_CONTEXT (decl) == std_node
9160 19022415 : && DECL_NAME (decl)
9161 19022409 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
9162 16 : && (LOCATION_FILE (input_location) != main_input_filename
9163 10 : || module_exporting_p ())
9164 92325531 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
9165 92325537 : && !explained)
9166 : {
9167 6 : explained = true;
9168 6 : inform (loc, "its value can vary between compiler versions or "
9169 : "with different %<-mtune%> or %<-mcpu%> flags");
9170 6 : inform (loc, "if this use is part of a public ABI, change it to "
9171 : "instead use a constant variable you define");
9172 6 : inform (loc, "the default value for the current CPU tuning "
9173 : "is %d bytes", param_destruct_interfere_size);
9174 6 : inform (loc, "you can stabilize this value with %<--param "
9175 : "hardware_destructive_interference_size=%d%>, or disable "
9176 : "this warning with %<-Wno-interference-size%>",
9177 : param_destruct_interfere_size);
9178 : }
9179 92325528 : }
9180 :
9181 : /* For element type ELT_TYPE, return the appropriate type of the heap object
9182 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
9183 : in bytes. If COOKIE_SIZE is NULL, return array type
9184 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
9185 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
9186 : where N is computed such that the size of the struct fits into FULL_SIZE.
9187 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
9188 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
9189 : will be also 0 and so it is not possible to determine the actual array
9190 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
9191 : expression evaluation of subexpressions of ARG_SIZE. */
9192 :
9193 : static tree
9194 118802 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9195 : tree cookie_size, tree full_size, tree arg_size,
9196 : bool *non_constant_p, bool *overflow_p,
9197 : tree *jump_target)
9198 : {
9199 118802 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9200 118802 : gcc_assert (tree_fits_uhwi_p (full_size));
9201 118802 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9202 118802 : if (arg_size)
9203 : {
9204 9 : STRIP_NOPS (arg_size);
9205 9 : if (cookie_size)
9206 : {
9207 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9208 : arg_size = NULL_TREE;
9209 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9210 0 : && tree_int_cst_equal (cookie_size,
9211 0 : TREE_OPERAND (arg_size, 0)))
9212 : {
9213 0 : arg_size = TREE_OPERAND (arg_size, 1);
9214 0 : STRIP_NOPS (arg_size);
9215 : }
9216 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9217 0 : && tree_int_cst_equal (cookie_size,
9218 0 : TREE_OPERAND (arg_size, 1)))
9219 : {
9220 0 : arg_size = TREE_OPERAND (arg_size, 0);
9221 0 : STRIP_NOPS (arg_size);
9222 : }
9223 : else
9224 : arg_size = NULL_TREE;
9225 : }
9226 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9227 : {
9228 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9229 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9230 9 : if (integer_zerop (op0))
9231 9 : arg_size
9232 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9233 : non_constant_p, overflow_p,
9234 : jump_target);
9235 0 : else if (integer_zerop (op1))
9236 0 : arg_size
9237 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9238 : non_constant_p, overflow_p,
9239 : jump_target);
9240 : else
9241 : arg_size = NULL_TREE;
9242 9 : if (*jump_target)
9243 : return NULL_TREE;
9244 : }
9245 : else
9246 : arg_size = NULL_TREE;
9247 : }
9248 :
9249 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9250 118802 : if (!arg_size)
9251 : {
9252 118793 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9253 118793 : gcc_assert (fsz >= csz);
9254 118793 : fsz -= csz;
9255 118793 : if (esz)
9256 118793 : fsz /= esz;
9257 : }
9258 118802 : tree itype2 = build_index_type (size_int (fsz - 1));
9259 118802 : if (!cookie_size)
9260 118793 : return build_cplus_array_type (elt_type, itype2);
9261 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9262 : }
9263 :
9264 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9265 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9266 : the expression which needed the cleanup threw. If both threw, diagnose
9267 : it and return NULL, otherwise return R. If only the cleanup threw, set
9268 : *JUMP_TARGET to the exception object from the cleanup. */
9269 :
9270 : static tree
9271 1762091 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9272 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9273 : {
9274 1762091 : if (!throws (&jmp_target))
9275 : return r;
9276 14 : if (throws (jump_target))
9277 : {
9278 : /* [except.throw]/9 - If the exception handling mechanism
9279 : handling an uncaught exception directly invokes a function
9280 : that exits via an exception, the function std::terminate is
9281 : invoked. */
9282 12 : if (!ctx->quiet)
9283 : {
9284 4 : auto_diagnostic_group d;
9285 4 : diagnose_std_terminate (loc, ctx, *jump_target);
9286 4 : inform (loc, "destructor exited with an exception");
9287 4 : }
9288 12 : *non_constant_p = true;
9289 12 : *jump_target = NULL_TREE;
9290 12 : return NULL_TREE;
9291 : }
9292 2 : *jump_target = jmp_target;
9293 2 : return r;
9294 : }
9295 :
9296 : /* Attempt to reduce the expression T to a constant value.
9297 : On failure, issue diagnostic and return error_mark_node. */
9298 : /* FIXME unify with c_fully_fold */
9299 : /* FIXME overflow_p is too global */
9300 :
9301 : tree
9302 3003556530 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9303 : value_cat lval,
9304 : bool *non_constant_p, bool *overflow_p,
9305 : tree *jump_target)
9306 : {
9307 3003556530 : if (*jump_target)
9308 : {
9309 : /* If we are jumping, ignore all statements/expressions except those
9310 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9311 1619996 : switch (TREE_CODE (t))
9312 : {
9313 : case BIND_EXPR:
9314 : case STATEMENT_LIST:
9315 : case LOOP_EXPR:
9316 : case COND_EXPR:
9317 : case IF_STMT:
9318 : case DO_STMT:
9319 : case WHILE_STMT:
9320 : case FOR_STMT:
9321 : break;
9322 501368 : case LABEL_EXPR:
9323 501368 : case CASE_LABEL_EXPR:
9324 501368 : if (label_matches (ctx, jump_target, t))
9325 : /* Found it. */
9326 49658 : *jump_target = NULL_TREE;
9327 : return NULL_TREE;
9328 : default:
9329 : return NULL_TREE;
9330 : }
9331 : }
9332 3002182242 : if (error_operand_p (t))
9333 : {
9334 140 : *non_constant_p = true;
9335 140 : return t;
9336 : }
9337 :
9338 : /* Change the input location to the currently processed expression for
9339 : better error messages when a subexpression has no location. */
9340 3002182102 : location_t loc = cp_expr_loc_or_input_loc (t);
9341 6004358772 : iloc_sentinel sentinel (loc);
9342 :
9343 3002182102 : STRIP_ANY_LOCATION_WRAPPER (t);
9344 :
9345 3002182102 : if (CONSTANT_CLASS_P (t))
9346 : {
9347 451904385 : if (TREE_OVERFLOW (t))
9348 : {
9349 62 : if (!ctx->quiet)
9350 15 : permerror (input_location, "overflow in constant expression");
9351 62 : if (!flag_permissive || ctx->quiet)
9352 59 : *overflow_p = true;
9353 : }
9354 :
9355 451904385 : if (TREE_CODE (t) == INTEGER_CST
9356 429563623 : && TYPE_PTR_P (TREE_TYPE (t))
9357 : /* INTEGER_CST with pointer-to-method type is only used
9358 : for a virtual method in a pointer to member function.
9359 : Don't reject those. */
9360 2114188 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9361 454018249 : && !integer_zerop (t))
9362 : {
9363 6 : if (!ctx->quiet)
9364 0 : error ("value %qE of type %qT is not a constant expression",
9365 0 : t, TREE_TYPE (t));
9366 6 : *non_constant_p = true;
9367 : }
9368 :
9369 : return t;
9370 : }
9371 :
9372 : /* Avoid excessively long constexpr evaluations. */
9373 2550277717 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9374 : {
9375 9 : if (!ctx->quiet)
9376 3 : error_at (loc,
9377 : "%<constexpr%> evaluation operation count exceeds limit of "
9378 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9379 : constexpr_ops_limit);
9380 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9381 9 : *non_constant_p = true;
9382 9 : return t;
9383 : }
9384 :
9385 2550277708 : constexpr_ctx new_ctx;
9386 2550277708 : tree r = t;
9387 :
9388 2550277708 : tree_code tcode = TREE_CODE (t);
9389 2550277708 : switch (tcode)
9390 : {
9391 59339121 : case RESULT_DECL:
9392 59339121 : if (lval)
9393 : return t;
9394 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9395 : through an invisible reference, or in named return value
9396 : optimization. */
9397 147136 : if (tree v = ctx->global->get_value (t))
9398 : return v;
9399 : else
9400 : {
9401 3 : if (!ctx->quiet)
9402 0 : error ("%qE is not a constant expression", t);
9403 3 : *non_constant_p = true;
9404 : }
9405 3 : break;
9406 :
9407 298662443 : case VAR_DECL:
9408 298662443 : if (DECL_HAS_VALUE_EXPR_P (t))
9409 : {
9410 9232689 : if (is_normal_capture_proxy (t)
9411 9232689 : && current_function_decl == DECL_CONTEXT (t))
9412 : {
9413 : /* Function parms aren't constexpr within the function
9414 : definition, so don't try to look at the closure. But if the
9415 : captured variable is constant, try to evaluate it directly. */
9416 628739 : r = DECL_CAPTURED_VARIABLE (t);
9417 : }
9418 : else
9419 8603950 : r = DECL_VALUE_EXPR (t);
9420 :
9421 9232689 : tree type = TREE_TYPE (t);
9422 9232689 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
9423 : {
9424 : /* Adjust r to match the reference-ness of t. */
9425 257835 : if (TYPE_REF_P (type))
9426 253644 : r = build_address (r);
9427 : else
9428 4191 : r = convert_from_reference (r);
9429 : }
9430 9232689 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
9431 9232689 : overflow_p, jump_target);
9432 : }
9433 : /* fall through */
9434 300201176 : case CONST_DECL:
9435 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
9436 : CONST_DECL for aggregate constants. */
9437 300201176 : if (lval)
9438 : return t;
9439 213349671 : else if (t == ctx->object)
9440 1024705 : return ctx->ctor;
9441 212324966 : if (VAR_P (t))
9442 : {
9443 201553544 : if (tree v = ctx->global->get_value (t))
9444 : {
9445 : r = v;
9446 : break;
9447 : }
9448 136433274 : if (ctx->global->is_outside_lifetime (t))
9449 : {
9450 137 : if (!ctx->quiet)
9451 39 : outside_lifetime_error (loc, t);
9452 137 : *non_constant_p = true;
9453 137 : break;
9454 : }
9455 : }
9456 147204559 : if (ctx->manifestly_const_eval == mce_true)
9457 92325528 : maybe_warn_about_constant_value (loc, t);
9458 147204559 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
9459 147204559 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9460 : {
9461 : /* If the class is empty, we aren't actually loading anything. */
9462 252924 : r = build_constructor (TREE_TYPE (t), NULL);
9463 252924 : TREE_CONSTANT (r) = true;
9464 : }
9465 146951635 : else if (ctx->strict)
9466 146602798 : r = decl_really_constant_value (t, /*unshare_p=*/false);
9467 : else
9468 348837 : r = decl_constant_value (t, /*unshare_p=*/false);
9469 147201862 : if (TREE_CODE (r) == TARGET_EXPR
9470 147201862 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9471 0 : r = TARGET_EXPR_INITIAL (r);
9472 147201862 : if (DECL_P (r)
9473 : /* P2280 allows references to unknown. */
9474 147489558 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
9475 : {
9476 34549173 : if (!ctx->quiet)
9477 183 : non_const_var_error (loc, r, /*fundef_p*/false);
9478 34549173 : *non_constant_p = true;
9479 : }
9480 : break;
9481 :
9482 : case DEBUG_BEGIN_STMT:
9483 : /* ??? It might be nice to retain this information somehow, so
9484 : as to be able to step into a constexpr function call. */
9485 : /* Fall through. */
9486 :
9487 : case FUNCTION_DECL:
9488 : case TEMPLATE_DECL:
9489 : case LABEL_DECL:
9490 : case LABEL_EXPR:
9491 : case CASE_LABEL_EXPR:
9492 : case PREDICT_EXPR:
9493 : case OMP_DECLARE_MAPPER:
9494 : case REFLECT_EXPR:
9495 : return t;
9496 :
9497 304341453 : case PARM_DECL:
9498 304341453 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9499 : {
9500 : /* glvalue use. */
9501 21213595 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9502 219727 : if (tree v = ctx->global->get_value (t))
9503 2051876752 : r = v;
9504 : }
9505 283127858 : else if (tree v = ctx->global->get_value (t))
9506 : {
9507 164766074 : r = v;
9508 164766074 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9509 11305 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9510 : non_constant_p, overflow_p,
9511 : jump_target);
9512 164766074 : if (*jump_target)
9513 : return NULL_TREE;
9514 : }
9515 118361784 : else if (lval)
9516 : /* Defer in case this is only used for its type. */;
9517 118361784 : else if (ctx->global->is_outside_lifetime (t))
9518 : {
9519 18 : if (!ctx->quiet)
9520 6 : outside_lifetime_error (loc, t);
9521 18 : *non_constant_p = true;
9522 18 : break;
9523 : }
9524 118361766 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9525 118361766 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9526 : {
9527 : /* If the class is empty, we aren't actually loading anything. */
9528 41703 : r = build_constructor (TREE_TYPE (t), NULL);
9529 41703 : TREE_CONSTANT (r) = true;
9530 : }
9531 118320063 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9532 : /* P2280 allows references to unknown... */;
9533 118039599 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9534 : /* ...as well as the this pointer. */;
9535 : else
9536 : {
9537 117581269 : if (!ctx->quiet)
9538 196 : error ("%qE is not a constant expression", t);
9539 117581269 : *non_constant_p = true;
9540 : }
9541 : break;
9542 :
9543 206705676 : case CALL_EXPR:
9544 206705676 : case AGGR_INIT_EXPR:
9545 206705676 : r = cxx_eval_call_expression (ctx, t, lval,
9546 : non_constant_p, overflow_p, jump_target);
9547 206705676 : break;
9548 :
9549 21501298 : case DECL_EXPR:
9550 21501298 : {
9551 21501298 : r = DECL_EXPR_DECL (t);
9552 21501298 : if (TREE_CODE (r) == USING_DECL)
9553 : {
9554 9422 : r = void_node;
9555 9422 : break;
9556 : }
9557 :
9558 21491876 : if (VAR_P (r)
9559 21491876 : && (TREE_STATIC (r)
9560 21331260 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9561 : /* Allow __FUNCTION__ etc. */
9562 160616 : && !DECL_ARTIFICIAL (r)
9563 21492773 : && !decl_constant_var_p (r))
9564 : {
9565 7 : if (!ctx->quiet)
9566 : {
9567 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9568 1 : error_at (loc, "control passes through definition of %qD "
9569 : "with thread storage duration", r);
9570 : else
9571 1 : error_at (loc, "control passes through definition of %qD "
9572 : "with static storage duration", r);
9573 : }
9574 7 : *non_constant_p = true;
9575 7 : break;
9576 : }
9577 :
9578 : /* make_rtl_for_nonlocal_decl could have deferred emission of
9579 : a local static var, but if it appears in a statement expression
9580 : which is constant expression evaluated to e.g. just the address
9581 : of the variable, its DECL_EXPR will never be seen during
9582 : gimple lowering's record_vars_into as the statement expression
9583 : will not be in the IL at all. */
9584 21491869 : if (VAR_P (r)
9585 21491869 : && TREE_STATIC (r)
9586 160609 : && !DECL_REALLY_EXTERN (r)
9587 160587 : && DECL_FUNCTION_SCOPE_P (r)
9588 160587 : && !var_in_maybe_constexpr_fn (r)
9589 21491872 : && decl_constant_var_p (r))
9590 : {
9591 3 : varpool_node *node = varpool_node::get (r);
9592 3 : if (node == NULL || !node->definition)
9593 3 : rest_of_decl_compilation (r, 0, at_eof);
9594 : }
9595 :
9596 42743989 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9597 40087408 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9598 : {
9599 2899294 : new_ctx = *ctx;
9600 2899294 : new_ctx.object = r;
9601 2899294 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9602 2899294 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9603 2899294 : ctx->global->put_value (r, new_ctx.ctor);
9604 2899294 : ctx = &new_ctx;
9605 : }
9606 :
9607 21491869 : if (tree init = DECL_INITIAL (r))
9608 : {
9609 11225872 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9610 : non_constant_p, overflow_p,
9611 : jump_target);
9612 11225872 : if (*jump_target)
9613 : return NULL_TREE;
9614 : /* Don't share a CONSTRUCTOR that might be changed. */
9615 11225872 : init = unshare_constructor (init);
9616 : /* Remember that a constant object's constructor has already
9617 : run. */
9618 22451744 : if (CLASS_TYPE_P (TREE_TYPE (r))
9619 11775044 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9620 71489 : TREE_READONLY (init) = true;
9621 11225872 : ctx->global->put_value (r, init);
9622 : }
9623 10265997 : else if (ctx == &new_ctx)
9624 : /* We gave it a CONSTRUCTOR above. */;
9625 : else
9626 7992504 : ctx->global->put_value (r, NULL_TREE);
9627 : }
9628 : break;
9629 :
9630 27065074 : case TARGET_EXPR:
9631 27065074 : {
9632 27065074 : tree type = TREE_TYPE (t);
9633 :
9634 27065074 : if (!literal_type_p (type))
9635 : {
9636 6597 : if (!ctx->quiet)
9637 : {
9638 0 : auto_diagnostic_group d;
9639 0 : error ("temporary of non-literal type %qT in a "
9640 : "constant expression", type);
9641 0 : explain_non_literal_class (type);
9642 0 : }
9643 6597 : *non_constant_p = true;
9644 11809580 : break;
9645 : }
9646 27058477 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9647 : /* Avoid evaluating a TARGET_EXPR more than once. */
9648 27058477 : tree slot = TARGET_EXPR_SLOT (t);
9649 27058477 : if (tree v = ctx->global->get_value (slot))
9650 : {
9651 2136 : if (lval)
9652 10953534 : return slot;
9653 : r = v;
9654 : break;
9655 : }
9656 27056341 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9657 : {
9658 : /* We're being expanded without an explicit target, so start
9659 : initializing a new object; expansion with an explicit target
9660 : strips the TARGET_EXPR before we get here. */
9661 20510753 : new_ctx = *ctx;
9662 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9663 : any PLACEHOLDER_EXPR within the initializer that refers to the
9664 : former object under construction. */
9665 20510753 : new_ctx.parent = ctx;
9666 20510753 : new_ctx.ctor = build_constructor (type, NULL);
9667 20510753 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9668 20510753 : new_ctx.object = slot;
9669 20510753 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9670 20510753 : ctx = &new_ctx;
9671 : }
9672 :
9673 : /* If the initializer is complex, evaluate it to initialize slot. */
9674 27056341 : bool is_complex = target_expr_needs_replace (t);
9675 27056341 : if (is_complex)
9676 : /* In case no initialization actually happens, clear out any
9677 : void_node from a previous evaluation. */
9678 483 : ctx->global->put_value (slot, NULL_TREE);
9679 :
9680 : /* Pass vc_prvalue because this indicates
9681 : initialization of a temporary. */
9682 27056341 : r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t),
9683 : vc_prvalue, non_constant_p,
9684 : overflow_p, jump_target);
9685 27056341 : if (*non_constant_p)
9686 : break;
9687 15254444 : if (ctx->save_exprs)
9688 10107942 : ctx->save_exprs->safe_push (slot);
9689 15254444 : if (*jump_target)
9690 : {
9691 561 : if (!is_complex
9692 561 : && !(AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9693 : /* If TARGET_EXPR_INITIAL throws exception and slot's value
9694 : has not been changed yet, CLEANUP_POINT_EXPR handling
9695 : could see there void_node from a previous evaluation
9696 : and complain. */
9697 6 : ctx->global->put_value (slot, NULL_TREE);
9698 : return NULL_TREE;
9699 : }
9700 15253883 : if (!is_complex)
9701 : {
9702 15253478 : r = unshare_constructor (r);
9703 : /* Adjust the type of the result to the type of the temporary. */
9704 15253478 : r = adjust_temp_type (type, r);
9705 15253478 : ctx->global->put_value (slot, r);
9706 : }
9707 15253883 : if (TARGET_EXPR_CLEANUP (t)
9708 15253883 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9709 : {
9710 1458759 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9711 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9712 : them. */
9713 1458759 : if (CLEANUP_EH_ONLY (t))
9714 1896 : ctx->global->cleanups->safe_push (NULL_TREE);
9715 : }
9716 15253883 : if (lval)
9717 : return slot;
9718 4301960 : if (is_complex)
9719 0 : r = ctx->global->get_value (slot);
9720 : }
9721 4301960 : break;
9722 :
9723 103344626 : case INIT_EXPR:
9724 103344626 : case MODIFY_EXPR:
9725 103344626 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9726 103344626 : r = cxx_eval_store_expression (ctx, t, lval,
9727 : non_constant_p, overflow_p, jump_target);
9728 103344626 : break;
9729 :
9730 0 : case SCOPE_REF:
9731 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9732 : lval,
9733 : non_constant_p, overflow_p,
9734 : jump_target);
9735 0 : break;
9736 :
9737 64285807 : case RETURN_EXPR:
9738 64285807 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9739 63941006 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9740 : lval,
9741 : non_constant_p, overflow_p,
9742 : jump_target);
9743 64285803 : if (!throws (jump_target))
9744 64285451 : *jump_target = t;
9745 : break;
9746 54013 : case BREAK_STMT:
9747 54013 : case CONTINUE_STMT:
9748 54013 : *jump_target = t;
9749 54013 : break;
9750 :
9751 960265 : case SAVE_EXPR:
9752 : /* Avoid evaluating a SAVE_EXPR more than once. */
9753 960265 : if (tree v = ctx->global->get_value (t))
9754 : r = v;
9755 : else
9756 : {
9757 938344 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9758 : vc_prvalue, non_constant_p,
9759 : overflow_p, jump_target);
9760 938344 : if (*non_constant_p || *jump_target)
9761 : break;
9762 12741 : ctx->global->put_value (t, r);
9763 12741 : if (ctx->save_exprs)
9764 9205 : ctx->save_exprs->safe_push (t);
9765 : }
9766 : break;
9767 :
9768 62782859 : case NON_LVALUE_EXPR:
9769 62782859 : case EXPR_STMT:
9770 62782859 : case EH_SPEC_BLOCK:
9771 62782859 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9772 : lval,
9773 : non_constant_p, overflow_p,
9774 : jump_target);
9775 62782859 : break;
9776 :
9777 9232 : case TRY_BLOCK:
9778 9232 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9779 : non_constant_p, overflow_p,
9780 : jump_target);
9781 9232 : if (!*non_constant_p && throws (jump_target))
9782 3611 : if (tree h = TRY_HANDLERS (t))
9783 : {
9784 3611 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9785 3611 : if (TREE_CODE (h) == STATEMENT_LIST)
9786 : {
9787 938 : for (tree stmt : tsi_range (h))
9788 904 : if (TREE_CODE (stmt) == HANDLER
9789 904 : && handler_match_for_exception_type (stmt, type))
9790 : {
9791 : h = stmt;
9792 : break;
9793 : }
9794 462 : if (TREE_CODE (h) == STATEMENT_LIST)
9795 : h = NULL_TREE;
9796 : }
9797 3149 : else if (TREE_CODE (h) != HANDLER
9798 3149 : || !handler_match_for_exception_type (h, type))
9799 : h = NULL_TREE;
9800 : if (h)
9801 : {
9802 3577 : gcc_assert (VAR_P (*jump_target));
9803 3577 : ctx->global->caught_exceptions.safe_push (*jump_target);
9804 3577 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9805 3577 : *jump_target = NULL_TREE;
9806 3577 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9807 : vc_discard, non_constant_p,
9808 : overflow_p, jump_target);
9809 : }
9810 : }
9811 : break;
9812 :
9813 91632452 : case CLEANUP_POINT_EXPR:
9814 91632452 : {
9815 91632452 : auto_vec<tree, 2> cleanups;
9816 91632452 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9817 91632452 : ctx->global->cleanups = &cleanups;
9818 :
9819 91632452 : auto_vec<tree, 10> save_exprs;
9820 91632452 : constexpr_ctx new_ctx = *ctx;
9821 91632452 : new_ctx.save_exprs = &save_exprs;
9822 :
9823 91632452 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9824 : lval,
9825 : non_constant_p, overflow_p,
9826 : jump_target);
9827 :
9828 91632450 : ctx->global->cleanups = prev_cleanups;
9829 91632450 : unsigned int i;
9830 91632450 : tree cleanup, jmp_target = NULL_TREE;
9831 91632450 : bool eh = throws (jump_target);
9832 : /* Evaluate the cleanups. */
9833 184621245 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9834 1356345 : if (cleanup == NULL_TREE)
9835 : {
9836 : /* NULL_TREE cleanup is a marker that before it is
9837 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9838 : if the body didn't throw. */
9839 1873 : if (!eh)
9840 1871 : --i;
9841 : }
9842 : else
9843 1354472 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9844 : non_constant_p, overflow_p,
9845 : &jmp_target);
9846 :
9847 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9848 : full-expression. */
9849 285014497 : for (tree save_expr : save_exprs)
9850 10117147 : destroy_value_checked (ctx, save_expr, non_constant_p);
9851 91632450 : if (throws (&jmp_target))
9852 0 : *jump_target = jmp_target;
9853 91632450 : }
9854 91632450 : break;
9855 :
9856 58794856 : case MUST_NOT_THROW_EXPR:
9857 58794856 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9858 : lval,
9859 : non_constant_p, overflow_p,
9860 : jump_target);
9861 58794854 : if (throws (jump_target))
9862 : {
9863 : /* [except.handle]/7 - If the search for a handler exits the
9864 : function body of a function with a non-throwing exception
9865 : specification, the function std::terminate is invoked. */
9866 54 : if (!ctx->quiet)
9867 : {
9868 18 : auto_diagnostic_group d;
9869 18 : diagnose_std_terminate (loc, ctx, *jump_target);
9870 18 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9871 14 : && ctx->call
9872 32 : && ctx->call->fundef)
9873 14 : inform (loc, "uncaught exception exited from %<noexcept%> "
9874 : "function %qD",
9875 : ctx->call->fundef->decl);
9876 4 : else if (MUST_NOT_THROW_THROW_P (t))
9877 2 : inform (loc, "destructor exited with an exception after "
9878 : "initializing the exception object");
9879 2 : else if (MUST_NOT_THROW_CATCH_P (t))
9880 2 : inform (loc, "constructor exited with another exception while "
9881 : "entering handler");
9882 18 : }
9883 54 : *non_constant_p = true;
9884 54 : *jump_target = NULL_TREE;
9885 54 : r = NULL_TREE;
9886 : }
9887 : break;
9888 :
9889 0 : case TRY_CATCH_EXPR:
9890 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9891 : {
9892 0 : r = void_node;
9893 0 : break;
9894 : }
9895 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9896 : non_constant_p, overflow_p,
9897 : jump_target);
9898 0 : if (!*non_constant_p && throws (jump_target))
9899 : {
9900 0 : tree jmp_target = NULL_TREE;
9901 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9902 : non_constant_p, overflow_p,
9903 : &jmp_target);
9904 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9905 : jmp_target);
9906 : }
9907 : break;
9908 :
9909 842 : case TRY_FINALLY_EXPR:
9910 842 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9911 : non_constant_p, overflow_p,
9912 : jump_target);
9913 842 : if (!*non_constant_p)
9914 : {
9915 827 : tree jmp_target = NULL_TREE;
9916 : /* Also evaluate the cleanup. */
9917 827 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9918 827 : && throws (jump_target))
9919 0 : cxx_eval_constant_expression (ctx,
9920 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9921 : 1), vc_discard,
9922 : non_constant_p, overflow_p,
9923 : &jmp_target);
9924 : else
9925 827 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9926 : non_constant_p, overflow_p,
9927 : &jmp_target);
9928 827 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9929 : jmp_target);
9930 : }
9931 : break;
9932 :
9933 0 : case EH_ELSE_EXPR:
9934 : /* Evaluate any cleanup that applies to non-EH exits. */
9935 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9936 : non_constant_p, overflow_p,
9937 : jump_target);
9938 :
9939 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9940 0 : break;
9941 :
9942 3940496 : case CLEANUP_STMT:
9943 3940496 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9944 : non_constant_p, overflow_p,
9945 : jump_target);
9946 3940494 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9947 : {
9948 1761264 : iloc_sentinel ils (loc);
9949 1761264 : tree jmp_target = NULL_TREE;
9950 : /* Also evaluate the cleanup. */
9951 1761264 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9952 : non_constant_p, overflow_p,
9953 : &jmp_target);
9954 1761264 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9955 : jmp_target);
9956 1761264 : }
9957 : break;
9958 :
9959 : /* These differ from cxx_eval_unary_expression in that this doesn't
9960 : check for a constant operand or result; an address can be
9961 : constant without its operand being, and vice versa. */
9962 103606845 : case MEM_REF:
9963 103606845 : case INDIRECT_REF:
9964 103606845 : r = cxx_eval_indirect_ref (ctx, t, lval,
9965 : non_constant_p, overflow_p,
9966 : jump_target);
9967 103606845 : break;
9968 :
9969 101154862 : case ADDR_EXPR:
9970 101154862 : {
9971 101154862 : tree oldop = TREE_OPERAND (t, 0);
9972 101154862 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9973 : non_constant_p, overflow_p,
9974 : jump_target);
9975 101154862 : if (*jump_target)
9976 : return NULL_TREE;
9977 : /* Don't VERIFY_CONSTANT here. */
9978 101154857 : if (*non_constant_p)
9979 : return t;
9980 81547253 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9981 : /* This function does more aggressive folding than fold itself. */
9982 81547253 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9983 81547253 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9984 : {
9985 56530648 : ggc_free (r);
9986 56530648 : return t;
9987 : }
9988 : break;
9989 : }
9990 :
9991 855043 : case REALPART_EXPR:
9992 855043 : case IMAGPART_EXPR:
9993 855043 : if (lval)
9994 : {
9995 1284 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9996 : non_constant_p, overflow_p,
9997 : jump_target);
9998 1284 : if (*jump_target)
9999 : return NULL_TREE;
10000 1284 : if (r == error_mark_node)
10001 : ;
10002 1284 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
10003 : r = t;
10004 : else
10005 1216 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
10006 : break;
10007 : }
10008 : /* FALLTHRU */
10009 25754657 : case CONJ_EXPR:
10010 25754657 : case FIX_TRUNC_EXPR:
10011 25754657 : case FLOAT_EXPR:
10012 25754657 : case NEGATE_EXPR:
10013 25754657 : case ABS_EXPR:
10014 25754657 : case ABSU_EXPR:
10015 25754657 : case BIT_NOT_EXPR:
10016 25754657 : case TRUTH_NOT_EXPR:
10017 25754657 : case FIXED_CONVERT_EXPR:
10018 25754657 : case VEC_DUPLICATE_EXPR:
10019 25754657 : r = cxx_eval_unary_expression (ctx, t, lval,
10020 : non_constant_p, overflow_p,
10021 : jump_target);
10022 25754657 : break;
10023 :
10024 9426689 : case SIZEOF_EXPR:
10025 9426689 : r = fold_sizeof_expr (t);
10026 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
10027 : which could lead to an infinite recursion. */
10028 9426689 : if (TREE_CODE (r) != SIZEOF_EXPR)
10029 9426689 : r = cxx_eval_constant_expression (ctx, r, lval,
10030 : non_constant_p, overflow_p,
10031 : jump_target);
10032 : else
10033 : {
10034 0 : *non_constant_p = true;
10035 0 : gcc_assert (ctx->quiet);
10036 : }
10037 :
10038 : break;
10039 :
10040 13762169 : case COMPOUND_EXPR:
10041 13762169 : {
10042 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
10043 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10044 : introduced by build_call_a. */
10045 13762169 : tree op0 = TREE_OPERAND (t, 0);
10046 13762169 : tree op1 = TREE_OPERAND (t, 1);
10047 13762169 : STRIP_NOPS (op1);
10048 5586828 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10049 16392385 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10050 6836702 : r = cxx_eval_constant_expression (ctx, op0,
10051 : lval, non_constant_p, overflow_p,
10052 : jump_target);
10053 : else
10054 : {
10055 : /* Check that the LHS is constant and then discard it. */
10056 6925467 : cxx_eval_constant_expression (ctx, op0, vc_discard,
10057 : non_constant_p, overflow_p,
10058 : jump_target);
10059 6925467 : if (*jump_target)
10060 : return NULL_TREE;
10061 6918588 : if (*non_constant_p)
10062 : return t;
10063 6640371 : op1 = TREE_OPERAND (t, 1);
10064 6640371 : r = cxx_eval_constant_expression (ctx, op1,
10065 : lval, non_constant_p, overflow_p,
10066 : jump_target);
10067 : }
10068 : }
10069 : break;
10070 :
10071 112157476 : case POINTER_PLUS_EXPR:
10072 112157476 : case POINTER_DIFF_EXPR:
10073 112157476 : case PLUS_EXPR:
10074 112157476 : case MINUS_EXPR:
10075 112157476 : case MULT_EXPR:
10076 112157476 : case TRUNC_DIV_EXPR:
10077 112157476 : case CEIL_DIV_EXPR:
10078 112157476 : case FLOOR_DIV_EXPR:
10079 112157476 : case ROUND_DIV_EXPR:
10080 112157476 : case TRUNC_MOD_EXPR:
10081 112157476 : case CEIL_MOD_EXPR:
10082 112157476 : case ROUND_MOD_EXPR:
10083 112157476 : case RDIV_EXPR:
10084 112157476 : case EXACT_DIV_EXPR:
10085 112157476 : case MIN_EXPR:
10086 112157476 : case MAX_EXPR:
10087 112157476 : case LSHIFT_EXPR:
10088 112157476 : case RSHIFT_EXPR:
10089 112157476 : case LROTATE_EXPR:
10090 112157476 : case RROTATE_EXPR:
10091 112157476 : case BIT_IOR_EXPR:
10092 112157476 : case BIT_XOR_EXPR:
10093 112157476 : case BIT_AND_EXPR:
10094 112157476 : case TRUTH_XOR_EXPR:
10095 112157476 : case LT_EXPR:
10096 112157476 : case LE_EXPR:
10097 112157476 : case GT_EXPR:
10098 112157476 : case GE_EXPR:
10099 112157476 : case EQ_EXPR:
10100 112157476 : case NE_EXPR:
10101 112157476 : case SPACESHIP_EXPR:
10102 112157476 : case UNORDERED_EXPR:
10103 112157476 : case ORDERED_EXPR:
10104 112157476 : case UNLT_EXPR:
10105 112157476 : case UNLE_EXPR:
10106 112157476 : case UNGT_EXPR:
10107 112157476 : case UNGE_EXPR:
10108 112157476 : case UNEQ_EXPR:
10109 112157476 : case LTGT_EXPR:
10110 112157476 : case RANGE_EXPR:
10111 112157476 : case COMPLEX_EXPR:
10112 112157476 : r = cxx_eval_binary_expression (ctx, t, lval,
10113 : non_constant_p, overflow_p,
10114 : jump_target);
10115 112157476 : break;
10116 :
10117 : /* fold can introduce non-IF versions of these; still treat them as
10118 : short-circuiting. */
10119 15407209 : case TRUTH_AND_EXPR:
10120 15407209 : case TRUTH_ANDIF_EXPR:
10121 15407209 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
10122 : boolean_true_node,
10123 : non_constant_p, overflow_p,
10124 : jump_target);
10125 15407209 : break;
10126 :
10127 3053459 : case TRUTH_OR_EXPR:
10128 3053459 : case TRUTH_ORIF_EXPR:
10129 3053459 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
10130 : boolean_false_node,
10131 : non_constant_p, overflow_p,
10132 : jump_target);
10133 3053459 : break;
10134 :
10135 15518954 : case ARRAY_REF:
10136 15518954 : r = cxx_eval_array_reference (ctx, t, lval,
10137 : non_constant_p, overflow_p,
10138 : jump_target);
10139 15518954 : break;
10140 :
10141 115800324 : case COMPONENT_REF:
10142 115800324 : if (is_overloaded_fn (t))
10143 : {
10144 : /* We can only get here in checking mode via
10145 : build_non_dependent_expr, because any expression that
10146 : calls or takes the address of the function will have
10147 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
10148 6 : gcc_checking_assert (ctx->quiet || errorcount);
10149 6 : *non_constant_p = true;
10150 6 : return t;
10151 : }
10152 115800318 : r = cxx_eval_component_reference (ctx, t, lval,
10153 : non_constant_p, overflow_p,
10154 : jump_target);
10155 115800318 : break;
10156 :
10157 40745 : case BIT_FIELD_REF:
10158 40745 : r = cxx_eval_bit_field_ref (ctx, t, lval,
10159 : non_constant_p, overflow_p,
10160 : jump_target);
10161 40745 : break;
10162 :
10163 39378470 : case COND_EXPR:
10164 39378470 : case IF_STMT:
10165 39378470 : if (*jump_target)
10166 : {
10167 151552 : tree orig_jump = *jump_target;
10168 151552 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
10169 303104 : ? TREE_OPERAND (t, 1) : void_node);
10170 : /* When jumping to a label, the label might be either in the
10171 : then or else blocks, so process then block first in skipping
10172 : mode first, and if we are still in the skipping mode at its end,
10173 : process the else block too. */
10174 151552 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10175 : overflow_p, jump_target);
10176 : /* It's possible that we found the label in the then block. But
10177 : it could have been followed by another jumping statement, e.g.
10178 : say we're looking for case 1:
10179 : if (cond)
10180 : {
10181 : // skipped statements
10182 : case 1:; // clears up *jump_target
10183 : return 1; // and sets it to a RETURN_EXPR
10184 : }
10185 : else { ... }
10186 : in which case we need not go looking to the else block.
10187 : (goto is not allowed in a constexpr function.) */
10188 151552 : if (*jump_target == orig_jump)
10189 : {
10190 151609 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
10191 151609 : ? TREE_OPERAND (t, 2) : void_node);
10192 151514 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10193 : overflow_p, jump_target);
10194 : }
10195 : break;
10196 : }
10197 39226918 : r = cxx_eval_conditional_expression (ctx, t, lval,
10198 : non_constant_p, overflow_p,
10199 : jump_target);
10200 39226918 : break;
10201 11755 : case VEC_COND_EXPR:
10202 11755 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10203 : overflow_p, jump_target);
10204 11755 : break;
10205 :
10206 23279938 : case CONSTRUCTOR:
10207 23279938 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10208 : {
10209 : /* Don't re-process a constant CONSTRUCTOR. */
10210 19896958 : verify_constructor_flags (t);
10211 19896958 : if (TREE_CONSTANT (t))
10212 : return t;
10213 : }
10214 3382980 : if (TREE_CLOBBER_P (t))
10215 : {
10216 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10217 : a clobber by itself isn't a constant-expression. */
10218 240 : gcc_assert (ctx->quiet);
10219 240 : *non_constant_p = true;
10220 240 : break;
10221 : }
10222 3382740 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10223 : non_constant_p, overflow_p, jump_target);
10224 3382740 : break;
10225 :
10226 790 : case VEC_INIT_EXPR:
10227 : /* We can get this in a defaulted constructor for a class with a
10228 : non-static data member of array type. Either the initializer will
10229 : be NULL, meaning default-initialization, or it will be an lvalue
10230 : or xvalue of the same type, meaning direct-initialization from the
10231 : corresponding member. */
10232 790 : r = cxx_eval_vec_init (ctx, t, lval,
10233 : non_constant_p, overflow_p, jump_target);
10234 790 : break;
10235 :
10236 43640 : case VEC_PERM_EXPR:
10237 43640 : r = cxx_eval_trinary_expression (ctx, t, lval,
10238 : non_constant_p, overflow_p,
10239 : jump_target);
10240 43640 : break;
10241 :
10242 7 : case PAREN_EXPR:
10243 7 : gcc_assert (!REF_PARENTHESIZED_P (t));
10244 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10245 : constant expressions since it's unaffected by -fassociative-math. */
10246 7 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10247 : non_constant_p, overflow_p,
10248 : jump_target);
10249 7 : break;
10250 :
10251 440364145 : case NOP_EXPR:
10252 440364145 : if (REINTERPRET_CAST_P (t))
10253 : {
10254 145 : if (!ctx->quiet)
10255 6 : error_at (loc,
10256 : "%<reinterpret_cast%> is not a constant expression");
10257 145 : *non_constant_p = true;
10258 145 : return t;
10259 : }
10260 : /* FALLTHROUGH. */
10261 551359399 : case CONVERT_EXPR:
10262 551359399 : case VIEW_CONVERT_EXPR:
10263 551359399 : case UNARY_PLUS_EXPR:
10264 551359399 : {
10265 551359399 : tree oldop = TREE_OPERAND (t, 0);
10266 :
10267 551359399 : tree op = cxx_eval_constant_expression (ctx, oldop,
10268 551359399 : VOID_TYPE_P (TREE_TYPE (t))
10269 : ? vc_discard
10270 : : tcode == VIEW_CONVERT_EXPR
10271 517660844 : ? lval : vc_prvalue,
10272 : non_constant_p, overflow_p,
10273 : jump_target);
10274 551356700 : if (*jump_target)
10275 : return NULL_TREE;
10276 551352826 : if (*non_constant_p)
10277 : return t;
10278 443798802 : tree type = TREE_TYPE (t);
10279 :
10280 443798802 : if (VOID_TYPE_P (type))
10281 32497292 : return void_node;
10282 :
10283 411301510 : if (TREE_CODE (t) == CONVERT_EXPR
10284 59783064 : && ARITHMETIC_TYPE_P (type)
10285 5070031 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10286 411325027 : && ctx->strict)
10287 : {
10288 23422 : if (!ctx->quiet)
10289 54 : error_at (loc,
10290 : "conversion from pointer type %qT to arithmetic type "
10291 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10292 23422 : *non_constant_p = true;
10293 23422 : return t;
10294 : }
10295 :
10296 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10297 : type cannot be part of a core constant expression as a resolution to
10298 : DR 1312. */
10299 152651190 : if (TYPE_PTROB_P (type)
10300 149245532 : && TYPE_PTR_P (TREE_TYPE (op))
10301 94407049 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10302 : /* Inside a call to std::construct_at,
10303 : std::allocator<T>::{,de}allocate, or
10304 : std::source_location::current, we permit casting from void*
10305 : because that is compiler-generated code. */
10306 1939948 : && !is_std_construct_at (ctx->call)
10307 163532 : && !is_std_allocator_allocate (ctx->call)
10308 411359555 : && !is_std_source_location_current (ctx->call))
10309 : {
10310 : /* Likewise, don't error when casting from void* when OP is
10311 : &heap uninit and similar. */
10312 81308 : tree sop = tree_strip_nop_conversions (op);
10313 81308 : tree decl = NULL_TREE;
10314 81308 : if (TREE_CODE (sop) == ADDR_EXPR)
10315 61940 : decl = TREE_OPERAND (sop, 0);
10316 61940 : if (decl
10317 61940 : && VAR_P (decl)
10318 61120 : && DECL_ARTIFICIAL (decl)
10319 122626 : && (DECL_NAME (decl) == heap_identifier
10320 39065 : || DECL_NAME (decl) == heap_uninit_identifier
10321 4525 : || DECL_NAME (decl) == heap_vec_identifier
10322 2608 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
10323 : /* OK */;
10324 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10325 : cv void" to a pointer-to-object type T unless P is a null
10326 : pointer value or points to an object whose type is similar to
10327 : T. */
10328 21033 : else if (cxx_dialect > cxx23)
10329 : {
10330 20868 : if (integer_zerop (sop))
10331 30 : return build_int_cst (type, 0);
10332 20838 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10333 : NULL, jump_target);
10334 20838 : if (*jump_target)
10335 : return NULL_TREE;
10336 20838 : if (r)
10337 : {
10338 20791 : r = build1 (ADDR_EXPR, type, r);
10339 20791 : break;
10340 : }
10341 47 : if (!ctx->quiet)
10342 : {
10343 5 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10344 5 : auto_diagnostic_group d;
10345 5 : error_at (loc, "cast from %qT is not allowed in a "
10346 : "constant expression because "
10347 : "pointed-to type %qT is not similar to %qT",
10348 5 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10349 5 : TREE_TYPE (type));
10350 5 : tree obj = build_fold_indirect_ref (sop);
10351 5 : if (TREE_CODE (obj) == COMPONENT_REF)
10352 4 : obj = TREE_OPERAND (obj, 1);
10353 5 : if (DECL_P (obj))
10354 5 : inform (DECL_SOURCE_LOCATION (obj),
10355 : "pointed-to object declared here");
10356 5 : }
10357 47 : *non_constant_p = true;
10358 47 : return t;
10359 : }
10360 : else
10361 : {
10362 165 : if (!ctx->quiet)
10363 26 : error_at (loc, "cast from %qT is not allowed in a "
10364 : "constant expression before C++26",
10365 26 : TREE_TYPE (op));
10366 165 : *non_constant_p = true;
10367 165 : return t;
10368 : }
10369 : }
10370 :
10371 411257055 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
10372 : {
10373 1064 : op = cplus_expand_constant (op);
10374 1064 : if (TREE_CODE (op) == PTRMEM_CST)
10375 : {
10376 21 : if (!ctx->quiet)
10377 3 : error_at (loc, "%qE is not a constant expression when the "
10378 : "class %qT is still incomplete", op,
10379 3 : PTRMEM_CST_CLASS (op));
10380 21 : *non_constant_p = true;
10381 21 : return t;
10382 : }
10383 : }
10384 :
10385 411257034 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
10386 : {
10387 655 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
10388 655 : && !can_convert_qual (type, op))
10389 6 : op = cplus_expand_constant (op);
10390 655 : return cp_fold_convert (type, op);
10391 : }
10392 :
10393 411256379 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
10394 : {
10395 838040 : if (integer_zerop (op))
10396 : {
10397 781107 : if (TYPE_REF_P (type))
10398 : {
10399 36 : if (!ctx->quiet)
10400 4 : error_at (loc, "dereferencing a null pointer");
10401 36 : *non_constant_p = true;
10402 36 : return t;
10403 : }
10404 : }
10405 56933 : else if (TYPE_PTR_P (type)
10406 56933 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10407 : /* INTEGER_CST with pointer-to-method type is only used
10408 : for a virtual method in a pointer to member function.
10409 : Don't reject those. */
10410 : ;
10411 : else
10412 : {
10413 : /* This detects for example:
10414 : reinterpret_cast<void*>(sizeof 0)
10415 : */
10416 56930 : if (!ctx->quiet)
10417 15 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
10418 : "a constant expression",
10419 : type, op);
10420 56930 : *non_constant_p = true;
10421 56930 : return t;
10422 : }
10423 : }
10424 :
10425 411199413 : if (INDIRECT_TYPE_P (type)
10426 255078577 : && TREE_CODE (op) == NOP_EXPR
10427 122324103 : && TREE_TYPE (op) == ptr_type_node
10428 521765 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
10429 518601 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
10430 411607265 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10431 407852 : 0)) == heap_uninit_identifier
10432 291247 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10433 291247 : 0)) == heap_vec_uninit_identifier))
10434 : {
10435 118802 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
10436 118802 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
10437 118802 : tree elt_type = TREE_TYPE (type);
10438 118802 : tree cookie_size = NULL_TREE;
10439 118802 : tree arg_size = NULL_TREE;
10440 118802 : if (TREE_CODE (elt_type) == RECORD_TYPE
10441 118802 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
10442 : {
10443 9 : tree fld1 = TYPE_FIELDS (elt_type);
10444 9 : tree fld2 = DECL_CHAIN (fld1);
10445 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
10446 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
10447 : }
10448 118802 : DECL_NAME (var)
10449 118802 : = (DECL_NAME (var) == heap_uninit_identifier
10450 118802 : ? heap_identifier : heap_vec_identifier);
10451 : /* For zero sized elt_type, try to recover how many outer_nelts
10452 : it should have. */
10453 118802 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
10454 118793 : : integer_zerop (var_size))
10455 105 : && !int_size_in_bytes (elt_type)
10456 9 : && TREE_CODE (oldop) == CALL_EXPR
10457 118820 : && call_expr_nargs (oldop) >= 1)
10458 9 : if (tree fun = get_function_named_in_call (oldop))
10459 9 : if (cxx_replaceable_global_alloc_fn (fun)
10460 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
10461 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
10462 118802 : tree new_type
10463 118802 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
10464 : var_size, arg_size,
10465 : non_constant_p, overflow_p,
10466 : jump_target);
10467 118802 : if (*jump_target)
10468 : return NULL_TREE;
10469 118802 : TREE_TYPE (var) = new_type;
10470 118802 : TREE_TYPE (TREE_OPERAND (op, 0))
10471 237604 : = build_pointer_type (TREE_TYPE (var));
10472 : }
10473 :
10474 : /* This can happen for std::meta::info(^^int) where the cast has no
10475 : meaning. */
10476 411199413 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
10477 : {
10478 : r = op;
10479 : break;
10480 : }
10481 :
10482 411091337 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
10483 : /* We didn't fold at the top so we could check for ptr-int
10484 : conversion. */
10485 32582717 : return fold (t);
10486 :
10487 378508620 : tree sop;
10488 :
10489 : /* Handle an array's bounds having been deduced after we built
10490 : the wrapping expression. */
10491 378508620 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
10492 : r = op;
10493 164854159 : else if (sop = tree_strip_nop_conversions (op),
10494 231031917 : sop != op && (same_type_ignoring_tlq_and_bounds_p
10495 66177758 : (type, TREE_TYPE (sop))))
10496 : r = sop;
10497 135710764 : else if (tcode == UNARY_PLUS_EXPR)
10498 0 : r = fold_convert (TREE_TYPE (t), op);
10499 : else
10500 135710764 : r = fold_build1 (tcode, type, op);
10501 :
10502 : /* Conversion of an out-of-range value has implementation-defined
10503 : behavior; the language considers it different from arithmetic
10504 : overflow, which is undefined. */
10505 378508620 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10506 13159 : TREE_OVERFLOW (r) = false;
10507 : }
10508 : break;
10509 :
10510 9653 : case EXCESS_PRECISION_EXPR:
10511 9653 : {
10512 9653 : tree oldop = TREE_OPERAND (t, 0);
10513 :
10514 9653 : tree op = cxx_eval_constant_expression (ctx, oldop,
10515 : lval,
10516 : non_constant_p, overflow_p,
10517 : jump_target);
10518 9653 : if (*jump_target)
10519 : return NULL_TREE;
10520 9653 : if (*non_constant_p)
10521 : return t;
10522 9647 : r = fold_convert (TREE_TYPE (t), op);
10523 9647 : break;
10524 : }
10525 :
10526 65012 : case EMPTY_CLASS_EXPR:
10527 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10528 : it to an appropriate CONSTRUCTOR. */
10529 65012 : return build_constructor (TREE_TYPE (t), NULL);
10530 :
10531 48396260 : case STATEMENT_LIST:
10532 48396260 : new_ctx = *ctx;
10533 48396260 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10534 48396260 : return cxx_eval_statement_list (&new_ctx, t,
10535 48396256 : non_constant_p, overflow_p, jump_target);
10536 :
10537 33655992 : case BIND_EXPR:
10538 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10539 : map, so that when checking whether they're already destroyed later we
10540 : don't get confused by remnants of previous calls. */
10541 52930644 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10542 19274652 : ctx->global->clear_value (decl);
10543 33655992 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10544 : lval,
10545 : non_constant_p, overflow_p,
10546 : jump_target);
10547 52930640 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10548 19274650 : destroy_value_checked (ctx, decl, non_constant_p);
10549 : break;
10550 :
10551 8696244 : case PREINCREMENT_EXPR:
10552 8696244 : case POSTINCREMENT_EXPR:
10553 8696244 : case PREDECREMENT_EXPR:
10554 8696244 : case POSTDECREMENT_EXPR:
10555 8696244 : return cxx_eval_increment_expression (ctx, t,
10556 : lval, non_constant_p, overflow_p,
10557 8696244 : jump_target);
10558 :
10559 1922 : case THROW_EXPR:
10560 1922 : if (cxx_dialect >= cxx26)
10561 1595 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10562 : non_constant_p, overflow_p,
10563 1595 : jump_target);
10564 : /* FALLTHROUGH */
10565 335 : case LAMBDA_EXPR:
10566 335 : case NEW_EXPR:
10567 335 : case VEC_NEW_EXPR:
10568 335 : case DELETE_EXPR:
10569 335 : case VEC_DELETE_EXPR:
10570 335 : case MODOP_EXPR:
10571 : /* GCC internal stuff. */
10572 335 : case VA_ARG_EXPR:
10573 335 : case BASELINK:
10574 335 : case OFFSET_REF:
10575 335 : if (!ctx->quiet)
10576 86 : error_at (loc, "expression %qE is not a constant expression", t);
10577 335 : *non_constant_p = true;
10578 335 : break;
10579 :
10580 405957 : case OBJ_TYPE_REF:
10581 : /* Virtual function lookup. We don't need to do anything fancy. */
10582 405957 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10583 : lval, non_constant_p, overflow_p,
10584 405957 : jump_target);
10585 :
10586 16140 : case PLACEHOLDER_EXPR:
10587 : /* Use of the value or address of the current object. */
10588 16140 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10589 : {
10590 15897 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10591 : return ctor;
10592 : else
10593 15597 : return cxx_eval_constant_expression (ctx, ctor, lval,
10594 : non_constant_p, overflow_p,
10595 15597 : jump_target);
10596 : }
10597 : /* A placeholder without a referent. We can get here when
10598 : checking whether NSDMIs are noexcept, or in massage_init_elt;
10599 : just say it's non-constant for now. */
10600 243 : gcc_assert (ctx->quiet);
10601 243 : *non_constant_p = true;
10602 243 : break;
10603 :
10604 6580 : case EXIT_EXPR:
10605 6580 : {
10606 6580 : tree cond = TREE_OPERAND (t, 0);
10607 6580 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10608 : non_constant_p, overflow_p,
10609 : jump_target);
10610 6580 : if (*jump_target)
10611 : return NULL_TREE;
10612 6580 : VERIFY_CONSTANT (cond);
10613 6580 : if (integer_nonzerop (cond))
10614 3219 : *jump_target = t;
10615 : }
10616 : break;
10617 :
10618 83 : case GOTO_EXPR:
10619 83 : if (breaks (&TREE_OPERAND (t, 0))
10620 83 : || continues (&TREE_OPERAND (t, 0)))
10621 64 : *jump_target = TREE_OPERAND (t, 0);
10622 : else
10623 : {
10624 19 : if (!ctx->quiet)
10625 1 : error_at (loc, "%<goto%> is not a constant expression");
10626 19 : *non_constant_p = true;
10627 : }
10628 : break;
10629 :
10630 6651765 : case LOOP_EXPR:
10631 6651765 : case DO_STMT:
10632 6651765 : case WHILE_STMT:
10633 6651765 : case FOR_STMT:
10634 6651765 : cxx_eval_loop_expr (ctx, t,
10635 : non_constant_p, overflow_p, jump_target);
10636 6651765 : break;
10637 :
10638 49991 : case SWITCH_EXPR:
10639 49991 : case SWITCH_STMT:
10640 49991 : cxx_eval_switch_expr (ctx, t,
10641 : non_constant_p, overflow_p, jump_target);
10642 49991 : break;
10643 :
10644 144 : case REQUIRES_EXPR:
10645 : /* It's possible to get a requires-expression in a constant
10646 : expression. For example:
10647 :
10648 : template<typename T> concept bool C() {
10649 : return requires (T t) { t; };
10650 : }
10651 :
10652 : template<typename T> requires !C<T>() void f(T);
10653 :
10654 : Normalization leaves f with the associated constraint
10655 : '!requires (T t) { ... }' which is not transformed into
10656 : a constraint. */
10657 144 : if (!processing_template_decl)
10658 144 : return evaluate_requires_expr (t);
10659 : else
10660 0 : *non_constant_p = true;
10661 0 : return t;
10662 :
10663 1223581 : case ANNOTATE_EXPR:
10664 1223581 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10665 : lval,
10666 : non_constant_p, overflow_p,
10667 : jump_target);
10668 1223581 : break;
10669 :
10670 16509 : case USING_STMT:
10671 16509 : r = void_node;
10672 16509 : break;
10673 :
10674 30 : case ASSERTION_STMT:
10675 30 : case PRECONDITION_STMT:
10676 30 : case POSTCONDITION_STMT:
10677 30 : {
10678 30 : r = void_node;
10679 : /* Only record the first fail, and do not go further is the semantic
10680 : is 'ignore'. */
10681 30 : if (*non_constant_p || ctx->global->contract_statement
10682 60 : || contract_ignored_p (t))
10683 : break;
10684 :
10685 30 : tree cond = CONTRACT_CONDITION (t);
10686 30 : if (!potential_rvalue_constant_expression (cond))
10687 : {
10688 6 : ctx->global->contract_statement = t;
10689 6 : ctx->global->contract_condition_non_const = true;
10690 6 : break;
10691 : }
10692 :
10693 : /* We need to evaluate and stash the result of this here, since whether
10694 : it needs to be reported (and how) depends on whether the containing
10695 : expression is otherwise const. */
10696 24 : bool ctrct_non_const_p = false;
10697 24 : bool ctrct_overflow_p = false;
10698 24 : tree jmp_target = NULL_TREE;
10699 24 : constexpr_ctx new_ctx = *ctx;
10700 24 : new_ctx.quiet = true;
10701 : /* Avoid modification of existing values. */
10702 24 : modifiable_tracker ms (new_ctx.global);
10703 24 : tree eval =
10704 24 : cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
10705 : &ctrct_non_const_p,
10706 : &ctrct_overflow_p, &jmp_target);
10707 : /* Not a constant. */
10708 24 : if (ctrct_non_const_p)
10709 : {
10710 0 : ctx->global->contract_statement = t;
10711 0 : ctx->global->contract_condition_non_const = true;
10712 0 : break;
10713 : }
10714 : /* Constant, but check failed. */
10715 24 : if (integer_zerop (eval))
10716 24 : ctx->global->contract_statement = t;
10717 24 : }
10718 24 : break;
10719 :
10720 2753877 : case TEMPLATE_ID_EXPR:
10721 2753877 : {
10722 : /* We can evaluate template-id that refers to a concept only if
10723 : the template arguments are non-dependent. */
10724 2753877 : gcc_assert (concept_check_p (t));
10725 :
10726 2753877 : if (!value_dependent_expression_p (t)
10727 2753877 : && !uid_sensitive_constexpr_evaluation_p ())
10728 2752954 : r = evaluate_concept_check (t);
10729 : else
10730 923 : *non_constant_p = true;
10731 :
10732 : break;
10733 : }
10734 :
10735 51 : case ASM_EXPR:
10736 51 : if (!ctx->quiet)
10737 29 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10738 51 : *non_constant_p = true;
10739 51 : return t;
10740 :
10741 4019 : case BIT_CAST_EXPR:
10742 4019 : if (lval)
10743 : {
10744 0 : if (!ctx->quiet)
10745 0 : error_at (EXPR_LOCATION (t),
10746 : "address of a call to %qs is not a constant expression",
10747 : "__builtin_bit_cast");
10748 0 : *non_constant_p = true;
10749 0 : return t;
10750 : }
10751 4019 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10752 4019 : break;
10753 :
10754 0 : case OMP_PARALLEL:
10755 0 : case OMP_TASK:
10756 0 : case OMP_FOR:
10757 0 : case OMP_SIMD:
10758 0 : case OMP_DISTRIBUTE:
10759 0 : case OMP_TASKLOOP:
10760 0 : case OMP_LOOP:
10761 0 : case OMP_TEAMS:
10762 0 : case OMP_TARGET_DATA:
10763 0 : case OMP_TARGET:
10764 0 : case OMP_SECTIONS:
10765 0 : case OMP_ORDERED:
10766 0 : case OMP_CRITICAL:
10767 0 : case OMP_SINGLE:
10768 0 : case OMP_SCAN:
10769 0 : case OMP_SCOPE:
10770 0 : case OMP_SECTION:
10771 0 : case OMP_STRUCTURED_BLOCK:
10772 0 : case OMP_MASTER:
10773 0 : case OMP_MASKED:
10774 0 : case OMP_TASKGROUP:
10775 0 : case OMP_TARGET_UPDATE:
10776 0 : case OMP_TARGET_ENTER_DATA:
10777 0 : case OMP_TARGET_EXIT_DATA:
10778 0 : case OMP_ATOMIC:
10779 0 : case OMP_ATOMIC_READ:
10780 0 : case OMP_ATOMIC_CAPTURE_OLD:
10781 0 : case OMP_ATOMIC_CAPTURE_NEW:
10782 0 : case OMP_DEPOBJ:
10783 0 : case OACC_PARALLEL:
10784 0 : case OACC_KERNELS:
10785 0 : case OACC_SERIAL:
10786 0 : case OACC_DATA:
10787 0 : case OACC_HOST_DATA:
10788 0 : case OACC_LOOP:
10789 0 : case OACC_CACHE:
10790 0 : case OACC_DECLARE:
10791 0 : case OACC_ENTER_DATA:
10792 0 : case OACC_EXIT_DATA:
10793 0 : case OACC_UPDATE:
10794 0 : if (!ctx->quiet)
10795 0 : error_at (EXPR_LOCATION (t),
10796 : "statement is not a constant expression");
10797 0 : *non_constant_p = true;
10798 0 : break;
10799 :
10800 0 : default:
10801 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10802 : {
10803 : /* This function doesn't know how to deal with pre-genericize
10804 : statements; this can only happen with statement-expressions,
10805 : so for now just fail. */
10806 0 : if (!ctx->quiet)
10807 0 : error_at (EXPR_LOCATION (t),
10808 : "statement is not a constant expression");
10809 : }
10810 0 : else if (flag_checking)
10811 0 : internal_error ("unexpected expression %qE of kind %s", t,
10812 : get_tree_code_name (TREE_CODE (t)));
10813 0 : *non_constant_p = true;
10814 0 : break;
10815 : }
10816 :
10817 2051876752 : if (r == error_mark_node)
10818 14650163 : *non_constant_p = true;
10819 :
10820 135403330 : if (r == void_node && lval != vc_discard && !*jump_target
10821 2051894845 : && !VOID_TYPE_P (TREE_TYPE (t)))
10822 : {
10823 : /* For diagnostic quality we should have handled this sooner, where we
10824 : can be more specific about the out-of-lifetime object. But here we
10825 : can still be correct. */
10826 2 : gcc_checking_assert (false);
10827 : if (!ctx->quiet)
10828 : error_at (EXPR_LOCATION (t),
10829 : "%qE accesses an object outside its lifetime", t);
10830 : *non_constant_p = true;
10831 : }
10832 :
10833 2051876750 : if (*non_constant_p)
10834 : return t;
10835 : else
10836 1628793337 : return r;
10837 : }
10838 :
10839 : /* P0859: A function is needed for constant evaluation if it is a constexpr
10840 : function that is named by an expression ([basic.def.odr]) that is
10841 : potentially constant evaluated.
10842 :
10843 : So we need to instantiate any constexpr functions mentioned by the
10844 : expression even if the definition isn't needed for evaluating the
10845 : expression. */
10846 :
10847 : static tree
10848 584543630 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10849 : {
10850 584543630 : if (TREE_CODE (*tp) == FUNCTION_DECL
10851 12220217 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10852 12082790 : && !DECL_INITIAL (*tp)
10853 3114462 : && !trivial_fn_p (*tp)
10854 3114313 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10855 584543630 : && !uid_sensitive_constexpr_evaluation_p ())
10856 : {
10857 3084223 : ++function_depth;
10858 3084223 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10859 3084196 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10860 : else
10861 27 : synthesize_method (*tp);
10862 3084223 : --function_depth;
10863 : }
10864 581459407 : else if (TREE_CODE (*tp) == CALL_EXPR
10865 569733581 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10866 : {
10867 12285232 : if (EXPR_HAS_LOCATION (*tp))
10868 12278421 : input_location = EXPR_LOCATION (*tp);
10869 : }
10870 :
10871 584543630 : if (!EXPR_P (*tp))
10872 330488148 : *walk_subtrees = 0;
10873 :
10874 584543630 : return NULL_TREE;
10875 : }
10876 :
10877 : static void
10878 280530774 : instantiate_constexpr_fns (tree t)
10879 : {
10880 280530774 : location_t loc = input_location;
10881 280530774 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10882 280530774 : input_location = loc;
10883 280530774 : }
10884 :
10885 : /* Look for heap variables in the expression *TP. */
10886 :
10887 : static tree
10888 381117 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10889 : {
10890 381117 : if (VAR_P (*tp)
10891 381117 : && (DECL_NAME (*tp) == heap_uninit_identifier
10892 10165 : || DECL_NAME (*tp) == heap_identifier
10893 3245 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10894 2487 : || DECL_NAME (*tp) == heap_vec_identifier
10895 719 : || DECL_NAME (*tp) == heap_deleted_identifier))
10896 : return *tp;
10897 :
10898 267412 : if (TYPE_P (*tp))
10899 178 : *walk_subtrees = 0;
10900 : return NULL_TREE;
10901 : }
10902 :
10903 : /* Find immediate function decls in *TP if any. */
10904 :
10905 : static tree
10906 613277342 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
10907 : {
10908 617342035 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10909 : return *tp;
10910 613270000 : if (TREE_CODE (*tp) == PTRMEM_CST
10911 29792 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10912 613298989 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10913 : return PTRMEM_CST_MEMBER (*tp);
10914 613269895 : if (REFLECT_EXPR_P (*tp))
10915 103576 : *walk_subtrees = 0;
10916 : return NULL_TREE;
10917 : }
10918 :
10919 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10920 : expression. Return a version of T that has TREE_CONSTANT cleared. */
10921 :
10922 : static tree
10923 148099 : mark_non_constant (tree t)
10924 : {
10925 148099 : gcc_checking_assert (TREE_CONSTANT (t));
10926 :
10927 : /* This isn't actually constant, so unset TREE_CONSTANT.
10928 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10929 : it to be set if it is invariant address, even when it is not
10930 : a valid C++ constant expression. Wrap it with a NOP_EXPR
10931 : instead. */
10932 148099 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10933 145344 : t = copy_node (t);
10934 2755 : else if (TREE_CODE (t) == CONSTRUCTOR)
10935 475 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10936 : else
10937 2280 : t = build_nop (TREE_TYPE (t), t);
10938 148099 : TREE_CONSTANT (t) = false;
10939 148099 : return t;
10940 : }
10941 :
10942 : /* If we have a successful constant evaluation, now check whether there is
10943 : a failed or non-constant contract that would invalidate this. */
10944 :
10945 : static bool
10946 558756747 : check_for_failed_contracts (constexpr_ctx *ctx)
10947 : {
10948 558756747 : constexpr_global_ctx *const global_ctx = ctx->global;
10949 558756747 : if (!flag_contracts || !global_ctx->contract_statement)
10950 : return false;
10951 :
10952 30 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
10953 30 : enum diagnostics::kind kind;
10954 30 : bool error = false;
10955 : /* [intro.compliance.general]/2.3.4. */
10956 : /* [basic.contract.eval]/8. */
10957 30 : if (ctx->manifestly_const_eval != mce_true)
10958 : {
10959 : /* When !MCE, silently return not constant. */
10960 : return true;
10961 : }
10962 27 : else if (contract_terminating_p (global_ctx->contract_statement))
10963 : {
10964 : kind = diagnostics::kind::error;
10965 : error = true;
10966 : }
10967 : else
10968 6 : kind = diagnostics::kind::warning;
10969 :
10970 : /* [basic.contract.eval]/7.3 */
10971 27 : if (global_ctx->contract_condition_non_const)
10972 : {
10973 6 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
10974 6 : return error;
10975 : }
10976 :
10977 : /* Otherwise, the evaluation was const, but determined to be false. */
10978 21 : emit_diagnostic (kind, loc, 0,
10979 : "contract predicate is false in constant expression");
10980 21 : return error;
10981 : }
10982 :
10983 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10984 : STRICT has the same sense as for constant_value_1: true if we only allow
10985 : conforming C++ constant expressions, or false if we want a constant value
10986 : even if it doesn't conform.
10987 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10988 : per P0595 even when ALLOW_NON_CONSTANT is true.
10989 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10990 : OBJECT must be non-NULL in that case. */
10991 :
10992 : static tree
10993 568832578 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10994 : bool strict = true,
10995 : mce_value manifestly_const_eval = mce_unknown,
10996 : bool constexpr_dtor = false,
10997 : tree object = NULL_TREE)
10998 : {
10999 568832578 : auto_timevar time (TV_CONSTEXPR);
11000 :
11001 568832578 : bool non_constant_p = false;
11002 568832578 : bool overflow_p = false;
11003 :
11004 568832578 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
11005 : {
11006 0 : gcc_checking_assert (allow_non_constant);
11007 : return t;
11008 : }
11009 :
11010 568832578 : constexpr_global_ctx global_ctx;
11011 568832578 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
11012 : allow_non_constant, strict,
11013 568832578 : !allow_non_constant ? mce_true : manifestly_const_eval };
11014 :
11015 : /* Turn off -frounding-math for manifestly constant evaluation. */
11016 568832578 : warning_sentinel rm (flag_rounding_math,
11017 568832578 : ctx.manifestly_const_eval == mce_true);
11018 568832578 : tree type = (object
11019 568832578 : ? cv_unqualified (TREE_TYPE (object))
11020 502196990 : : initialized_type (t));
11021 568832578 : tree r = t;
11022 568832578 : bool is_consteval = false;
11023 568832578 : if (VOID_TYPE_P (type))
11024 : {
11025 10068341 : if (!constexpr_dtor)
11026 : {
11027 10068341 : if (cxx_dialect < cxx20)
11028 : return t;
11029 : /* We could have a COMPOUND_EXPR here coming from
11030 : keep_unused_object_arg. */
11031 10057296 : tree x = extract_call_expr (t);
11032 10057296 : if (x == NULL_TREE || x == error_mark_node)
11033 : return t;
11034 : /* Calls to immediate functions returning void need to be
11035 : evaluated. */
11036 10057256 : tree fndecl = cp_get_callee_fndecl_nofold (x);
11037 20114512 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
11038 : return t;
11039 : else
11040 1134 : is_consteval = true;
11041 1134 : tree lam;
11042 1134 : if (manifestly_const_eval == mce_true
11043 1653 : && LAMBDA_FUNCTION_P (fndecl)
11044 513 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
11045 1647 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
11046 496 : global_ctx.consteval_block = fndecl;
11047 : }
11048 : }
11049 558764237 : else if (cxx_dialect >= cxx20
11050 549861355 : && (TREE_CODE (t) == CALL_EXPR
11051 471063704 : || TREE_CODE (t) == AGGR_INIT_EXPR
11052 465950669 : || TREE_CODE (t) == TARGET_EXPR))
11053 : {
11054 89373186 : tree x = t;
11055 89373186 : if (TREE_CODE (x) == TARGET_EXPR)
11056 5462500 : x = TARGET_EXPR_INITIAL (x);
11057 89373186 : tree fndecl = cp_get_callee_fndecl_nofold (x);
11058 175963508 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
11059 : is_consteval = true;
11060 : /* Don't try to evaluate a std::vector constructor taking an integer, it
11061 : will fail in the 'if (heap_var)' block below after doing all the work
11062 : (c++/113835). This will need adjustment if P3554 is accepted. Note
11063 : that evaluation of e.g. the vector default constructor can succeed, so
11064 : we don't shortcut all vector constructors. */
11065 173180644 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
11066 11512416 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
11067 89397656 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
11068 : return t;
11069 : }
11070 558763384 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
11071 : {
11072 : /* In C++14 an NSDMI can participate in aggregate initialization,
11073 : and can refer to the address of the object being initialized, so
11074 : we need to pass in the relevant VAR_DECL if we want to do the
11075 : evaluation in a single pass. The evaluation will dynamically
11076 : update ctx.values for the VAR_DECL. We use the same strategy
11077 : for C++11 constexpr constructors that refer to the object being
11078 : initialized. */
11079 49536834 : if (constexpr_dtor)
11080 : {
11081 200 : gcc_assert (object && VAR_P (object));
11082 200 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
11083 200 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
11084 200 : if (error_operand_p (DECL_INITIAL (object)))
11085 : return t;
11086 183 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
11087 183 : TREE_READONLY (ctx.ctor) = false;
11088 : /* Temporarily force decl_really_constant_value to return false
11089 : for it, we want to use ctx.ctor for the current value instead. */
11090 183 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
11091 : }
11092 : else
11093 : {
11094 49536634 : ctx.ctor = build_constructor (type, NULL);
11095 49536634 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
11096 : }
11097 49536817 : if (!object)
11098 : {
11099 21967707 : if (TREE_CODE (t) == CALL_EXPR)
11100 : {
11101 : /* If T is calling a constructor to initialize an object, reframe
11102 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
11103 : from outside the constant evaluation, which will fail even if
11104 : the value is actually constant (is_constant_evaluated3.C). */
11105 9957502 : tree fn = cp_get_callee_fndecl_nofold (t);
11106 19914796 : if (fn && DECL_CONSTRUCTOR_P (fn))
11107 : {
11108 4088516 : object = CALL_EXPR_ARG (t, 0);
11109 4088516 : object = build_fold_indirect_ref (object);
11110 4088516 : r = build_aggr_init_expr (type, r);
11111 : }
11112 : }
11113 12010205 : else if (TREE_CODE (t) == TARGET_EXPR)
11114 5357708 : object = TARGET_EXPR_SLOT (t);
11115 6652497 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
11116 708010 : object = AGGR_INIT_EXPR_SLOT (t);
11117 : }
11118 49536817 : ctx.object = object;
11119 49536817 : if (object)
11120 37723344 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
11121 : (type, TREE_TYPE (object)));
11122 37723344 : if (object && DECL_P (object))
11123 31593089 : global_ctx.put_value (object, ctx.ctor);
11124 49536817 : if (TREE_CODE (r) == TARGET_EXPR)
11125 : /* Avoid creating another CONSTRUCTOR when we expand the
11126 : TARGET_EXPR. */
11127 5498999 : r = TARGET_EXPR_INITIAL (r);
11128 : }
11129 :
11130 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
11131 : constexpr evaluation to avoid unnecessary template instantiation, and is
11132 : always done with mce_unknown. But due to gaps in the restriction logic
11133 : we may still end up taking an evaluation path that in turn requires
11134 : manifestly constant evaluation, and such evaluation must not be
11135 : restricted since it likely has semantic consequences.
11136 : TODO: Remove/replace the mechanism in GCC 17. */
11137 558763367 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
11138 558763367 : if (ctx.manifestly_const_eval == mce_true)
11139 280530774 : uid_sensitive_constexpr_evaluation_value = false;
11140 :
11141 558763367 : auto_vec<tree, 16> cleanups;
11142 558763367 : global_ctx.cleanups = &cleanups;
11143 :
11144 558763367 : if (manifestly_const_eval == mce_true)
11145 280530774 : instantiate_constexpr_fns (r);
11146 558763367 : tree jmp_target = NULL_TREE;
11147 558763367 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
11148 : &non_constant_p, &overflow_p,
11149 : &jmp_target);
11150 558761920 : if (throws (&jmp_target) && !non_constant_p)
11151 : {
11152 1252 : if (!ctx.quiet)
11153 374 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11154 1252 : non_constant_p = true;
11155 1252 : jmp_target = NULL_TREE;
11156 1252 : r = t;
11157 : }
11158 558759416 : else if (!non_constant_p && jmp_target)
11159 : {
11160 24 : non_constant_p = true;
11161 24 : if (!ctx.quiet)
11162 : {
11163 3 : if (breaks (&jmp_target))
11164 3 : error ("%<break%> outside of a loop or %<switch%>");
11165 0 : else if (continues (&jmp_target))
11166 0 : error ("%<continue%> outside of a loop");
11167 0 : else if (returns (&jmp_target))
11168 0 : error ("%<return%> in a statement expression");
11169 : else
11170 0 : gcc_unreachable ();
11171 : }
11172 24 : r = t;
11173 : }
11174 :
11175 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
11176 : of statements, and the result ought to be stored in ctx.ctor. */
11177 558760668 : if (r == void_node && !constexpr_dtor && ctx.ctor)
11178 0 : r = ctx.ctor;
11179 :
11180 558760668 : unsigned int i;
11181 558760668 : tree cleanup;
11182 558760668 : jmp_target = NULL_TREE;
11183 : /* Evaluate the cleanups. */
11184 1117623752 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
11185 102416 : if (cleanup == NULL_TREE)
11186 : /* NULL_TREE cleanup is a marker that before it is
11187 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
11188 23 : --i;
11189 : else
11190 102393 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
11191 : &non_constant_p, &overflow_p,
11192 : &jmp_target);
11193 558760668 : if (throws (&jmp_target) && !non_constant_p)
11194 : {
11195 0 : if (!ctx.quiet)
11196 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11197 0 : non_constant_p = true;
11198 0 : r = t;
11199 : }
11200 :
11201 : /* Mutable logic is a bit tricky: we want to allow initialization of
11202 : constexpr variables with mutable members, but we can't copy those
11203 : members to another constexpr variable. */
11204 558760668 : if (!non_constant_p
11205 407443181 : && TREE_CODE (r) == CONSTRUCTOR
11206 575660224 : && CONSTRUCTOR_MUTABLE_POISON (r))
11207 : {
11208 90 : if (!allow_non_constant)
11209 6 : error ("%qE is not a constant expression because it refers to "
11210 : "mutable subobjects of %qT", t, type);
11211 90 : non_constant_p = true;
11212 : }
11213 :
11214 407443091 : if (!non_constant_p && cxx_dialect >= cxx20
11215 966203759 : && !global_ctx.heap_vars.is_empty ())
11216 : {
11217 124402 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11218 : NULL);
11219 124402 : unsigned int i;
11220 124402 : if (heap_var)
11221 : {
11222 113705 : if (!allow_non_constant && !non_constant_p)
11223 : {
11224 17 : auto_diagnostic_group d;
11225 17 : if (DECL_LANG_SPECIFIC (heap_var))
11226 4 : error ("%qE is not a constant expression because it refers to "
11227 : "exception object allocated with "
11228 : "%<__cxa_allocate_exception%>", t);
11229 : else
11230 13 : error ("%qE is not a constant expression because it refers to "
11231 : "a result of %<operator new%>", t);
11232 17 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11233 17 : }
11234 113705 : r = t;
11235 113705 : non_constant_p = true;
11236 : }
11237 322556 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11238 : {
11239 198154 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11240 : {
11241 114277 : if (!allow_non_constant && !non_constant_p)
11242 : {
11243 16 : auto_diagnostic_group d;
11244 16 : error ("%qE is not a constant expression because allocated "
11245 : "storage has not been deallocated", t);
11246 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11247 16 : }
11248 114277 : r = t;
11249 114277 : non_constant_p = true;
11250 : }
11251 : }
11252 : }
11253 :
11254 : /* Check that immediate invocation does not return an expression referencing
11255 : any immediate function decls. */
11256 558760668 : if (!non_constant_p && cxx_dialect >= cxx20)
11257 803233522 : if (tree immediate_fndecl
11258 401616761 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11259 : NULL))
11260 : {
11261 7447 : if (!allow_non_constant && !non_constant_p)
11262 : {
11263 74 : if (is_consteval)
11264 36 : error_at (cp_expr_loc_or_input_loc (t),
11265 : "immediate evaluation returns address of immediate "
11266 : "function %qD", immediate_fndecl);
11267 : else
11268 58 : error_at (cp_expr_loc_or_input_loc (t),
11269 : "constant evaluation returns address of immediate "
11270 : "function %qD", immediate_fndecl);
11271 : }
11272 7447 : r = t;
11273 7447 : non_constant_p = true;
11274 : }
11275 :
11276 : /* Detect consteval-only smuggling: turning a consteval-only object
11277 : into one that is not. For instance, in
11278 : struct B { };
11279 : struct D : B { info r; };
11280 : constexpr D d{^^::};
11281 : constexpr const B &b = d; // #1
11282 : #1 is wrong because D is a consteval-only type but B is not. */
11283 558760668 : if (flag_reflection
11284 11239015 : && !non_constant_p
11285 8988550 : && object
11286 1017310 : && POINTER_TYPE_P (TREE_TYPE (object))
11287 4256 : && !consteval_only_p (object)
11288 558764810 : && check_out_of_consteval_use (r, /*complain=*/false))
11289 : {
11290 30 : if (!allow_non_constant)
11291 : {
11292 10 : if (TYPE_REF_P (TREE_TYPE (object)))
11293 12 : error_at (cp_expr_loc_or_input_loc (t),
11294 : "reference into an object of consteval-only type is "
11295 : "not a constant expression unless it also has "
11296 : "consteval-only type");
11297 : else
11298 4 : error_at (cp_expr_loc_or_input_loc (t),
11299 : "pointer into an object of consteval-only type is "
11300 : "not a constant expression unless it also has "
11301 : "consteval-only type");
11302 : }
11303 30 : r = t;
11304 30 : non_constant_p = true;
11305 : }
11306 :
11307 558760668 : if (!non_constant_p && !constexpr_dtor)
11308 407321664 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11309 :
11310 : /* After verify_constant because reduced_constant_expression_p can unset
11311 : CONSTRUCTOR_NO_CLEARING. */
11312 558760668 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11313 : {
11314 69746 : if (!allow_non_constant)
11315 45 : error ("%qE is not a constant expression because it refers to "
11316 : "an incompletely initialized variable", t);
11317 69746 : TREE_CONSTANT (r) = false;
11318 69746 : non_constant_p = true;
11319 : }
11320 :
11321 558760668 : if (non_constant_p)
11322 : /* If we saw something bad, go back to our argument. The wrapping below is
11323 : only for the cases of TREE_CONSTANT argument or overflow. */
11324 153871500 : r = t;
11325 :
11326 558760668 : if (!non_constant_p && overflow_p)
11327 215 : non_constant_p = true;
11328 :
11329 : /* Unshare the result. */
11330 558760668 : bool should_unshare = true;
11331 558760668 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
11332 1511079 : && TARGET_EXPR_INITIAL (t) == r))
11333 : should_unshare = false;
11334 :
11335 558760668 : if (non_constant_p && !allow_non_constant)
11336 3921 : return error_mark_node;
11337 558756747 : else if (check_for_failed_contracts (&ctx))
11338 : {
11339 24 : if (manifestly_const_eval == mce_true)
11340 : /* If MCE, we gave a hard error and return error_mark_node. */
11341 21 : return error_mark_node;
11342 : else
11343 : {
11344 : /* Otherwise treat it as non-constant so the violation is still
11345 : detectable at run-time. */
11346 3 : gcc_checking_assert (allow_non_constant);
11347 : return t;
11348 : }
11349 : }
11350 558756723 : else if (non_constant_p && TREE_CONSTANT (r))
11351 143601 : r = mark_non_constant (r);
11352 558613122 : else if (non_constant_p)
11353 : return t;
11354 :
11355 405032530 : if (constexpr_dtor)
11356 : {
11357 165 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
11358 165 : return r;
11359 : }
11360 :
11361 : /* Check we are not trying to return the wrong type. */
11362 405032365 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
11363 : {
11364 : /* If so, this is not a constant expression. */
11365 156 : if (!allow_non_constant)
11366 4 : error ("%qE is not a constant expression because it initializes "
11367 4 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
11368 : return t;
11369 : }
11370 :
11371 405032209 : if (should_unshare)
11372 232254992 : r = unshare_expr (r);
11373 :
11374 405032209 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
11375 : {
11376 15948133 : r = adjust_temp_type (type, r);
11377 15948133 : if (TREE_CODE (t) == TARGET_EXPR
11378 15948133 : && TARGET_EXPR_INITIAL (t) == r)
11379 : return t;
11380 14989620 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
11381 2519562 : || TREE_CODE (t) == AGGR_INIT_EXPR)
11382 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
11383 836534 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
11384 764 : r = get_target_expr (r);
11385 835770 : else if (processing_template_decl)
11386 : /* Don't insert TARGET_EXPR in template trees. */
11387 : return r;
11388 : else
11389 : {
11390 835767 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
11391 835767 : TREE_CONSTANT (r) = true;
11392 : }
11393 : }
11394 :
11395 404073693 : if (TREE_CODE (t) == TARGET_EXPR
11396 552566 : && TREE_CODE (r) == TARGET_EXPR)
11397 : {
11398 : /* Preserve this flag for potential_constant_expression, and the others
11399 : for good measure. */
11400 552454 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
11401 552454 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
11402 552454 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
11403 552454 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
11404 : }
11405 :
11406 : /* Remember the original location if that wouldn't need a wrapper. */
11407 404073693 : if (location_t loc = EXPR_LOCATION (t))
11408 189362848 : protected_set_expr_location (r, loc);
11409 :
11410 404073693 : return r;
11411 568829879 : }
11412 :
11413 : /* If T represents a constant expression returns its reduced value.
11414 : Otherwise return error_mark_node. */
11415 :
11416 : tree
11417 149927723 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
11418 : tsubst_flags_t complain /* = tf_error */)
11419 : {
11420 149927723 : bool sfinae = !(complain & tf_error);
11421 149927723 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
11422 149927723 : if (sfinae && !TREE_CONSTANT (r))
11423 2559 : r = error_mark_node;
11424 149927723 : return r;
11425 : }
11426 :
11427 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
11428 : of constexpr variables. The actual initializer of DECL is not modified. */
11429 :
11430 : void
11431 200 : cxx_constant_dtor (tree t, tree decl)
11432 : {
11433 200 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
11434 200 : }
11435 :
11436 : /* Helper routine for fold_simple function. Either return simplified
11437 : expression T, otherwise NULL_TREE.
11438 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
11439 : even if we are within template-declaration. So be careful on call, as in
11440 : such case types can be undefined. */
11441 :
11442 : static tree
11443 170521903 : fold_simple_1 (tree t)
11444 : {
11445 170521903 : tree op1;
11446 170521903 : enum tree_code code = TREE_CODE (t);
11447 :
11448 170521903 : switch (code)
11449 : {
11450 : case INTEGER_CST:
11451 : case REAL_CST:
11452 : case VECTOR_CST:
11453 : case FIXED_CST:
11454 : case COMPLEX_CST:
11455 : return t;
11456 :
11457 1481901 : case SIZEOF_EXPR:
11458 1481901 : return fold_sizeof_expr (t);
11459 :
11460 40381167 : case ABS_EXPR:
11461 40381167 : case ABSU_EXPR:
11462 40381167 : case CONJ_EXPR:
11463 40381167 : case REALPART_EXPR:
11464 40381167 : case IMAGPART_EXPR:
11465 40381167 : case NEGATE_EXPR:
11466 40381167 : case BIT_NOT_EXPR:
11467 40381167 : case TRUTH_NOT_EXPR:
11468 40381167 : case VIEW_CONVERT_EXPR:
11469 40381167 : CASE_CONVERT:
11470 40381167 : case FLOAT_EXPR:
11471 40381167 : case FIX_TRUNC_EXPR:
11472 40381167 : case FIXED_CONVERT_EXPR:
11473 40381167 : case ADDR_SPACE_CONVERT_EXPR:
11474 :
11475 40381167 : op1 = TREE_OPERAND (t, 0);
11476 :
11477 40381167 : t = const_unop (code, TREE_TYPE (t), op1);
11478 40381167 : if (!t)
11479 : return NULL_TREE;
11480 :
11481 6109089 : if (CONVERT_EXPR_CODE_P (code)
11482 6109089 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
11483 0 : TREE_OVERFLOW (t) = false;
11484 : return t;
11485 :
11486 : default:
11487 : return NULL_TREE;
11488 : }
11489 : }
11490 :
11491 : /* If T is a simple constant expression, returns its simplified value.
11492 : Otherwise returns T. In contrast to maybe_constant_value we
11493 : simplify only few operations on constant-expressions, and we don't
11494 : try to simplify constexpressions. */
11495 :
11496 : tree
11497 171290620 : fold_simple (tree t)
11498 : {
11499 171290620 : if (processing_template_decl)
11500 : return t;
11501 :
11502 170521903 : tree r = fold_simple_1 (t);
11503 170521903 : if (r)
11504 119763330 : return r;
11505 :
11506 : return t;
11507 : }
11508 :
11509 : /* Try folding the expression T to a simple constant.
11510 : Returns that constant, otherwise returns T. */
11511 :
11512 : tree
11513 1679551 : fold_to_constant (tree t)
11514 : {
11515 1679551 : if (processing_template_decl)
11516 : return t;
11517 :
11518 1582155 : tree r = fold (t);
11519 1582155 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
11520 : return r;
11521 : else
11522 140870 : return t;
11523 : }
11524 :
11525 : /* If T is a constant expression, returns its reduced value.
11526 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
11527 : Otherwise, returns a version of T without TREE_CONSTANT.
11528 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
11529 : as per P0595. */
11530 :
11531 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
11532 :
11533 : tree
11534 868353333 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
11535 : mce_value manifestly_const_eval /* = mce_unknown */)
11536 : {
11537 868353333 : tree orig_t = t;
11538 868353333 : tree r;
11539 :
11540 868353333 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
11541 : {
11542 : /* Look up each operand in the cv_cache first to see if we've already
11543 : reduced it, and reuse that result to avoid quadratic behavior if
11544 : we're called when building up a large expression. */
11545 251995187 : int n = cp_tree_operand_length (t);
11546 251995187 : tree *ops = XALLOCAVEC (tree, n);
11547 251995187 : bool rebuild = false;
11548 705372552 : for (int i = 0; i < n; ++i)
11549 : {
11550 453377365 : ops[i] = TREE_OPERAND (t, i);
11551 937467066 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
11552 31352778 : if (*cached != ops[i])
11553 : {
11554 9797541 : ops[i] = *cached;
11555 9797541 : rebuild = true;
11556 : }
11557 : }
11558 251995187 : if (rebuild)
11559 : {
11560 8226001 : t = copy_node (t);
11561 26794718 : for (int i = 0; i < n; ++i)
11562 18568717 : TREE_OPERAND (t, i) = ops[i];
11563 : }
11564 : }
11565 :
11566 868353333 : if (!is_nondependent_constant_expression (t))
11567 : {
11568 0 : if (TREE_OVERFLOW_P (t)
11569 128712274 : || (!processing_template_decl && TREE_CONSTANT (t)))
11570 4498 : t = mark_non_constant (t);
11571 128712274 : return t;
11572 : }
11573 739641059 : else if (CONSTANT_CLASS_P (t))
11574 : /* No caching or evaluation needed. */
11575 : return t;
11576 :
11577 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11578 : but at least try folding it to a simple constant. */
11579 338447613 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11580 511127 : return fold_to_constant (t);
11581 :
11582 321450684 : if (manifestly_const_eval != mce_unknown)
11583 : /* TODO: Extend the cache to be mce_value aware. And if we have a
11584 : previously cached mce_unknown result that's TREE_CONSTANT, it means
11585 : the reduced value is independent of mce_value and so we should
11586 : be able to reuse it in the mce_true/false case. */
11587 173057098 : return cxx_eval_outermost_constant_expr (t, true, true,
11588 173054399 : manifestly_const_eval, false, decl);
11589 :
11590 164879388 : if (cv_cache == NULL)
11591 150540 : cv_cache = hash_map<tree, tree>::create_ggc (101);
11592 164879388 : if (tree *cached = cv_cache->get (t))
11593 : {
11594 13422914 : r = *cached;
11595 13422914 : if (r != t)
11596 : {
11597 : /* Clear processing_template_decl for sake of break_out_target_exprs;
11598 : entries in the cv_cache are non-templated. */
11599 4520690 : processing_template_decl_sentinel ptds;
11600 :
11601 4520690 : r = break_out_target_exprs (r, /*clear_loc*/true);
11602 4520690 : protected_set_expr_location (r, EXPR_LOCATION (t));
11603 4520690 : }
11604 13422914 : return r;
11605 : }
11606 :
11607 151456474 : uid_sensitive_constexpr_evaluation_checker c;
11608 151456474 : r = cxx_eval_outermost_constant_expr (t, true, true,
11609 : manifestly_const_eval, false, decl);
11610 151456474 : gcc_checking_assert (r == t
11611 : || CONVERT_EXPR_P (t)
11612 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11613 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11614 : || !cp_tree_equal (r, t));
11615 151456474 : if (!c.evaluation_restricted_p ())
11616 151200005 : cv_cache->put (orig_t, r);
11617 : return r;
11618 : }
11619 :
11620 : /* Dispose of the whole CV_CACHE. */
11621 :
11622 : static void
11623 41217916 : clear_cv_cache (void)
11624 : {
11625 41217916 : if (cv_cache != NULL)
11626 40910369 : cv_cache->empty ();
11627 41217916 : }
11628 :
11629 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
11630 :
11631 : void
11632 41217916 : clear_cv_and_fold_caches ()
11633 : {
11634 41217916 : clear_cv_cache ();
11635 41217916 : clear_fold_cache ();
11636 41217916 : }
11637 :
11638 : /* Internal function handling expressions in templates for
11639 : fold_non_dependent_expr and fold_non_dependent_init.
11640 :
11641 : If we're in a template, but T isn't value dependent, simplify
11642 : it. We're supposed to treat:
11643 :
11644 : template <typename T> void f(T[1 + 1]);
11645 : template <typename T> void f(T[2]);
11646 :
11647 : as two declarations of the same function, for example. */
11648 :
11649 : static tree
11650 31702409 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11651 : bool manifestly_const_eval,
11652 : tree object)
11653 : {
11654 31702409 : gcc_assert (processing_template_decl);
11655 :
11656 31702409 : if (is_nondependent_constant_expression (t))
11657 : {
11658 17644160 : processing_template_decl_sentinel s;
11659 17644160 : t = instantiate_non_dependent_expr_internal (t, complain);
11660 :
11661 17644160 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11662 : {
11663 0 : if (TREE_OVERFLOW_P (t))
11664 : {
11665 0 : t = build_nop (TREE_TYPE (t), t);
11666 0 : TREE_CONSTANT (t) = false;
11667 : }
11668 : return t;
11669 : }
11670 17644160 : else if (CONSTANT_CLASS_P (t))
11671 : /* No evaluation needed. */
11672 : return t;
11673 :
11674 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11675 : but at least try folding it to a simple constant. */
11676 4684660 : if (cp_unevaluated_operand && !manifestly_const_eval)
11677 89 : return fold_to_constant (t);
11678 :
11679 4684571 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11680 : mce_value (manifestly_const_eval),
11681 : false, object);
11682 : /* cp_tree_equal looks through NOPs, so allow them. */
11683 4684571 : gcc_checking_assert (r == t
11684 : || CONVERT_EXPR_P (t)
11685 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11686 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11687 : || !cp_tree_equal (r, t));
11688 : return r;
11689 17644160 : }
11690 14058249 : else if (TREE_OVERFLOW_P (t))
11691 : {
11692 0 : t = build_nop (TREE_TYPE (t), t);
11693 0 : TREE_CONSTANT (t) = false;
11694 : }
11695 :
11696 : return t;
11697 : }
11698 :
11699 : /* Like maybe_constant_value but first fully instantiate the argument.
11700 :
11701 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11702 : followed by maybe_constant_value but is more efficient,
11703 : because it calls instantiation_dependent_expression_p and
11704 : potential_constant_expression at most once.
11705 : The manifestly_const_eval argument is passed to maybe_constant_value.
11706 :
11707 : Callers should generally pass their active complain, or if they are in a
11708 : non-template, diagnosing context, they can use the default of
11709 : tf_warning_or_error. Callers that might be within a template context, don't
11710 : have a complain parameter, and aren't going to remember the result for long
11711 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11712 : appropriately. */
11713 :
11714 : tree
11715 265744397 : fold_non_dependent_expr (tree t,
11716 : tsubst_flags_t complain /* = tf_warning_or_error */,
11717 : bool manifestly_const_eval /* = false */,
11718 : tree object /* = NULL_TREE */)
11719 : {
11720 265744397 : if (t == NULL_TREE)
11721 : return NULL_TREE;
11722 :
11723 264618289 : if (processing_template_decl)
11724 30569702 : return fold_non_dependent_expr_template (t, complain,
11725 30569702 : manifestly_const_eval, object);
11726 :
11727 234048587 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11728 : }
11729 :
11730 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11731 : return the original expression. */
11732 :
11733 : tree
11734 2401582 : maybe_fold_non_dependent_expr (tree expr,
11735 : tsubst_flags_t complain/*=tf_warning_or_error*/)
11736 : {
11737 2401582 : tree t = fold_non_dependent_expr (expr, complain);
11738 2401582 : if (t && TREE_CONSTANT (t))
11739 1153849 : return t;
11740 :
11741 : return expr;
11742 : }
11743 :
11744 : /* Like maybe_constant_init but first fully instantiate the argument. */
11745 :
11746 : tree
11747 62875180 : fold_non_dependent_init (tree t,
11748 : tsubst_flags_t complain /*=tf_warning_or_error*/,
11749 : bool manifestly_const_eval /*=false*/,
11750 : tree object /* = NULL_TREE */)
11751 : {
11752 62875180 : if (t == NULL_TREE)
11753 : return NULL_TREE;
11754 :
11755 62875180 : if (processing_template_decl)
11756 : {
11757 1132707 : t = fold_non_dependent_expr_template (t, complain,
11758 : manifestly_const_eval, object);
11759 : /* maybe_constant_init does this stripping, so do it here too. */
11760 1132707 : if (TREE_CODE (t) == TARGET_EXPR)
11761 : {
11762 2107 : tree init = TARGET_EXPR_INITIAL (t);
11763 2107 : if (TREE_CODE (init) == CONSTRUCTOR)
11764 62875180 : t = init;
11765 : }
11766 : return t;
11767 : }
11768 :
11769 61742473 : return maybe_constant_init (t, object, manifestly_const_eval);
11770 : }
11771 :
11772 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11773 : than wrapped in a TARGET_EXPR.
11774 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11775 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11776 : per P0595 even when ALLOW_NON_CONSTANT is true. */
11777 :
11778 : static tree
11779 175568866 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11780 : mce_value manifestly_const_eval)
11781 : {
11782 175568866 : if (!t)
11783 : return t;
11784 175568866 : if (TREE_CODE (t) == EXPR_STMT)
11785 255 : t = TREE_OPERAND (t, 0);
11786 175568866 : if (TREE_CODE (t) == CONVERT_EXPR
11787 175568866 : && VOID_TYPE_P (TREE_TYPE (t)))
11788 2816 : t = TREE_OPERAND (t, 0);
11789 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11790 : DECL and losing that information would cause mischief later. */
11791 175568866 : if (TREE_CODE (t) == INIT_EXPR
11792 175568866 : && (!decl
11793 2579 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11794 2579 : TREE_TYPE (t))))
11795 1679 : t = TREE_OPERAND (t, 1);
11796 175568866 : if (TREE_CODE (t) == TARGET_EXPR)
11797 7871144 : t = TARGET_EXPR_INITIAL (t);
11798 175568866 : if (!is_nondependent_static_init_expression (t))
11799 : /* Don't try to evaluate it. */;
11800 141864728 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11801 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11802 : else
11803 : {
11804 : /* [basic.start.static] allows constant-initialization of variables with
11805 : static or thread storage duration even if it isn't required, but we
11806 : shouldn't bend the rules the same way for automatic variables.
11807 :
11808 : But still enforce the requirements of constexpr/constinit.
11809 : [dcl.constinit] "If a variable declared with the constinit specifier
11810 : has dynamic initialization, the program is ill-formed, even if the
11811 : implementation would perform that initialization as a static
11812 : initialization." */
11813 43302951 : bool is_static = (decl && DECL_P (decl)
11814 103311428 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11815 : bool strict = (!is_static
11816 : || (decl && DECL_P (decl)
11817 3441759 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11818 992005 : || DECL_DECLARED_CONSTINIT_P (decl))));
11819 : if (is_static)
11820 : manifestly_const_eval = mce_true;
11821 :
11822 65770175 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11823 89099 : return fold_to_constant (t);
11824 :
11825 65681076 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11826 : manifestly_const_eval,
11827 : false, decl);
11828 : }
11829 175479767 : if (TREE_CODE (t) == TARGET_EXPR)
11830 : {
11831 283519 : tree init = TARGET_EXPR_INITIAL (t);
11832 283519 : if (TREE_CODE (init) == CONSTRUCTOR)
11833 175568866 : t = init;
11834 : }
11835 : return t;
11836 : }
11837 :
11838 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11839 :
11840 : tree
11841 96113096 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11842 : {
11843 96113096 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11844 : }
11845 :
11846 : tree
11847 79453662 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11848 : {
11849 79453662 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11850 : }
11851 :
11852 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11853 :
11854 : tree
11855 2108 : cxx_constant_init (tree t, tree decl)
11856 : {
11857 2108 : return maybe_constant_init_1 (t, decl, false, mce_true);
11858 : }
11859 :
11860 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11861 :
11862 : static bool
11863 231853475 : callee_might_throw (tree t)
11864 : {
11865 231853475 : if (cxx_dialect < cxx26 || !flag_exceptions)
11866 : return false;
11867 36517511 : tree callee = cp_get_callee (t);
11868 36517511 : if (callee == NULL_TREE)
11869 : return false;
11870 36461045 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11871 36461045 : return (!flag_enforce_eh_specs
11872 36461045 : || type_dependent_expression_p (callee)
11873 33454495 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11874 67737667 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11875 14741794 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11876 : }
11877 :
11878 : #if 0
11879 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11880 : /* Return true if the object referred to by REF has automatic or thread
11881 : local storage. */
11882 :
11883 : enum { ck_ok, ck_bad, ck_unknown };
11884 : static int
11885 : check_automatic_or_tls (tree ref)
11886 : {
11887 : machine_mode mode;
11888 : poly_int64 bitsize, bitpos;
11889 : tree offset;
11890 : int volatilep = 0, unsignedp = 0;
11891 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11892 : &mode, &unsignedp, &volatilep, false);
11893 : duration_kind dk;
11894 :
11895 : /* If there isn't a decl in the middle, we don't know the linkage here,
11896 : and this isn't a constant expression anyway. */
11897 : if (!DECL_P (decl))
11898 : return ck_unknown;
11899 : dk = decl_storage_duration (decl);
11900 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11901 : }
11902 : #endif
11903 :
11904 : /* Data structure for passing data from potential_constant_expression_1
11905 : to check_for_return_continue via cp_walk_tree. */
11906 : struct check_for_return_continue_data {
11907 : hash_set<tree> *pset;
11908 : tree continue_stmt;
11909 : tree break_stmt;
11910 : bool could_throw;
11911 : };
11912 :
11913 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11914 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11915 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11916 : For C++26 also note presence of possibly throwing calls. */
11917 : static tree
11918 51556807 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11919 : {
11920 51556807 : tree t = *tp, s, b;
11921 51556807 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11922 51556807 : switch (TREE_CODE (t))
11923 : {
11924 : case RETURN_EXPR:
11925 : return t;
11926 :
11927 28 : case CONTINUE_STMT:
11928 28 : if (d->continue_stmt == NULL_TREE)
11929 28 : d->continue_stmt = t;
11930 : break;
11931 :
11932 3212 : case BREAK_STMT:
11933 3212 : if (d->break_stmt == NULL_TREE)
11934 1337 : d->break_stmt = t;
11935 : break;
11936 :
11937 : #define RECUR(x) \
11938 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11939 : d->pset)) \
11940 : return r
11941 :
11942 : /* For loops, walk subtrees manually, so that continue stmts found
11943 : inside of the bodies of the loops are ignored. */
11944 42269 : case DO_STMT:
11945 42269 : *walk_subtrees = 0;
11946 42269 : RECUR (DO_COND (t));
11947 42269 : s = d->continue_stmt;
11948 42269 : b = d->break_stmt;
11949 42269 : RECUR (DO_BODY (t));
11950 42269 : d->continue_stmt = s;
11951 42269 : d->break_stmt = b;
11952 42269 : break;
11953 :
11954 123 : case WHILE_STMT:
11955 123 : *walk_subtrees = 0;
11956 123 : RECUR (WHILE_COND_PREP (t));
11957 123 : RECUR (WHILE_COND (t));
11958 123 : s = d->continue_stmt;
11959 123 : b = d->break_stmt;
11960 123 : RECUR (WHILE_BODY (t));
11961 109 : d->continue_stmt = s;
11962 109 : d->break_stmt = b;
11963 109 : break;
11964 :
11965 378 : case FOR_STMT:
11966 378 : *walk_subtrees = 0;
11967 378 : RECUR (FOR_INIT_STMT (t));
11968 378 : RECUR (FOR_COND_PREP (t));
11969 378 : RECUR (FOR_COND (t));
11970 378 : RECUR (FOR_EXPR (t));
11971 378 : s = d->continue_stmt;
11972 378 : b = d->break_stmt;
11973 378 : RECUR (FOR_BODY (t));
11974 348 : d->continue_stmt = s;
11975 348 : d->break_stmt = b;
11976 348 : break;
11977 :
11978 0 : case RANGE_FOR_STMT:
11979 0 : *walk_subtrees = 0;
11980 0 : RECUR (RANGE_FOR_EXPR (t));
11981 0 : s = d->continue_stmt;
11982 0 : b = d->break_stmt;
11983 0 : RECUR (RANGE_FOR_BODY (t));
11984 0 : d->continue_stmt = s;
11985 0 : d->break_stmt = b;
11986 0 : break;
11987 :
11988 209 : case SWITCH_STMT:
11989 209 : *walk_subtrees = 0;
11990 209 : RECUR (SWITCH_STMT_COND (t));
11991 209 : b = d->break_stmt;
11992 209 : RECUR (SWITCH_STMT_BODY (t));
11993 189 : d->break_stmt = b;
11994 189 : break;
11995 : #undef RECUR
11996 :
11997 : case STATEMENT_LIST:
11998 : case CONSTRUCTOR:
11999 : break;
12000 :
12001 2653990 : case AGGR_INIT_EXPR:
12002 2653990 : case CALL_EXPR:
12003 : /* In C++26 a function could throw. */
12004 2653990 : if (callee_might_throw (t))
12005 125901 : d->could_throw = true;
12006 : break;
12007 :
12008 47442867 : default:
12009 47442867 : if (!EXPR_P (t))
12010 14716112 : *walk_subtrees = 0;
12011 : break;
12012 : }
12013 :
12014 : return NULL_TREE;
12015 : }
12016 :
12017 : /* Return true if T denotes a potentially constant expression. Issue
12018 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
12019 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
12020 : consider the expression in the current context, independent of constexpr
12021 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
12022 : and hard errors should not be reported by constexpr_error.
12023 :
12024 : C++0x [expr.const] used to say
12025 :
12026 : 6 An expression is a potential constant expression if it is
12027 : a constant expression where all occurrences of function
12028 : parameters are replaced by arbitrary constant expressions
12029 : of the appropriate type.
12030 :
12031 : 2 A conditional expression is a constant expression unless it
12032 : involves one of the following as a potentially evaluated
12033 : subexpression (3.2), but subexpressions of logical AND (5.14),
12034 : logical OR (5.15), and conditional (5.16) operations that are
12035 : not evaluated are not considered. */
12036 :
12037 : static bool
12038 4964701019 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
12039 : bool fundef_p, tsubst_flags_t flags,
12040 : tree *jump_target)
12041 : {
12042 : #define RECUR(T,RV) \
12043 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
12044 : jump_target)
12045 :
12046 4964701019 : enum { any = false, rval = true };
12047 4964701019 : int i;
12048 4964701019 : tree tmp;
12049 :
12050 4964701019 : if (t == error_mark_node)
12051 : return false;
12052 4964688735 : if (t == NULL_TREE)
12053 : return true;
12054 4956514511 : location_t loc = cp_expr_loc_or_input_loc (t);
12055 4956514511 : iloc_sentinel ils = loc;
12056 :
12057 4956514511 : if (*jump_target)
12058 : /* If we are jumping, ignore everything. This is simpler than the
12059 : cxx_eval_constant_expression handling because we only need to be
12060 : conservatively correct, and we don't necessarily have a constant value
12061 : available, so we don't bother with switch tracking. */
12062 : return true;
12063 :
12064 2239067 : if (TREE_THIS_VOLATILE (t) && want_rval
12065 1264734 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
12066 4947029999 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
12067 : {
12068 78475 : if (TREE_CLOBBER_P (t))
12069 : {
12070 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
12071 0 : gcc_checking_assert (false);
12072 : return true;
12073 : }
12074 :
12075 78475 : if (flags & tf_error)
12076 25 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
12077 : "a volatile lvalue %qE with type %qT", t,
12078 25 : TREE_TYPE (t));
12079 : return false;
12080 : }
12081 4946873022 : if (CONSTANT_CLASS_P (t))
12082 : return true;
12083 3604960930 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
12084 3604960930 : && TREE_TYPE (t) == error_mark_node)
12085 : return false;
12086 :
12087 3604960887 : switch (TREE_CODE (t))
12088 : {
12089 : case FUNCTION_DECL:
12090 : case BASELINK:
12091 : case TEMPLATE_DECL:
12092 : case OVERLOAD:
12093 : case TEMPLATE_ID_EXPR:
12094 : case LABEL_DECL:
12095 : case CASE_LABEL_EXPR:
12096 : case PREDICT_EXPR:
12097 : case CONST_DECL:
12098 : case SIZEOF_EXPR:
12099 : case ALIGNOF_EXPR:
12100 : case OFFSETOF_EXPR:
12101 : case NOEXCEPT_EXPR:
12102 : case TEMPLATE_PARM_INDEX:
12103 : case TRAIT_EXPR:
12104 : case IDENTIFIER_NODE:
12105 : case USERDEF_LITERAL:
12106 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
12107 : case FIELD_DECL:
12108 : case RESULT_DECL:
12109 : case USING_DECL:
12110 : case USING_STMT:
12111 : case PLACEHOLDER_EXPR:
12112 : case REQUIRES_EXPR:
12113 : case STATIC_ASSERT:
12114 : case DEBUG_BEGIN_STMT:
12115 : case REFLECT_EXPR:
12116 : return true;
12117 :
12118 24974345 : case RETURN_EXPR:
12119 24974345 : if (!RECUR (TREE_OPERAND (t, 0), any))
12120 : return false;
12121 : /* FALLTHROUGH */
12122 :
12123 24806490 : case BREAK_STMT:
12124 24806490 : case CONTINUE_STMT:
12125 24806490 : *jump_target = t;
12126 24806490 : return true;
12127 :
12128 394873351 : case PARM_DECL:
12129 394873351 : if (now && want_rval)
12130 : {
12131 101472460 : tree type = TREE_TYPE (t);
12132 101472460 : if (dependent_type_p (type)
12133 72504619 : || !COMPLETE_TYPE_P (processing_template_decl
12134 : ? type : complete_type (type))
12135 173977079 : || is_really_empty_class (type, /*ignore_vptr*/false))
12136 : /* An empty class has no data to read. */
12137 : return true;
12138 72503913 : if (flags & tf_error)
12139 19 : constexpr_error (input_location, fundef_p,
12140 : "%qE is not a constant expression", t);
12141 : return false;
12142 : }
12143 : return true;
12144 :
12145 286167352 : case AGGR_INIT_EXPR:
12146 286167352 : case CALL_EXPR:
12147 : /* -- an invocation of a function other than a constexpr function
12148 : or a constexpr constructor. */
12149 286167352 : {
12150 286167352 : tree fun = get_function_named_in_call (t);
12151 286167352 : const int nargs = call_expr_nargs (t);
12152 286167352 : i = 0;
12153 :
12154 286167352 : if (fun == NULL_TREE)
12155 : {
12156 : /* Reset to allow the function to continue past the end
12157 : of the block below. Otherwise return early. */
12158 426918 : bool bail = true;
12159 :
12160 426918 : if (TREE_CODE (t) == CALL_EXPR
12161 426918 : && CALL_EXPR_FN (t) == NULL_TREE)
12162 426918 : switch (CALL_EXPR_IFN (t))
12163 : {
12164 : /* These should be ignored, they are optimized away from
12165 : constexpr functions. */
12166 : case IFN_UBSAN_NULL:
12167 : case IFN_UBSAN_BOUNDS:
12168 : case IFN_UBSAN_VPTR:
12169 : case IFN_FALLTHROUGH:
12170 : case IFN_ASSUME:
12171 : return true;
12172 :
12173 : case IFN_ADD_OVERFLOW:
12174 : case IFN_SUB_OVERFLOW:
12175 : case IFN_MUL_OVERFLOW:
12176 : case IFN_LAUNDER:
12177 : case IFN_VEC_CONVERT:
12178 : case IFN_BSWAP:
12179 : case IFN_BITREVERSE:
12180 : bail = false;
12181 : break;
12182 :
12183 : default:
12184 : break;
12185 : }
12186 :
12187 : if (bail)
12188 : {
12189 : /* fold_call_expr can't do anything with IFN calls. */
12190 172 : if (flags & tf_error)
12191 0 : constexpr_error (loc, fundef_p,
12192 : "call to internal function %qE", t);
12193 : return false;
12194 : }
12195 : }
12196 :
12197 285740434 : if (fun && is_overloaded_fn (fun))
12198 : {
12199 266882292 : if (!RECUR (fun, true))
12200 : return false;
12201 266456396 : fun = get_fns (fun);
12202 :
12203 266456396 : if (TREE_CODE (fun) == FUNCTION_DECL)
12204 : {
12205 248522611 : if (builtin_valid_in_constant_expr_p (fun))
12206 : return true;
12207 246700485 : if (!maybe_constexpr_fn (fun)
12208 : /* Allow any built-in function; if the expansion
12209 : isn't constant, we'll deal with that then. */
12210 55415637 : && !fndecl_built_in_p (fun)
12211 : /* In C++20, replaceable global allocation functions
12212 : are constant expressions. */
12213 37809000 : && (!cxx_replaceable_global_alloc_fn (fun)
12214 992139 : || TREE_CODE (t) != CALL_EXPR
12215 992139 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
12216 362564 : && (current_function_decl == NULL_TREE
12217 362564 : || !is_std_allocator_allocate
12218 362564 : (current_function_decl))))
12219 : /* Allow placement new in std::construct_at. */
12220 36823911 : && (!cxx_placement_new_fn (fun)
12221 402928 : || TREE_CODE (t) != CALL_EXPR
12222 402928 : || current_function_decl == NULL_TREE
12223 402916 : || !is_std_construct_at (current_function_decl))
12224 36551489 : && !cxx_dynamic_cast_fn_p (fun)
12225 283248363 : && !cxx_cxa_builtin_fn_p (fun))
12226 : {
12227 : /* In C++26 evaluation of the function arguments might
12228 : throw and in that case it is irrelevant whether
12229 : fun is constexpr or not. */
12230 36495304 : if (cxx_dialect >= cxx26)
12231 8029619 : for (; i < nargs; ++i)
12232 : {
12233 4818585 : tree x = get_nth_callarg (t, i);
12234 4818585 : bool rv = processing_template_decl ? any : rval;
12235 4818585 : bool sub_now = false;
12236 4818585 : if (!potential_constant_expression_1 (x, rv, strict,
12237 : sub_now,
12238 : fundef_p,
12239 : flags,
12240 : jump_target))
12241 : return false;
12242 4531812 : if (throws (jump_target))
12243 : return true;
12244 : }
12245 36179385 : if ((flags & tf_error)
12246 36179385 : && constexpr_error (loc, fundef_p,
12247 : "call to non-%<constexpr%> "
12248 : "function %qD", fun))
12249 272 : explain_invalid_constexpr_fn (fun);
12250 : return false;
12251 : }
12252 : }
12253 :
12254 228138966 : fun = OVL_FIRST (fun);
12255 : /* Skip initial arguments to base constructors. */
12256 228138966 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12257 4178133 : i = num_artificial_parms_for (fun);
12258 : }
12259 18858142 : else if (fun)
12260 : {
12261 18858142 : if (TREE_TYPE (fun)
12262 18858142 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12263 : want_rval = rval;
12264 : else
12265 : want_rval = any;
12266 18858142 : if (RECUR (fun, want_rval))
12267 : /* Might end up being a constant function pointer. But it
12268 : could also be a function object with constexpr op(), so
12269 : we pass 'any' so that the underlying VAR_DECL is deemed
12270 : as potentially-constant even though it wasn't declared
12271 : constexpr. */;
12272 : else
12273 : return false;
12274 : }
12275 548279455 : for (; i < nargs; ++i)
12276 : {
12277 319025167 : tree x = get_nth_callarg (t, i);
12278 : /* In a template, reference arguments haven't been converted to
12279 : REFERENCE_TYPE and we might not even know if the parameter
12280 : is a reference, so accept lvalue constants too. */
12281 319025167 : bool rv = processing_template_decl ? any : rval;
12282 : /* Don't require an immediately constant value, as constexpr
12283 : substitution might not use the value of the argument. */
12284 319025167 : bool sub_now = false;
12285 319025167 : if (!potential_constant_expression_1 (x, rv, strict,
12286 : sub_now, fundef_p, flags,
12287 : jump_target))
12288 : return false;
12289 3229973469 : if (throws (jump_target))
12290 : return true;
12291 : }
12292 : /* In C++26 a function could throw. */
12293 229254288 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12294 11582065 : *jump_target = void_node;
12295 : return true;
12296 : }
12297 :
12298 203335307 : case NON_LVALUE_EXPR:
12299 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
12300 : -- an lvalue of integral type that refers to a non-volatile
12301 : const variable or static data member initialized with
12302 : constant expressions, or
12303 :
12304 : -- an lvalue of literal type that refers to non-volatile
12305 : object defined with constexpr, or that refers to a
12306 : sub-object of such an object; */
12307 203335307 : return RECUR (TREE_OPERAND (t, 0), rval);
12308 :
12309 70631 : case EXCESS_PRECISION_EXPR:
12310 70631 : return RECUR (TREE_OPERAND (t, 0), rval);
12311 :
12312 315330596 : case VAR_DECL:
12313 315330596 : if (DECL_HAS_VALUE_EXPR_P (t))
12314 : {
12315 5231170 : if (now && is_normal_capture_proxy (t))
12316 : {
12317 : /* -- in a lambda-expression, a reference to this or to a
12318 : variable with automatic storage duration defined outside that
12319 : lambda-expression, where the reference would be an
12320 : odr-use. */
12321 :
12322 470205 : if (want_rval)
12323 : /* Since we're doing an lvalue-rvalue conversion, this might
12324 : not be an odr-use, so evaluate the variable directly. */
12325 463253 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
12326 :
12327 6952 : if (flags & tf_error)
12328 : {
12329 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
12330 3 : auto_diagnostic_group d;
12331 3 : if (constexpr_error (input_location, fundef_p,
12332 : "lambda capture of %qE is not a "
12333 : "constant expression", cap)
12334 3 : && decl_constant_var_p (cap))
12335 3 : inform (input_location, "because it is used as a glvalue");
12336 3 : }
12337 : return false;
12338 : }
12339 4760965 : tree ve = DECL_VALUE_EXPR (t);
12340 : /* Treat __PRETTY_FUNCTION__ inside a template function as
12341 : potentially-constant. */
12342 4760965 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
12343 : return true;
12344 4760960 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
12345 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
12346 4758224 : return RECUR (ve, rval);
12347 : }
12348 310099426 : if (want_rval
12349 219074118 : && (now || !var_in_maybe_constexpr_fn (t))
12350 199671817 : && !type_dependent_expression_p (t)
12351 167789939 : && !decl_maybe_constant_var_p (t)
12352 55077038 : && !is_local_temp (t)
12353 54502631 : && (strict
12354 4111585 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
12355 1634520 : || (DECL_INITIAL (t)
12356 1165514 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
12357 53994542 : && COMPLETE_TYPE_P (TREE_TYPE (t))
12358 364093816 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
12359 : {
12360 53989559 : if (flags & tf_error)
12361 171 : non_const_var_error (loc, t, fundef_p);
12362 : return false;
12363 : }
12364 : return true;
12365 :
12366 449015294 : case NOP_EXPR:
12367 449015294 : if (REINTERPRET_CAST_P (t))
12368 : {
12369 163623 : if (flags & tf_error)
12370 49 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
12371 : "constant expression");
12372 : return false;
12373 : }
12374 : /* FALLTHRU */
12375 904045231 : case CONVERT_EXPR:
12376 904045231 : case VIEW_CONVERT_EXPR:
12377 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
12378 : may change to something more specific to type-punning (DR 1312). */
12379 904045231 : {
12380 904045231 : tree from = TREE_OPERAND (t, 0);
12381 904045231 : if (location_wrapper_p (t))
12382 371122122 : return (RECUR (from, want_rval));
12383 532923109 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
12384 : {
12385 303959829 : STRIP_ANY_LOCATION_WRAPPER (from);
12386 303959829 : if (TREE_CODE (from) == INTEGER_CST
12387 303959829 : && !integer_zerop (from))
12388 : {
12389 1629 : if (flags & tf_error)
12390 28 : constexpr_error (loc, fundef_p,
12391 : "%<reinterpret_cast%> from integer to "
12392 : "pointer");
12393 : return false;
12394 : }
12395 : }
12396 532921480 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
12397 : }
12398 :
12399 12956 : case ADDRESSOF_EXPR:
12400 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
12401 12956 : t = TREE_OPERAND (t, 0);
12402 12956 : goto handle_addr_expr;
12403 :
12404 100549042 : case ADDR_EXPR:
12405 : /* -- a unary operator & that is applied to an lvalue that
12406 : designates an object with thread or automatic storage
12407 : duration; */
12408 100549042 : t = TREE_OPERAND (t, 0);
12409 :
12410 100549042 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
12411 : /* A pointer-to-member constant. */
12412 : return true;
12413 :
12414 100561732 : handle_addr_expr:
12415 : #if 0
12416 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
12417 : any checking here, as we might dereference the pointer later. If
12418 : we remove this code, also remove check_automatic_or_tls. */
12419 : i = check_automatic_or_tls (t);
12420 : if (i == ck_ok)
12421 : return true;
12422 : if (i == ck_bad)
12423 : {
12424 : if (flags & tf_error)
12425 : error ("address-of an object %qE with thread local or "
12426 : "automatic storage is not a constant expression", t);
12427 : return false;
12428 : }
12429 : #endif
12430 100561732 : return RECUR (t, any);
12431 :
12432 107390665 : case COMPONENT_REF:
12433 107390665 : case ARROW_EXPR:
12434 107390665 : case OFFSET_REF:
12435 : /* -- a class member access unless its postfix-expression is
12436 : of literal type or of pointer to literal type. */
12437 : /* This test would be redundant, as it follows from the
12438 : postfix-expression being a potential constant expression. */
12439 107390665 : if (type_unknown_p (t))
12440 : return true;
12441 99414262 : if (is_overloaded_fn (t))
12442 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
12443 : which uses ob as an lvalue. */
12444 101926404 : want_rval = false;
12445 101926404 : gcc_fallthrough ();
12446 :
12447 101926404 : case REALPART_EXPR:
12448 101926404 : case IMAGPART_EXPR:
12449 101926404 : case BIT_FIELD_REF:
12450 101926404 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12451 :
12452 295319 : case EXPR_PACK_EXPANSION:
12453 295319 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
12454 :
12455 : case PACK_INDEX_EXPR:
12456 : return true;
12457 :
12458 113003615 : case INDIRECT_REF:
12459 113003615 : return RECUR (TREE_OPERAND (t, 0), rval);
12460 :
12461 25629036 : case STATEMENT_LIST:
12462 5057410768 : for (tree stmt : tsi_range (t))
12463 75794312 : if (!RECUR (stmt, any))
12464 4956514511 : return false;
12465 : return true;
12466 :
12467 5172925 : case MODIFY_EXPR:
12468 5172925 : if (cxx_dialect < cxx14)
12469 3892 : goto fail;
12470 5169033 : if (!RECUR (TREE_OPERAND (t, 0), any))
12471 : return false;
12472 : /* Just ignore clobbers. */
12473 4311179 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12474 : return true;
12475 3829387 : if (!RECUR (TREE_OPERAND (t, 1), rval))
12476 : return false;
12477 : return true;
12478 :
12479 907937 : case MODOP_EXPR:
12480 907937 : if (cxx_dialect < cxx14)
12481 98 : goto fail;
12482 907839 : if (!RECUR (TREE_OPERAND (t, 0), rval))
12483 : return false;
12484 897256 : if (!RECUR (TREE_OPERAND (t, 2), rval))
12485 : return false;
12486 : return true;
12487 :
12488 551563 : case DO_STMT:
12489 551563 : if (!RECUR (DO_COND (t), rval))
12490 : return false;
12491 551563 : if (!RECUR (DO_BODY (t), any))
12492 : return false;
12493 550948 : if (breaks (jump_target) || continues (jump_target))
12494 5 : *jump_target = NULL_TREE;
12495 : return true;
12496 :
12497 454469 : case FOR_STMT:
12498 454469 : if (!RECUR (FOR_INIT_STMT (t), any))
12499 : return false;
12500 454469 : if (!RECUR (FOR_COND_PREP (t), any))
12501 : return false;
12502 454469 : tmp = FOR_COND (t);
12503 454469 : if (!RECUR (tmp, rval))
12504 : return false;
12505 454395 : if (tmp)
12506 : {
12507 405853 : if (!processing_template_decl)
12508 401325 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12509 : /* If we couldn't evaluate the condition, it might not ever be
12510 : true. */
12511 405853 : if (!integer_onep (tmp))
12512 : {
12513 : /* Before returning true, check if the for body can contain
12514 : a return. */
12515 405853 : hash_set<tree> pset;
12516 405853 : check_for_return_continue_data data = { &pset, NULL_TREE,
12517 405853 : NULL_TREE, false };
12518 405853 : if (tree ret_expr
12519 405853 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
12520 : &data, &pset))
12521 141144 : *jump_target = ret_expr;
12522 405853 : if (data.could_throw)
12523 17802 : *jump_target = void_node;
12524 405853 : return true;
12525 405853 : }
12526 : }
12527 48542 : if (!RECUR (FOR_EXPR (t), any))
12528 : return false;
12529 48542 : if (!RECUR (FOR_BODY (t), any))
12530 : return false;
12531 48542 : if (!RECUR (FOR_COND_CLEANUP (t), any))
12532 : return false;
12533 48542 : if (breaks (jump_target) || continues (jump_target))
12534 12 : *jump_target = NULL_TREE;
12535 : return true;
12536 :
12537 478 : case RANGE_FOR_STMT:
12538 478 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
12539 : return false;
12540 478 : if (!RECUR (RANGE_FOR_EXPR (t), any))
12541 : return false;
12542 478 : if (!RECUR (RANGE_FOR_BODY (t), any))
12543 : return false;
12544 476 : if (breaks (jump_target) || continues (jump_target))
12545 0 : *jump_target = NULL_TREE;
12546 : return true;
12547 :
12548 168808 : case WHILE_STMT:
12549 168808 : if (!RECUR (WHILE_COND_PREP (t), any))
12550 : return false;
12551 168808 : tmp = WHILE_COND (t);
12552 168808 : if (!RECUR (tmp, rval))
12553 : return false;
12554 168744 : if (!processing_template_decl)
12555 168707 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12556 : /* If we couldn't evaluate the condition, it might not ever be true. */
12557 168744 : if (!integer_onep (tmp))
12558 : {
12559 : /* Before returning true, check if the while body can contain
12560 : a return. */
12561 158569 : hash_set<tree> pset;
12562 158569 : check_for_return_continue_data data = { &pset, NULL_TREE,
12563 158569 : NULL_TREE, false };
12564 158569 : if (tree ret_expr
12565 158569 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
12566 : &data, &pset))
12567 468 : *jump_target = ret_expr;
12568 158569 : if (data.could_throw)
12569 4605 : *jump_target = void_node;
12570 158569 : return true;
12571 158569 : }
12572 10175 : if (!RECUR (WHILE_BODY (t), any))
12573 : return false;
12574 10164 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
12575 : return false;
12576 10164 : if (breaks (jump_target) || continues (jump_target))
12577 24 : *jump_target = NULL_TREE;
12578 : return true;
12579 :
12580 43015 : case SWITCH_STMT:
12581 43015 : if (!RECUR (SWITCH_STMT_COND (t), rval))
12582 : return false;
12583 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
12584 : unreachable labels would be checked and it is enough if there is
12585 : a single switch cond value for which it is a valid constant
12586 : expression. We need to check if there are any RETURN_EXPRs
12587 : or CONTINUE_STMTs inside of the body though, as in that case
12588 : we need to set *jump_target. */
12589 : else
12590 : {
12591 43009 : hash_set<tree> pset;
12592 43009 : check_for_return_continue_data data = { &pset, NULL_TREE,
12593 43009 : NULL_TREE, false };
12594 43009 : if (tree ret_expr
12595 43009 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
12596 : &data, &pset))
12597 : /* The switch might return. */
12598 42622 : *jump_target = ret_expr;
12599 387 : else if (data.continue_stmt)
12600 : /* The switch can't return, but might continue. */
12601 3 : *jump_target = data.continue_stmt;
12602 43009 : if (data.could_throw)
12603 1853 : *jump_target = void_node;
12604 43009 : }
12605 43009 : return true;
12606 :
12607 50 : case STMT_EXPR:
12608 50 : return RECUR (STMT_EXPR_STMT (t), rval);
12609 :
12610 625795 : case LAMBDA_EXPR:
12611 625795 : if (cxx_dialect >= cxx17)
12612 : /* In C++17 lambdas can be constexpr, don't give up yet. */
12613 : return true;
12614 476 : else if (flags & tf_error)
12615 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
12616 : "constant expression before C++17");
12617 : return false;
12618 :
12619 114121 : case NEW_EXPR:
12620 114121 : case VEC_NEW_EXPR:
12621 114121 : case DELETE_EXPR:
12622 114121 : case VEC_DELETE_EXPR:
12623 114121 : if (cxx_dialect >= cxx20)
12624 : /* In C++20, new-expressions are potentially constant. */
12625 : return true;
12626 1077 : else if (flags & tf_error)
12627 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
12628 : "constant expression before C++20");
12629 : return false;
12630 :
12631 85281 : case DYNAMIC_CAST_EXPR:
12632 85281 : case PSEUDO_DTOR_EXPR:
12633 85281 : case OMP_PARALLEL:
12634 85281 : case OMP_TASK:
12635 85281 : case OMP_FOR:
12636 85281 : case OMP_SIMD:
12637 85281 : case OMP_DISTRIBUTE:
12638 85281 : case OMP_TASKLOOP:
12639 85281 : case OMP_LOOP:
12640 85281 : case OMP_TEAMS:
12641 85281 : case OMP_TARGET_DATA:
12642 85281 : case OMP_TARGET:
12643 85281 : case OMP_SECTIONS:
12644 85281 : case OMP_ORDERED:
12645 85281 : case OMP_CRITICAL:
12646 85281 : case OMP_SINGLE:
12647 85281 : case OMP_SCAN:
12648 85281 : case OMP_SCOPE:
12649 85281 : case OMP_SECTION:
12650 85281 : case OMP_MASTER:
12651 85281 : case OMP_MASKED:
12652 85281 : case OMP_TASKGROUP:
12653 85281 : case OMP_TARGET_UPDATE:
12654 85281 : case OMP_TARGET_ENTER_DATA:
12655 85281 : case OMP_TARGET_EXIT_DATA:
12656 85281 : case OMP_ATOMIC:
12657 85281 : case OMP_ATOMIC_READ:
12658 85281 : case OMP_ATOMIC_CAPTURE_OLD:
12659 85281 : case OMP_ATOMIC_CAPTURE_NEW:
12660 85281 : case OMP_DEPOBJ:
12661 85281 : case OACC_PARALLEL:
12662 85281 : case OACC_KERNELS:
12663 85281 : case OACC_SERIAL:
12664 85281 : case OACC_DATA:
12665 85281 : case OACC_HOST_DATA:
12666 85281 : case OACC_LOOP:
12667 85281 : case OACC_CACHE:
12668 85281 : case OACC_DECLARE:
12669 85281 : case OACC_ENTER_DATA:
12670 85281 : case OACC_EXIT_DATA:
12671 85281 : case OACC_UPDATE:
12672 85281 : case OMP_ARRAY_SECTION:
12673 : /* GCC internal stuff. */
12674 85281 : case VA_ARG_EXPR:
12675 85281 : case TRANSACTION_EXPR:
12676 85281 : case AT_ENCODE_EXPR:
12677 85281 : fail:
12678 85281 : if (flags & tf_error)
12679 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12680 : "expression", t);
12681 : return false;
12682 :
12683 : case OMP_DECLARE_MAPPER:
12684 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12685 : constant. */
12686 : return true;
12687 :
12688 26540 : case THROW_EXPR:
12689 26540 : if (cxx_dialect < cxx26)
12690 291 : goto fail;
12691 26249 : return RECUR (TREE_OPERAND (t, 0), rval);
12692 :
12693 531 : case ASM_EXPR:
12694 531 : if (flags & tf_error)
12695 6 : inline_asm_in_constexpr_error (loc, fundef_p);
12696 : return false;
12697 :
12698 543197 : case OBJ_TYPE_REF:
12699 543197 : if (cxx_dialect >= cxx20)
12700 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12701 : return true;
12702 9122 : else if (flags & tf_error)
12703 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12704 : "%<constexpr%> before C++20");
12705 : return false;
12706 :
12707 6558 : case TYPEID_EXPR:
12708 : /* In C++20, a typeid expression whose operand is of polymorphic
12709 : class type can be constexpr. */
12710 6558 : {
12711 6558 : tree e = TREE_OPERAND (t, 0);
12712 6558 : if (cxx_dialect < cxx20
12713 26 : && strict
12714 26 : && !TYPE_P (e)
12715 19 : && !type_dependent_expression_p (e)
12716 7 : && CLASS_TYPE_P (TREE_TYPE (e))
12717 6563 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12718 : {
12719 4 : if (flags & tf_error)
12720 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12721 : "constant expression because %qE is "
12722 : "of polymorphic type", e);
12723 : return false;
12724 : }
12725 : return true;
12726 : }
12727 :
12728 31431092 : case POINTER_DIFF_EXPR:
12729 31431092 : case MINUS_EXPR:
12730 31431092 : want_rval = true;
12731 31431092 : goto binary;
12732 :
12733 84537922 : case LT_EXPR:
12734 84537922 : case LE_EXPR:
12735 84537922 : case GT_EXPR:
12736 84537922 : case GE_EXPR:
12737 84537922 : case EQ_EXPR:
12738 84537922 : case NE_EXPR:
12739 84537922 : case SPACESHIP_EXPR:
12740 84537922 : want_rval = true;
12741 84537922 : goto binary;
12742 :
12743 2002967 : case PREINCREMENT_EXPR:
12744 2002967 : case POSTINCREMENT_EXPR:
12745 2002967 : case PREDECREMENT_EXPR:
12746 2002967 : case POSTDECREMENT_EXPR:
12747 2002967 : if (cxx_dialect < cxx14)
12748 9471 : goto fail;
12749 1993496 : goto unary;
12750 :
12751 1283393 : case BIT_NOT_EXPR:
12752 : /* A destructor. */
12753 1283393 : if (TYPE_P (TREE_OPERAND (t, 0)))
12754 : return true;
12755 : /* fall through. */
12756 :
12757 48454933 : case CONJ_EXPR:
12758 48454933 : case SAVE_EXPR:
12759 48454933 : case FIX_TRUNC_EXPR:
12760 48454933 : case FLOAT_EXPR:
12761 48454933 : case NEGATE_EXPR:
12762 48454933 : case ABS_EXPR:
12763 48454933 : case ABSU_EXPR:
12764 48454933 : case TRUTH_NOT_EXPR:
12765 48454933 : case FIXED_CONVERT_EXPR:
12766 48454933 : case UNARY_PLUS_EXPR:
12767 48454933 : case UNARY_LEFT_FOLD_EXPR:
12768 48454933 : case UNARY_RIGHT_FOLD_EXPR:
12769 48454933 : case VEC_DUPLICATE_EXPR:
12770 1283393 : unary:
12771 48454933 : return RECUR (TREE_OPERAND (t, 0), rval);
12772 :
12773 31710913 : case CAST_EXPR:
12774 31710913 : case CONST_CAST_EXPR:
12775 31710913 : case STATIC_CAST_EXPR:
12776 31710913 : case REINTERPRET_CAST_EXPR:
12777 31710913 : case IMPLICIT_CONV_EXPR:
12778 31710913 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12779 : /* In C++98, a conversion to non-integral type can't be part of a
12780 : constant expression. */
12781 : {
12782 346 : if (flags & tf_error)
12783 0 : constexpr_error (loc, fundef_p,
12784 : "cast to non-integral type %qT in a constant "
12785 0 : "expression", TREE_TYPE (t));
12786 : return false;
12787 : }
12788 : /* This might be a conversion from a class to a (potentially) literal
12789 : type. Let's consider it potentially constant since the conversion
12790 : might be a constexpr user-defined conversion. */
12791 31710567 : else if (cxx_dialect >= cxx11
12792 31688018 : && (dependent_type_p (TREE_TYPE (t))
12793 11157664 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12794 11142962 : || literal_type_p (TREE_TYPE (t)))
12795 31649816 : && TREE_OPERAND (t, 0)
12796 63114153 : && (TREE_CODE (t) != CAST_EXPR
12797 23804238 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12798 : {
12799 31360381 : tree from = TREE_OPERAND (t, 0);
12800 31360381 : if (TREE_CODE (t) == CAST_EXPR)
12801 23761033 : from = TREE_VALUE (from);
12802 31360381 : tree type = TREE_TYPE (from);
12803 : /* If this is a dependent type, it could end up being a class
12804 : with conversions. */
12805 31360381 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12806 : return true;
12807 : /* Or a non-dependent class which has conversions. */
12808 595365 : else if (CLASS_TYPE_P (type)
12809 595365 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12810 : return true;
12811 : }
12812 :
12813 25189876 : return (RECUR (TREE_OPERAND (t, 0),
12814 25189876 : !TYPE_REF_P (TREE_TYPE (t))));
12815 :
12816 14574055 : case BIND_EXPR:
12817 14574055 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12818 :
12819 71425801 : case CLEANUP_POINT_EXPR:
12820 71425801 : case MUST_NOT_THROW_EXPR:
12821 71425801 : case TRY_CATCH_EXPR:
12822 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12823 : body could throw, even with catch (...) among handlers we'd need
12824 : to analyze them in detail if they couldn't rethrow it. More
12825 : importantly though, throws (jump_target) is just conservative,
12826 : and there could be e.g.
12827 : try
12828 : {
12829 : possibly_throwing_fn (args);
12830 : break;
12831 : }
12832 : catch (...)
12833 : {
12834 : }
12835 : or continue or return instead of break. So, clearing *jump_target
12836 : because we see catch (...) handler might mean we missed break
12837 : etc. */
12838 71425801 : case TRY_BLOCK:
12839 71425801 : case EH_SPEC_BLOCK:
12840 71425801 : case EXPR_STMT:
12841 71425801 : case PAREN_EXPR:
12842 : /* For convenience. */
12843 71425801 : case LOOP_EXPR:
12844 71425801 : case EXIT_EXPR:
12845 71425801 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12846 :
12847 12988575 : case DECL_EXPR:
12848 12988575 : tmp = DECL_EXPR_DECL (t);
12849 8617407 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12850 19048817 : && (processing_template_decl
12851 5577340 : ? !decl_maybe_constant_var_p (tmp)
12852 5094438 : : !decl_constant_var_p (tmp)))
12853 : {
12854 5013264 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12855 : {
12856 8 : if (flags & tf_error)
12857 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12858 : "%qD defined %<thread_local%> in "
12859 : "%<constexpr%> context", tmp);
12860 : return false;
12861 : }
12862 5013256 : else if (TREE_STATIC (tmp))
12863 : {
12864 142 : if (flags & tf_error)
12865 34 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12866 : "%qD defined %<static%> in %<constexpr%> "
12867 : "context", tmp);
12868 : return false;
12869 : }
12870 5013114 : else if (!check_for_uninitialized_const_var
12871 5013114 : (tmp, /*constexpr_context_p=*/true, flags))
12872 : return false;
12873 : }
12874 12983480 : if (VAR_P (tmp))
12875 8612312 : return RECUR (DECL_INITIAL (tmp), want_rval);
12876 : return true;
12877 :
12878 43403 : case TRY_FINALLY_EXPR:
12879 43403 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12880 43403 : && RECUR (TREE_OPERAND (t, 1), any));
12881 :
12882 0 : case EH_ELSE_EXPR:
12883 : /* maybe_apply_function_contracts uses this to check postconditions only
12884 : on normal return. */
12885 0 : return (RECUR (TREE_OPERAND (t, 1), any)
12886 0 : || RECUR (TREE_OPERAND (t, 0), any));
12887 :
12888 24741172 : case SCOPE_REF:
12889 24741172 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12890 :
12891 49752122 : case TARGET_EXPR:
12892 49752122 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12893 49544150 : && !TARGET_EXPR_ELIDING_P (t)
12894 89584172 : && !literal_type_p (TREE_TYPE (t)))
12895 : {
12896 904659 : if (flags & tf_error)
12897 : {
12898 25 : auto_diagnostic_group d;
12899 25 : if (constexpr_error (loc, fundef_p,
12900 : "temporary of non-literal type %qT in a "
12901 25 : "constant expression", TREE_TYPE (t)))
12902 25 : explain_non_literal_class (TREE_TYPE (t));
12903 25 : }
12904 : return false;
12905 : }
12906 : /* FALLTHRU */
12907 77647652 : case INIT_EXPR:
12908 77647652 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12909 : return true;
12910 77526793 : return RECUR (TREE_OPERAND (t, 1), rval);
12911 :
12912 40022508 : case CONSTRUCTOR:
12913 40022508 : {
12914 40022508 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12915 40022508 : constructor_elt *ce;
12916 5229304876 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12917 234059438 : if (!RECUR (ce->value, want_rval))
12918 : return false;
12919 : return true;
12920 : }
12921 :
12922 21322788 : case TREE_LIST:
12923 21322788 : {
12924 21322788 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12925 : || DECL_P (TREE_PURPOSE (t)));
12926 21322788 : if (!RECUR (TREE_VALUE (t), want_rval))
12927 : return false;
12928 19107907 : if (TREE_CHAIN (t) == NULL_TREE)
12929 : return true;
12930 55329 : return RECUR (TREE_CHAIN (t), want_rval);
12931 : }
12932 :
12933 15504887 : case TRUNC_DIV_EXPR:
12934 15504887 : case CEIL_DIV_EXPR:
12935 15504887 : case FLOOR_DIV_EXPR:
12936 15504887 : case ROUND_DIV_EXPR:
12937 15504887 : case TRUNC_MOD_EXPR:
12938 15504887 : case CEIL_MOD_EXPR:
12939 15504887 : case ROUND_MOD_EXPR:
12940 15504887 : {
12941 15504887 : tree denom = TREE_OPERAND (t, 1);
12942 15504887 : if (!RECUR (denom, rval))
12943 : return false;
12944 : /* We can't call cxx_eval_outermost_constant_expr on an expression
12945 : that hasn't been through instantiate_non_dependent_expr yet. */
12946 14981731 : if (!processing_template_decl)
12947 7952743 : denom = cxx_eval_outermost_constant_expr (denom, true);
12948 14981731 : if (integer_zerop (denom))
12949 : {
12950 376 : if (flags & tf_error)
12951 38 : constexpr_error (input_location, fundef_p,
12952 : "division by zero is not a constant expression");
12953 : return false;
12954 : }
12955 : else
12956 : {
12957 14981355 : want_rval = true;
12958 14981355 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12959 : }
12960 : }
12961 :
12962 9737965 : case COMPOUND_EXPR:
12963 9737965 : {
12964 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12965 : COMPOUND_EXPR; don't get confused. */
12966 9737965 : tree op0 = TREE_OPERAND (t, 0);
12967 9737965 : tree op1 = TREE_OPERAND (t, 1);
12968 9737965 : STRIP_NOPS (op1);
12969 9737965 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12970 2731809 : return RECUR (op0, want_rval);
12971 : else
12972 7006156 : goto binary;
12973 : }
12974 :
12975 : /* If the first operand is the non-short-circuit constant, look at
12976 : the second operand; otherwise we only care about the first one for
12977 : potentiality. */
12978 18814061 : case TRUTH_AND_EXPR:
12979 18814061 : case TRUTH_ANDIF_EXPR:
12980 18814061 : tmp = boolean_true_node;
12981 18814061 : goto truth;
12982 7668838 : case TRUTH_OR_EXPR:
12983 7668838 : case TRUTH_ORIF_EXPR:
12984 7668838 : tmp = boolean_false_node;
12985 26482899 : truth:
12986 26482899 : {
12987 26482899 : tree op0 = TREE_OPERAND (t, 0);
12988 26482899 : tree op1 = TREE_OPERAND (t, 1);
12989 26482899 : if (!RECUR (op0, rval))
12990 : return false;
12991 18236362 : if (!(flags & tf_error) && RECUR (op1, rval))
12992 : /* When quiet, try to avoid expensive trial evaluation by first
12993 : checking potentiality of the second operand. */
12994 : return true;
12995 1848305 : if (!processing_template_decl)
12996 1444726 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12997 1848305 : if (tree_int_cst_equal (op0, tmp))
12998 5203 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12999 : else
13000 : return true;
13001 : }
13002 :
13003 : case PLUS_EXPR:
13004 : case MULT_EXPR:
13005 : case POINTER_PLUS_EXPR:
13006 : case RDIV_EXPR:
13007 : case EXACT_DIV_EXPR:
13008 : case MIN_EXPR:
13009 : case MAX_EXPR:
13010 : case LSHIFT_EXPR:
13011 : case RSHIFT_EXPR:
13012 : case LROTATE_EXPR:
13013 : case RROTATE_EXPR:
13014 : case BIT_IOR_EXPR:
13015 : case BIT_XOR_EXPR:
13016 : case BIT_AND_EXPR:
13017 : case TRUTH_XOR_EXPR:
13018 : case UNORDERED_EXPR:
13019 : case ORDERED_EXPR:
13020 : case UNLT_EXPR:
13021 : case UNLE_EXPR:
13022 : case UNGT_EXPR:
13023 : case UNGE_EXPR:
13024 : case UNEQ_EXPR:
13025 : case LTGT_EXPR:
13026 : case RANGE_EXPR:
13027 : case COMPLEX_EXPR:
13028 236977037 : want_rval = true;
13029 : /* Fall through. */
13030 249288471 : case ARRAY_REF:
13031 249288471 : case ARRAY_RANGE_REF:
13032 249288471 : case MEMBER_REF:
13033 249288471 : case DOTSTAR_EXPR:
13034 249288471 : case MEM_REF:
13035 249288471 : case BINARY_LEFT_FOLD_EXPR:
13036 249288471 : case BINARY_RIGHT_FOLD_EXPR:
13037 249288471 : binary:
13038 547538181 : for (i = 0; i < 2; ++i)
13039 405670256 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
13040 : return false;
13041 : return true;
13042 :
13043 : case VEC_PERM_EXPR:
13044 121529 : for (i = 0; i < 3; ++i)
13045 120583 : if (!RECUR (TREE_OPERAND (t, i), true))
13046 : return false;
13047 : return true;
13048 :
13049 7557331 : case COND_EXPR:
13050 7557331 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
13051 : {
13052 8 : if (flags & tf_error)
13053 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
13054 : "constant expression");
13055 : return false;
13056 : }
13057 : /* Fall through. */
13058 18380081 : case IF_STMT:
13059 18380081 : case VEC_COND_EXPR:
13060 : /* If the condition is a known constant, we know which of the legs we
13061 : care about; otherwise we only require that the condition and
13062 : either of the legs be potentially constant. */
13063 18380081 : tmp = TREE_OPERAND (t, 0);
13064 18380081 : if (!RECUR (tmp, rval))
13065 : return false;
13066 16063272 : if (!processing_template_decl)
13067 14057935 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
13068 : /* potential_constant_expression* isn't told if it is called for
13069 : manifestly_const_eval or not, so for consteval if always
13070 : process both branches as if the condition is not a known
13071 : constant. */
13072 16063272 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
13073 : {
13074 15986992 : if (integer_zerop (tmp))
13075 3513663 : return RECUR (TREE_OPERAND (t, 2), want_rval);
13076 12473329 : else if (TREE_CODE (tmp) == INTEGER_CST)
13077 3831238 : return RECUR (TREE_OPERAND (t, 1), want_rval);
13078 : }
13079 8718371 : tmp = *jump_target;
13080 10078013 : for (i = 1; i < 3; ++i)
13081 : {
13082 9928076 : tree this_jump_target = tmp;
13083 9928076 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
13084 : want_rval, strict, now, fundef_p,
13085 : tf_none, &this_jump_target))
13086 : {
13087 8865123 : if (returns (&this_jump_target) || throws (&this_jump_target))
13088 2473476 : *jump_target = this_jump_target;
13089 6094958 : else if (!returns (jump_target) && !throws (jump_target))
13090 : {
13091 6094958 : if (breaks (&this_jump_target)
13092 6094958 : || continues (&this_jump_target))
13093 45 : *jump_target = this_jump_target;
13094 6094958 : if (i == 1)
13095 : {
13096 : /* If the then branch is potentially constant, but
13097 : does not return, check if the else branch
13098 : couldn't return, break or continue. */
13099 5035863 : hash_set<tree> pset;
13100 5035863 : check_for_return_continue_data data = { &pset, NULL_TREE,
13101 : NULL_TREE,
13102 5035863 : false };
13103 10071726 : if (tree ret_expr
13104 5035863 : = cp_walk_tree (&TREE_OPERAND (t, 2),
13105 : check_for_return_continue, &data,
13106 : &pset))
13107 80548 : *jump_target = ret_expr;
13108 4955315 : else if (*jump_target == NULL_TREE)
13109 : {
13110 4955273 : if (data.continue_stmt)
13111 0 : *jump_target = data.continue_stmt;
13112 4955273 : else if (data.break_stmt)
13113 13 : *jump_target = data.break_stmt;
13114 : }
13115 5035863 : if (data.could_throw)
13116 54211 : *jump_target = void_node;
13117 5035863 : }
13118 : }
13119 8568434 : return true;
13120 : }
13121 : }
13122 149937 : if (flags & tf_error)
13123 : {
13124 3 : if (TREE_CODE (t) == IF_STMT)
13125 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
13126 : "constant expression");
13127 : else
13128 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
13129 : "constant expression", t);
13130 : }
13131 : return false;
13132 :
13133 1742 : case VEC_INIT_EXPR:
13134 1742 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
13135 : return true;
13136 868 : if (flags & tf_error)
13137 : {
13138 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
13139 : "initialization"))
13140 2 : diagnose_non_constexpr_vec_init (t);
13141 : }
13142 : return false;
13143 :
13144 : case TYPE_DECL:
13145 : case TAG_DEFN:
13146 : /* We can see these in statement-expressions. */
13147 : return true;
13148 :
13149 1496452 : case CLEANUP_STMT:
13150 1496452 : if (!RECUR (CLEANUP_BODY (t), any))
13151 : return false;
13152 2499374 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
13153 : return false;
13154 : return true;
13155 :
13156 : case EMPTY_CLASS_EXPR:
13157 : return true;
13158 :
13159 128 : case GOTO_EXPR:
13160 128 : {
13161 128 : tree *target = &TREE_OPERAND (t, 0);
13162 : /* Gotos representing break, continue and cdtor return are OK. */
13163 128 : if (breaks (target) || continues (target) || returns (target))
13164 : {
13165 108 : *jump_target = *target;
13166 108 : return true;
13167 : }
13168 20 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
13169 : /* The user didn't write this goto, this isn't the problem. */
13170 : return true;
13171 16 : if (flags & tf_error)
13172 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
13173 : "expression");
13174 : return false;
13175 : }
13176 :
13177 : case ASSERTION_STMT:
13178 : case PRECONDITION_STMT:
13179 : case POSTCONDITION_STMT:
13180 : /* Contracts are not supposed to alter this; we have to check that this
13181 : is not violated at a later time. */
13182 : return true;
13183 :
13184 228 : case LABEL_EXPR:
13185 228 : t = LABEL_EXPR_LABEL (t);
13186 228 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
13187 : return true;
13188 25 : else if (flags & tf_error)
13189 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
13190 : "function only available with %<-std=c++23%> or "
13191 : "%<-std=gnu++23%>");
13192 : return false;
13193 :
13194 10309 : case ANNOTATE_EXPR:
13195 10309 : return RECUR (TREE_OPERAND (t, 0), rval);
13196 :
13197 188529 : case BIT_CAST_EXPR:
13198 188529 : return RECUR (TREE_OPERAND (t, 0), rval);
13199 :
13200 : /* Coroutine await, yield and return expressions are not. */
13201 768 : case CO_AWAIT_EXPR:
13202 768 : case CO_YIELD_EXPR:
13203 768 : case CO_RETURN_EXPR:
13204 768 : case TEMPLATE_FOR_STMT:
13205 768 : if (flags & tf_error)
13206 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
13207 : "%qE is not a constant expression", t);
13208 : return false;
13209 :
13210 : /* Assume a TU-local entity is not constant, we'll error later when
13211 : instantiating. */
13212 : case TU_LOCAL_ENTITY:
13213 : return false;
13214 :
13215 : /* A splice expression is dependent, but will be constant after
13216 : substitution. */
13217 : case SPLICE_EXPR:
13218 : return true;
13219 :
13220 30 : case NONTYPE_ARGUMENT_PACK:
13221 30 : {
13222 30 : tree args = ARGUMENT_PACK_ARGS (t);
13223 30 : int len = TREE_VEC_LENGTH (args);
13224 90 : for (int i = 0; i < len; ++i)
13225 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
13226 : return false;
13227 : return true;
13228 : }
13229 :
13230 0 : default:
13231 0 : if (objc_non_constant_expr_p (t))
13232 : return false;
13233 :
13234 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13235 0 : gcc_unreachable ();
13236 : return false;
13237 : }
13238 : #undef RECUR
13239 4956514511 : }
13240 :
13241 : bool
13242 1775118602 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13243 : bool fundef_p, tsubst_flags_t flags)
13244 : {
13245 1775118602 : if (flags & tf_error)
13246 : {
13247 : /* Check potentiality quietly first, as that could be performed more
13248 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13249 : that fails, replay the check noisily to give errors. */
13250 11205022 : flags &= ~tf_error;
13251 11205022 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13252 : flags))
13253 : return true;
13254 1073 : flags |= tf_error;
13255 : }
13256 :
13257 1763914653 : tree target = NULL_TREE;
13258 1763914653 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13259 1763914653 : flags, &target);
13260 : }
13261 :
13262 : /* The main entry point to the above. */
13263 :
13264 : bool
13265 452029498 : potential_constant_expression (tree t)
13266 : {
13267 452029498 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13268 : /*now*/false, /*fundef_p*/false,
13269 452029498 : tf_none);
13270 : }
13271 :
13272 : /* As above, but require a constant rvalue. */
13273 :
13274 : bool
13275 35721986 : potential_rvalue_constant_expression (tree t)
13276 : {
13277 35721986 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13278 : /*now*/false, /*fundef_p*/false,
13279 35721986 : tf_none);
13280 : }
13281 :
13282 : /* Like above, but complain about non-constant expressions. */
13283 :
13284 : bool
13285 88 : require_potential_constant_expression (tree t)
13286 : {
13287 88 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13288 : /*now*/false, /*fundef_p*/false,
13289 88 : tf_warning_or_error);
13290 : }
13291 :
13292 : /* Cross product of the above. */
13293 :
13294 : bool
13295 132997 : require_potential_rvalue_constant_expression (tree t)
13296 : {
13297 132997 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13298 : /*now*/false, /*fundef_p*/false,
13299 132997 : tf_warning_or_error);
13300 : }
13301 :
13302 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
13303 :
13304 : bool
13305 290 : require_potential_rvalue_constant_expression_fncheck (tree t)
13306 : {
13307 290 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13308 : /*now*/false, /*fundef_p*/true,
13309 290 : tf_warning_or_error);
13310 : }
13311 :
13312 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
13313 :
13314 : bool
13315 1143 : require_rvalue_constant_expression (tree t)
13316 : {
13317 1143 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13318 : /*now*/true, /*fundef_p*/false,
13319 1143 : tf_warning_or_error);
13320 : }
13321 :
13322 : /* Like potential_constant_expression, but don't consider possible constexpr
13323 : substitution of the current function. That is, PARM_DECL qualifies under
13324 : potential_constant_expression, but not here.
13325 :
13326 : This is basically what you can check when any actual constant values might
13327 : be value-dependent. */
13328 :
13329 : bool
13330 932379492 : is_constant_expression (tree t)
13331 : {
13332 932379492 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13333 : /*now*/true, /*fundef_p*/false,
13334 932379492 : tf_none);
13335 : }
13336 :
13337 : /* As above, but expect an rvalue. */
13338 :
13339 : bool
13340 157008716 : is_rvalue_constant_expression (tree t)
13341 : {
13342 157008716 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13343 : /*now*/true, /*fundef_p*/false,
13344 157008716 : tf_none);
13345 : }
13346 :
13347 : /* Like above, but complain about non-constant expressions. */
13348 :
13349 : bool
13350 11070504 : require_constant_expression (tree t)
13351 : {
13352 11070504 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13353 : /*now*/true, /*fundef_p*/false,
13354 11070504 : tf_warning_or_error);
13355 : }
13356 :
13357 : /* Like is_constant_expression, but allow const variables that are not allowed
13358 : under constexpr rules. */
13359 :
13360 : bool
13361 175568866 : is_static_init_expression (tree t)
13362 : {
13363 175568866 : return potential_constant_expression_1 (t, /*want_rval*/false,
13364 : /*strict*/false, /*now*/true,
13365 175568866 : /*fundef_p*/false, tf_none);
13366 : }
13367 :
13368 : /* Returns true if T is a potential constant expression that is not
13369 : instantiation-dependent, and therefore a candidate for constant folding even
13370 : in a template. */
13371 :
13372 : bool
13373 900434459 : is_nondependent_constant_expression (tree t)
13374 : {
13375 900434459 : return (!type_unknown_p (t)
13376 900434409 : && is_constant_expression (t)
13377 1689507025 : && !instantiation_dependent_expression_p (t));
13378 : }
13379 :
13380 : /* Returns true if T is a potential static initializer expression that is not
13381 : instantiation-dependent. */
13382 :
13383 : bool
13384 175568866 : is_nondependent_static_init_expression (tree t)
13385 : {
13386 175568866 : return (!type_unknown_p (t)
13387 175568866 : && is_static_init_expression (t)
13388 317433602 : && !instantiation_dependent_expression_p (t));
13389 : }
13390 :
13391 : /* True iff FN is an implicitly constexpr function. */
13392 :
13393 : bool
13394 465579 : decl_implicit_constexpr_p (tree fn)
13395 : {
13396 465579 : if (!(flag_implicit_constexpr
13397 3 : && TREE_CODE (fn) == FUNCTION_DECL
13398 3 : && DECL_DECLARED_CONSTEXPR_P (fn)))
13399 : return false;
13400 :
13401 3 : if (DECL_CLONED_FUNCTION_P (fn))
13402 0 : fn = DECL_CLONED_FUNCTION (fn);
13403 :
13404 3 : return (DECL_LANG_SPECIFIC (fn)
13405 3 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
13406 : }
13407 :
13408 : /* Finalize constexpr processing after parsing. */
13409 :
13410 : void
13411 99007 : fini_constexpr (void)
13412 : {
13413 : /* The contexpr call and fundef copies tables are no longer needed. */
13414 99007 : constexpr_call_table = NULL;
13415 99007 : fundef_copies_table = NULL;
13416 99007 : }
13417 :
13418 : #include "gt-cp-constexpr.h"
|