Branch data 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-2025 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 : 73682760 : is_instantiation_of_constexpr (tree fun)
62 : : {
63 : 98341081 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 : 49503900 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 : 107412543 : || (DECL_DEFAULTED_FN (fun)
66 : 1613936 : && DECL_DECLARED_CONSTEXPR_P (fun)));
67 : : }
68 : :
69 : : /* Return true if T is a literal type. */
70 : :
71 : : bool
72 : 106646242 : literal_type_p (tree t)
73 : : {
74 : 104089902 : if (SCALAR_TYPE_P (t)
75 : 43328781 : || VECTOR_TYPE_P (t)
76 : 43316645 : || TYPE_REF_P (t)
77 : 142198131 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : : return true;
79 : 32768833 : if (CLASS_TYPE_P (t))
80 : : {
81 : 31500472 : t = complete_type (t);
82 : 31500472 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 : 31500472 : return CLASSTYPE_LITERAL_P (t);
84 : : }
85 : 1268361 : if (TREE_CODE (t) == ARRAY_TYPE)
86 : 1268183 : 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 : 283325444 : ensure_literal_type_for_constexpr_object (tree decl)
96 : : {
97 : 283325444 : tree type = TREE_TYPE (decl);
98 : 283325444 : if (VAR_P (decl)
99 : 100237090 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 : 77923258 : || var_in_constexpr_fn (decl))
101 : 313534854 : && !processing_template_decl)
102 : : {
103 : 20160614 : tree stype = strip_array_types (type);
104 : 20160614 : 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 : 20160608 : else if (!literal_type_p (type))
108 : : {
109 : 10076 : if (DECL_DECLARED_CONSTEXPR_P (decl))
110 : : {
111 : 49 : auto_diagnostic_group d;
112 : 49 : error_at (DECL_SOURCE_LOCATION (decl),
113 : : "the type %qT of %<constexpr%> variable %qD "
114 : : "is not literal", type, decl);
115 : 49 : explain_non_literal_class (type);
116 : 49 : decl = error_mark_node;
117 : 49 : }
118 : 10027 : else if (cxx_dialect < cxx23)
119 : : {
120 : 2413 : 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 : 2413 : cp_function_chain->invalid_constexpr = true;
131 : : }
132 : : }
133 : 20150532 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 : 20150532 : && 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 : 283325444 : 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 : 902 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : : const char *gmsgid, ...)
156 : : {
157 : 902 : diagnostics::diagnostic_info diagnostic;
158 : 902 : va_list ap;
159 : 902 : rich_location richloc (line_table, location);
160 : 902 : va_start (ap, gmsgid);
161 : 902 : bool ret;
162 : 902 : if (!constexpr_fundef_p)
163 : : {
164 : : /* Report an error that cannot be suppressed. */
165 : 678 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
166 : : diagnostics::kind::error);
167 : 678 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
168 : : }
169 : 224 : else if (warn_invalid_constexpr)
170 : : {
171 : 184 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
172 : 184 : (cxx_dialect < cxx23
173 : : ? diagnostics::kind::pedwarn
174 : : : diagnostics::kind::warning));
175 : 184 : diagnostic.m_option_id = OPT_Winvalid_constexpr;
176 : 184 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
177 : : }
178 : : else
179 : : ret = false;
180 : 902 : va_end (ap);
181 : 1804 : return ret;
182 : 902 : }
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 : 381143169 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : : const constexpr_fundef *rhs)
202 : : {
203 : 364527621 : 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 : 373783609 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : : {
212 : 373783609 : return DECL_UID (fundef->decl);
213 : : }
214 : :
215 : : /* Return a previously saved definition of function FUN. */
216 : :
217 : : constexpr_fundef *
218 : 43835538 : retrieve_constexpr_fundef (tree fun)
219 : : {
220 : 43835538 : if (constexpr_fundef_table == NULL)
221 : : return NULL;
222 : :
223 : 43831895 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 : 43831895 : 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 : 19172310 : is_valid_constexpr_fn (tree fun, bool complain)
232 : : {
233 : 19172310 : bool ret = true;
234 : :
235 : 38344620 : if (DECL_INHERITED_CTOR (fun)
236 : 2119251 : && 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 : 19172310 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 : 37392180 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 : 18219870 : if (!literal_type_p (TREE_TYPE (parm)))
248 : : {
249 : 9218 : ret = false;
250 : 9218 : if (complain)
251 : : {
252 : 22 : auto_diagnostic_group d;
253 : 22 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
254 : : "invalid type for parameter %d of "
255 : : "%<constexpr%> function %q+#D",
256 : 22 : DECL_PARM_INDEX (parm), fun))
257 : 20 : explain_non_literal_class (TREE_TYPE (parm));
258 : 22 : }
259 : : }
260 : : }
261 : :
262 : 30802011 : 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 : 38344614 : else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20)
270 : : {
271 : 96 : ret = false;
272 : 96 : 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 : 19172211 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : : {
279 : 16813213 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 : 16813213 : if (!literal_type_p (rettype))
281 : : {
282 : 51299 : ret = false;
283 : 51299 : if (complain)
284 : : {
285 : 12 : auto_diagnostic_group d;
286 : 12 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
287 : : "invalid return type %qT of %<constexpr%> "
288 : : "function %q+D", rettype, fun))
289 : 12 : explain_non_literal_class (rettype);
290 : 12 : }
291 : : }
292 : :
293 : : /* C++14 DR 1684 removed this restriction. */
294 : 16813213 : if (cxx_dialect < cxx14
295 : 31486 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 : 16814303 : && !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 : 2358998 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)) && cxx_dialect < cxx26)
311 : : {
312 : 50 : ret = false;
313 : 50 : 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 : 19172310 : 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 : 892 : 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 : 892 : auto_vec<tree, 2> fields;
344 : 1880 : do
345 : : {
346 : 1880 : fields.safe_push (TREE_OPERAND (member, 1));
347 : 1880 : member = TREE_OPERAND (member, 0);
348 : : }
349 : 1880 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 : 3766 : && 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 : 1880 : while (field = fields.pop(),
357 : 1880 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : : {
359 : 988 : tree ctor;
360 : : /* If there is already an outer constructor entry for the anonymous
361 : : aggregate FIELD, use it; otherwise, insert one. */
362 : 988 : if (vec_safe_is_empty (*vec)
363 : 198 : || (*vec)->last().index != field)
364 : : {
365 : 886 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 : 886 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : : }
368 : : else
369 : 102 : ctor = (*vec)->last().value;
370 : 988 : 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 : 892 : gcc_assert (fields.is_empty());
376 : 892 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 : :
378 : 892 : return true;
379 : 892 : }
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 : 3152195 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : : {
390 : 3424412 : tree member, init;
391 : 3424412 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 : 1466446 : t = TREE_OPERAND (t, 0);
393 : 3424412 : if (TREE_CODE (t) == EXPR_STMT)
394 : 1465671 : t = TREE_OPERAND (t, 0);
395 : 3424412 : if (t == error_mark_node)
396 : : return false;
397 : 3424405 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : : {
399 : 3663737 : for (tree stmt : tsi_range (t))
400 : 242629 : if (! build_data_member_initialization (stmt, vec))
401 : 7 : return false;
402 : : return true;
403 : : }
404 : 3155485 : 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 : 254175 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : : }
413 : 2901310 : if (TREE_CODE (t) == CONVERT_EXPR)
414 : 1758286 : t = TREE_OPERAND (t, 0);
415 : 2901310 : if (TREE_CODE (t) == INIT_EXPR
416 : : /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
417 : : use what this function builds for cx_check_missing_mem_inits, and
418 : : assignment in the ctor body doesn't count. */
419 : 1230835 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
420 : : {
421 : 1670957 : member = TREE_OPERAND (t, 0);
422 : 1670957 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
423 : : }
424 : 1230353 : else if (TREE_CODE (t) == CALL_EXPR)
425 : : {
426 : 1046280 : tree fn = get_callee_fndecl (t);
427 : 2092560 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
428 : : /* We're only interested in calls to subobject constructors. */
429 : : return true;
430 : 923913 : member = CALL_EXPR_ARG (t, 0);
431 : : /* We don't use build_cplus_new here because it complains about
432 : : abstract bases. Leaving the call unwrapped means that it has the
433 : : wrong type, but cxx_eval_constant_expression doesn't care. */
434 : 923913 : init = break_out_target_exprs (t);
435 : : }
436 : 184073 : else if (TREE_CODE (t) == BIND_EXPR)
437 : 18042 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
438 : : else
439 : : /* Don't add anything else to the CONSTRUCTOR. */
440 : : return true;
441 : 2594870 : if (INDIRECT_REF_P (member))
442 : 30617 : member = TREE_OPERAND (member, 0);
443 : 2594870 : if (TREE_CODE (member) == NOP_EXPR)
444 : : {
445 : 220805 : tree op = member;
446 : 220805 : STRIP_NOPS (op);
447 : 220805 : if (TREE_CODE (op) == ADDR_EXPR)
448 : : {
449 : 299 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
450 : : (TREE_TYPE (TREE_TYPE (op)),
451 : : TREE_TYPE (TREE_TYPE (member))));
452 : : /* Initializing a cv-qualified member; we need to look through
453 : : the const_cast. */
454 : : member = op;
455 : : }
456 : 220506 : else if (op == current_class_ptr
457 : 440900 : && (same_type_ignoring_top_level_qualifiers_p
458 : 220394 : (TREE_TYPE (TREE_TYPE (member)),
459 : : current_class_type)))
460 : : /* Delegating constructor. */
461 : : member = op;
462 : : else
463 : : {
464 : : /* This is an initializer for an empty base; keep it for now so
465 : : we can check it in cxx_eval_bare_aggregate. */
466 : 200328 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
467 : : }
468 : : }
469 : 2594870 : if (TREE_CODE (member) == ADDR_EXPR)
470 : 734024 : member = TREE_OPERAND (member, 0);
471 : 2594870 : if (TREE_CODE (member) == COMPONENT_REF)
472 : : {
473 : 2362553 : tree aggr = TREE_OPERAND (member, 0);
474 : 2362553 : if (TREE_CODE (aggr) == VAR_DECL)
475 : : /* Initializing a local variable, don't add anything. */
476 : : return true;
477 : 2362551 : if (TREE_CODE (aggr) != COMPONENT_REF)
478 : : /* Normal member initialization. */
479 : 2361389 : member = TREE_OPERAND (member, 1);
480 : 1162 : else if (VAR_P (get_base_address (aggr)))
481 : : /* Initializing a local variable, don't add anything. */
482 : : return true;
483 : 1159 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
484 : : /* Initializing a member of an anonymous union. */
485 : 892 : return build_anon_member_initialization (member, init, vec);
486 : : else
487 : : /* We're initializing a vtable pointer in a base. Leave it as
488 : : COMPONENT_REF so we remember the path to get to the vfield. */
489 : 267 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
490 : : }
491 : :
492 : : /* Value-initialization can produce multiple initializers for the
493 : : same field; use the last one. */
494 : 3174380 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
495 : 32 : (*vec)->last().value = init;
496 : : else
497 : 2593941 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
498 : : return true;
499 : : }
500 : :
501 : : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
502 : : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
503 : : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
504 : :
505 : : static bool
506 : 2363 : check_constexpr_bind_expr_vars (tree t)
507 : : {
508 : 2363 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
509 : :
510 : 3185 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
511 : 834 : if (TREE_CODE (var) == TYPE_DECL
512 : 815 : && DECL_IMPLICIT_TYPEDEF_P (var)
513 : 856 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
514 : : return false;
515 : : return true;
516 : : }
517 : :
518 : : /* Subroutine of check_constexpr_ctor_body. */
519 : :
520 : : static bool
521 : 4069 : check_constexpr_ctor_body_1 (tree last, tree list)
522 : : {
523 : 4069 : switch (TREE_CODE (list))
524 : : {
525 : 6 : case DECL_EXPR:
526 : 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
527 : 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
528 : : return true;
529 : : return false;
530 : :
531 : 3 : case CLEANUP_POINT_EXPR:
532 : 3 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
533 : 3 : /*complain=*/false);
534 : :
535 : 2026 : case BIND_EXPR:
536 : 2026 : if (!check_constexpr_bind_expr_vars (list)
537 : 2026 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
538 : : /*complain=*/false))
539 : 11 : return false;
540 : : return true;
541 : :
542 : : case USING_STMT:
543 : : case STATIC_ASSERT:
544 : : case DEBUG_BEGIN_STMT:
545 : : return true;
546 : :
547 : : default:
548 : : return false;
549 : : }
550 : : }
551 : :
552 : : /* Make sure that there are no statements after LAST in the constructor
553 : : body represented by LIST. */
554 : :
555 : : bool
556 : 3536101 : check_constexpr_ctor_body (tree last, tree list, bool complain)
557 : : {
558 : : /* C++14 doesn't require a constexpr ctor to have an empty body. */
559 : 3536101 : if (cxx_dialect >= cxx14)
560 : : return true;
561 : :
562 : 13582 : bool ok = true;
563 : 13582 : if (TREE_CODE (list) == STATEMENT_LIST)
564 : : {
565 : 13569 : tree_stmt_iterator i = tsi_last (list);
566 : 17607 : for (; !tsi_end_p (i); tsi_prev (&i))
567 : : {
568 : 15484 : tree t = tsi_stmt (i);
569 : 15484 : if (t == last)
570 : : break;
571 : 4056 : if (!check_constexpr_ctor_body_1 (last, t))
572 : : {
573 : : ok = false;
574 : : break;
575 : : }
576 : : }
577 : : }
578 : 13 : else if (list != last
579 : 13 : && !check_constexpr_ctor_body_1 (last, list))
580 : : ok = false;
581 : 13569 : if (!ok)
582 : : {
583 : 22 : if (complain)
584 : 16 : error ("%<constexpr%> constructor does not have empty body");
585 : 22 : DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
586 : : }
587 : : return ok;
588 : : }
589 : :
590 : : /* V is a vector of constructor elements built up for the base and member
591 : : initializers of a constructor for TYPE. They need to be in increasing
592 : : offset order, which they might not be yet if TYPE has a primary base
593 : : which is not first in the base-clause or a vptr and at least one base
594 : : all of which are non-primary. */
595 : :
596 : : static vec<constructor_elt, va_gc> *
597 : 2098263 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
598 : : {
599 : 2098263 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
600 : 2098263 : tree field_type;
601 : 2098263 : unsigned i;
602 : 2098263 : constructor_elt *ce;
603 : :
604 : 2098263 : if (pri)
605 : 32587 : field_type = BINFO_TYPE (pri);
606 : 2065676 : else if (TYPE_CONTAINS_VPTR_P (type))
607 : 26776 : field_type = vtbl_ptr_type_node;
608 : : else
609 : : return v;
610 : :
611 : : /* Find the element for the primary base or vptr and move it to the
612 : : beginning of the vec. */
613 : 76985 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
614 : 50304 : if (TREE_TYPE (ce->index) == field_type)
615 : : break;
616 : :
617 : 71655 : if (i > 0 && i < vec_safe_length (v))
618 : : {
619 : 23 : vec<constructor_elt, va_gc> &vref = *v;
620 : 23 : constructor_elt elt = vref[i];
621 : 46 : for (; i > 0; --i)
622 : 23 : vref[i] = vref[i-1];
623 : 23 : vref[0] = elt;
624 : : }
625 : :
626 : : return v;
627 : : }
628 : :
629 : : /* Build compile-time evalable representations of member-initializer list
630 : : for a constexpr constructor. */
631 : :
632 : : static tree
633 : 2118444 : build_constexpr_constructor_member_initializers (tree type, tree body)
634 : : {
635 : 2118444 : vec<constructor_elt, va_gc> *vec = NULL;
636 : 2118444 : bool ok = true;
637 : 3174679 : while (true)
638 : 3174679 : switch (TREE_CODE (body))
639 : : {
640 : 1056189 : case MUST_NOT_THROW_EXPR:
641 : 1056189 : case EH_SPEC_BLOCK:
642 : 1056189 : body = TREE_OPERAND (body, 0);
643 : 1056189 : break;
644 : :
645 : 46 : case STATEMENT_LIST:
646 : 3174744 : for (tree stmt : tsi_range (body))
647 : : {
648 : 95 : body = stmt;
649 : 95 : if (TREE_CODE (body) == BIND_EXPR)
650 : : break;
651 : : }
652 : : break;
653 : :
654 : 2118444 : case BIND_EXPR:
655 : 2118444 : body = BIND_EXPR_BODY (body);
656 : 2118444 : goto found;
657 : :
658 : 0 : default:
659 : 0 : gcc_unreachable ();
660 : : }
661 : 2118444 : found:
662 : 2118444 : if (TREE_CODE (body) == TRY_BLOCK)
663 : : {
664 : 26 : body = TREE_OPERAND (body, 0);
665 : 26 : if (TREE_CODE (body) == BIND_EXPR)
666 : 26 : body = BIND_EXPR_BODY (body);
667 : : }
668 : 2118444 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
669 : : {
670 : 1323713 : body = TREE_OPERAND (body, 0);
671 : 1323713 : if (TREE_CODE (body) == EXPR_STMT)
672 : 1323713 : body = TREE_OPERAND (body, 0);
673 : 1323713 : if (TREE_CODE (body) == INIT_EXPR
674 : 1323713 : && (same_type_ignoring_top_level_qualifiers_p
675 : 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
676 : : current_class_type)))
677 : : {
678 : : /* Trivial copy. */
679 : 0 : return TREE_OPERAND (body, 1);
680 : : }
681 : 1323713 : ok = build_data_member_initialization (body, &vec);
682 : : }
683 : 794731 : else if (TREE_CODE (body) == STATEMENT_LIST)
684 : : {
685 : 2379786 : for (tree stmt : tsi_range (body))
686 : : {
687 : 1585454 : ok = build_data_member_initialization (stmt, &vec);
688 : 1585454 : if (!ok)
689 : : break;
690 : : }
691 : : }
692 : 399 : else if (EXPR_P (body))
693 : 399 : ok = build_data_member_initialization (body, &vec);
694 : : else
695 : 0 : gcc_assert (errorcount > 0);
696 : 2118444 : if (ok)
697 : : {
698 : 2118437 : if (vec_safe_length (vec) > 0)
699 : : {
700 : : /* In a delegating constructor, return the target. */
701 : 2014332 : constructor_elt *ce = &(*vec)[0];
702 : 2014332 : if (ce->index == current_class_ptr)
703 : : {
704 : 20174 : body = ce->value;
705 : 20174 : vec_free (vec);
706 : 20174 : return body;
707 : : }
708 : : }
709 : 2098263 : vec = sort_constexpr_mem_initializers (type, vec);
710 : 2098263 : return build_constructor (type, vec);
711 : : }
712 : : else
713 : 7 : return error_mark_node;
714 : : }
715 : :
716 : : /* We have an expression tree T that represents a call, either CALL_EXPR
717 : : or AGGR_INIT_EXPR. If the call is lexically to a named function,
718 : : return the _DECL for that function. */
719 : :
720 : : static tree
721 : 319974495 : get_function_named_in_call (tree t)
722 : : {
723 : 319974495 : tree callee = cp_get_callee (t);
724 : 319974495 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
725 : 319974495 : return fun ? fun : callee;
726 : : }
727 : :
728 : : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
729 : : declared to be constexpr, or a sub-statement thereof. Returns the
730 : : return value if suitable, error_mark_node for a statement not allowed in
731 : : a constexpr function, or NULL_TREE if no return value was found. */
732 : :
733 : : tree
734 : 68577 : constexpr_fn_retval (tree body)
735 : : {
736 : 75103 : switch (TREE_CODE (body))
737 : : {
738 : 18510 : case STATEMENT_LIST:
739 : 18510 : {
740 : 18510 : tree expr = NULL_TREE;
741 : 55462 : for (tree stmt : tsi_range (body))
742 : : {
743 : 36979 : tree s = constexpr_fn_retval (stmt);
744 : 36979 : if (s == error_mark_node)
745 : : return error_mark_node;
746 : 36952 : else if (s == NULL_TREE)
747 : : /* Keep iterating. */;
748 : 18473 : else if (expr)
749 : : /* Multiple return statements. */
750 : : return error_mark_node;
751 : : else
752 : : expr = s;
753 : : }
754 : : return expr;
755 : : }
756 : :
757 : 31546 : case RETURN_EXPR:
758 : 31546 : return break_out_target_exprs (TREE_OPERAND (body, 0));
759 : :
760 : 20 : case DECL_EXPR:
761 : 20 : {
762 : 20 : tree decl = DECL_EXPR_DECL (body);
763 : 20 : if (TREE_CODE (decl) == USING_DECL
764 : : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
765 : 20 : || DECL_ARTIFICIAL (decl))
766 : : return NULL_TREE;
767 : 6 : return error_mark_node;
768 : : }
769 : :
770 : 6195 : case CLEANUP_POINT_EXPR:
771 : 6195 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
772 : :
773 : 337 : case BIND_EXPR:
774 : 337 : if (!check_constexpr_bind_expr_vars (body))
775 : 6 : return error_mark_node;
776 : 331 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
777 : :
778 : : case USING_STMT:
779 : : case DEBUG_BEGIN_STMT:
780 : : return NULL_TREE;
781 : :
782 : 0 : case CALL_EXPR:
783 : 0 : {
784 : 0 : tree fun = get_function_named_in_call (body);
785 : 0 : if (fun != NULL_TREE
786 : 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
787 : : return NULL_TREE;
788 : : }
789 : : /* Fallthru. */
790 : :
791 : 30 : default:
792 : 30 : return error_mark_node;
793 : : }
794 : : }
795 : :
796 : : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
797 : : FUN; do the necessary transformations to turn it into a single expression
798 : : that we can store in the hash table. */
799 : :
800 : : static tree
801 : 18566728 : massage_constexpr_body (tree fun, tree body)
802 : : {
803 : 37133456 : if (DECL_CONSTRUCTOR_P (fun))
804 : 2118444 : body = build_constexpr_constructor_member_initializers
805 : 2118444 : (DECL_CONTEXT (fun), body);
806 : 16448284 : else if (cxx_dialect < cxx14)
807 : : {
808 : 31363 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
809 : 1 : body = EH_SPEC_STMTS (body);
810 : 31363 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
811 : 20267 : body = TREE_OPERAND (body, 0);
812 : 31363 : body = constexpr_fn_retval (body);
813 : : }
814 : 18566728 : return body;
815 : : }
816 : :
817 : : /* CTYPE is a type constructed from BODY. Return true if some
818 : : bases/fields are uninitialized, and complain if COMPLAIN. */
819 : :
820 : : static bool
821 : 1771073 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
822 : : {
823 : : /* We allow uninitialized bases/fields in C++20. */
824 : 1771073 : if (cxx_dialect >= cxx20)
825 : : return false;
826 : :
827 : 798005 : unsigned nelts = 0;
828 : :
829 : 798005 : if (body)
830 : : {
831 : 798001 : if (TREE_CODE (body) != CONSTRUCTOR)
832 : : return false;
833 : 797745 : nelts = CONSTRUCTOR_NELTS (body);
834 : : }
835 : 797749 : tree field = TYPE_FIELDS (ctype);
836 : :
837 : 797749 : if (TREE_CODE (ctype) == UNION_TYPE)
838 : : {
839 : 8235 : if (nelts == 0 && next_aggregate_field (field))
840 : : {
841 : 2 : if (complain)
842 : 2 : error ("%<constexpr%> constructor for union %qT must "
843 : : "initialize exactly one non-static data member", ctype);
844 : 2 : return true;
845 : : }
846 : 8233 : return false;
847 : : }
848 : :
849 : : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
850 : : need an explicit initialization. */
851 : : bool bad = false;
852 : 1704640 : for (unsigned i = 0; i <= nelts; ++i)
853 : : {
854 : 1704640 : tree index = NULL_TREE;
855 : 1704640 : if (i < nelts)
856 : : {
857 : 915126 : index = CONSTRUCTOR_ELT (body, i)->index;
858 : : /* Skip vptr adjustment represented with a COMPONENT_REF. */
859 : 915126 : if (TREE_CODE (index) != FIELD_DECL)
860 : 67880 : continue;
861 : : }
862 : :
863 : 39704644 : for (; field != index; field = DECL_CHAIN (field))
864 : : {
865 : 38067953 : tree ftype;
866 : 38067953 : if (TREE_CODE (field) != FIELD_DECL)
867 : 76000974 : continue;
868 : 134842 : if (DECL_UNNAMED_BIT_FIELD (field))
869 : 16 : continue;
870 : : /* Artificial fields can be ignored unless they're bases. */
871 : 134826 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
872 : 3 : continue;
873 : 134823 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
874 : : {
875 : : /* Recurse to check the anonymous aggregate member. */
876 : 8 : bad |= cx_check_missing_mem_inits
877 : 4 : (TREE_TYPE (field), NULL_TREE, complain);
878 : 4 : if (bad && !complain)
879 : 69 : return true;
880 : 4 : continue;
881 : : }
882 : 134819 : ftype = TREE_TYPE (field);
883 : 134819 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
884 : : /* A flexible array can't be intialized here, so don't complain
885 : : that it isn't. */
886 : 2 : continue;
887 : 134817 : if (is_empty_field (field))
888 : : /* An empty field doesn't need an initializer. */
889 : 134716 : continue;
890 : 101 : ftype = strip_array_types (ftype);
891 : 101 : if (type_has_constexpr_default_constructor (ftype))
892 : : {
893 : : /* It's OK to skip a member with a trivial constexpr ctor.
894 : : A constexpr ctor that isn't trivial should have been
895 : : added in by now. */
896 : 11 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
897 : : || errorcount != 0);
898 : 11 : continue;
899 : : }
900 : 90 : if (!complain)
901 : : return true;
902 : 21 : auto_diagnostic_group d;
903 : 21 : error ("member %qD must be initialized by mem-initializer "
904 : : "in %<constexpr%> constructor", field);
905 : 21 : inform (DECL_SOURCE_LOCATION (field), "declared here");
906 : 21 : bad = true;
907 : 21 : }
908 : 1636691 : if (field == NULL_TREE)
909 : : break;
910 : :
911 : 847246 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
912 : : {
913 : : /* Check the anonymous aggregate initializer is valid. */
914 : 448 : bad |= cx_check_missing_mem_inits
915 : 224 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
916 : 224 : if (bad && !complain)
917 : : return true;
918 : : }
919 : 847246 : field = DECL_CHAIN (field);
920 : : }
921 : :
922 : : return bad;
923 : : }
924 : :
925 : : /* We are processing the definition of the constexpr function FUN.
926 : : Check that its body fulfills the apropriate requirements and
927 : : enter it in the constexpr function definition table. */
928 : :
929 : : void
930 : 143882151 : maybe_save_constexpr_fundef (tree fun)
931 : : {
932 : 143882151 : if (processing_template_decl
933 : 57291903 : || cp_function_chain->invalid_constexpr
934 : 201061633 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
935 : 125315693 : return;
936 : :
937 : : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
938 : : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
939 : 43441086 : bool implicit = false;
940 : 43441086 : if (flag_implicit_constexpr)
941 : : {
942 : 13 : if (DECL_DELETING_DESTRUCTOR_P (fun)
943 : 13 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
944 : : /* Don't inherit implicit constexpr from the non-deleting
945 : : destructor. */
946 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
947 : :
948 : 13 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
949 : 12 : && DECL_DECLARED_INLINE_P (fun)
950 : 21 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
951 : : implicit = true;
952 : : }
953 : :
954 : 43441086 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
955 : : return;
956 : :
957 : 18621923 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
958 : :
959 : 18621923 : if (!is_valid_constexpr_fn (fun, complain))
960 : : return;
961 : :
962 : 18566676 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
963 : 18566676 : if (massaged == NULL_TREE || massaged == error_mark_node)
964 : : {
965 : 66 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
966 : 22 : error ("body of %<constexpr%> function %qD not a return-statement",
967 : : fun);
968 : 33 : return;
969 : : }
970 : :
971 : 18566643 : bool potential = potential_rvalue_constant_expression (massaged);
972 : 18566643 : if (!potential && complain)
973 : 183 : require_potential_rvalue_constant_expression_fncheck (massaged);
974 : :
975 : 20685069 : if (DECL_CONSTRUCTOR_P (fun) && potential
976 : 20681552 : && !DECL_DEFAULTED_FN (fun))
977 : : {
978 : 1770834 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
979 : : massaged, complain))
980 : : potential = false;
981 : 1770746 : else if (cxx_dialect > cxx11)
982 : : {
983 : : /* What we got from massage_constexpr_body is pretty much just the
984 : : ctor-initializer, also check the body. */
985 : 1766048 : massaged = DECL_SAVED_TREE (fun);
986 : 1766048 : potential = potential_rvalue_constant_expression (massaged);
987 : 1766048 : if (!potential && complain)
988 : 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
989 : : }
990 : : }
991 : :
992 : 18566643 : if (!potential && complain
993 : : /* If -Wno-invalid-constexpr was specified, we haven't complained
994 : : about non-constant expressions yet. Register the function and
995 : : complain in explain_invalid_constexpr_fn if the function is
996 : : called. */
997 : 218 : && warn_invalid_constexpr != 0)
998 : : return;
999 : :
1000 : 18566466 : if (implicit)
1001 : : {
1002 : 8 : if (potential)
1003 : : {
1004 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
1005 : 0 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
1006 : 0 : if (DECL_CONSTRUCTOR_P (fun))
1007 : 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
1008 : : }
1009 : : else
1010 : : /* Don't bother keeping the pre-generic body of unsuitable functions
1011 : : not explicitly declared constexpr. */
1012 : : return;
1013 : : }
1014 : :
1015 : 18566458 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1016 : 18566458 : bool clear_ctx = false;
1017 : 18566458 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1018 : : {
1019 : 18566458 : clear_ctx = true;
1020 : 18566458 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1021 : : }
1022 : 18566458 : tree saved_fn = current_function_decl;
1023 : 18566458 : current_function_decl = fun;
1024 : 18566458 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1025 : 18566458 : current_function_decl = saved_fn;
1026 : 18566458 : if (clear_ctx)
1027 : 18566458 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1028 : 18566458 : if (!potential)
1029 : : /* For a template instantiation, we want to remember the pre-generic body
1030 : : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1031 : : that it doesn't need to bother trying to expand the function. */
1032 : 82670 : entry.result = error_mark_node;
1033 : :
1034 : 18566458 : register_constexpr_fundef (entry);
1035 : : }
1036 : :
1037 : : /* BODY is a validated and massaged definition of a constexpr
1038 : : function. Register it in the hash table. */
1039 : :
1040 : : void
1041 : 18588271 : register_constexpr_fundef (const constexpr_fundef &value)
1042 : : {
1043 : : /* Create the constexpr function table if necessary. */
1044 : 18588271 : if (constexpr_fundef_table == NULL)
1045 : 25791 : constexpr_fundef_table
1046 : 25791 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1047 : :
1048 : 18588271 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1049 : 18588271 : (const_cast<constexpr_fundef *> (&value), INSERT);
1050 : :
1051 : 18588271 : gcc_assert (*slot == NULL);
1052 : 18588271 : *slot = ggc_alloc<constexpr_fundef> ();
1053 : 18588271 : **slot = value;
1054 : 18588271 : }
1055 : :
1056 : : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1057 : : function called in a context that requires a constant expression).
1058 : : If it comes from a constexpr template, explain why the instantiation
1059 : : isn't constexpr. Otherwise, explain why the function cannot be used
1060 : : in a constexpr context. */
1061 : :
1062 : : void
1063 : 776 : explain_invalid_constexpr_fn (tree fun)
1064 : : {
1065 : 776 : static hash_set<tree> *diagnosed;
1066 : 776 : tree body;
1067 : :
1068 : : /* Don't try to explain a function we already complained about. */
1069 : 776 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1070 : 583 : if (f->language->erroneous)
1071 : 776 : return;
1072 : :
1073 : : /* In C++23, a function marked 'constexpr' may not actually be a constant
1074 : : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1075 : : wasn't enabled. The function was called, so diagnose why it cannot be
1076 : : used in a constant expression. */
1077 : 727 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1078 : : /* Go on. */;
1079 : : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1080 : 702 : else if (!DECL_DEFAULTED_FN (fun)
1081 : 925 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1082 : 675 : && !(flag_implicit_constexpr
1083 : 6 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1084 : 6 : && DECL_DECLARED_INLINE_P (fun))
1085 : 1374 : && !is_instantiation_of_constexpr (fun))
1086 : : {
1087 : 658 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1088 : 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1089 : 661 : && decl_defined_p (fun))
1090 : 3 : inform (DECL_SOURCE_LOCATION (fun),
1091 : : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1092 : 658 : return;
1093 : : }
1094 : 69 : if (diagnosed == NULL)
1095 : 55 : diagnosed = new hash_set<tree>;
1096 : 69 : if (diagnosed->add (fun))
1097 : : /* Already explained. */
1098 : : return;
1099 : :
1100 : 68 : iloc_sentinel ils = input_location;
1101 : 68 : if (!lambda_static_thunk_p (fun))
1102 : : {
1103 : : /* Diagnostics should completely ignore the static thunk, so leave
1104 : : input_location set to our caller's location. */
1105 : 66 : input_location = DECL_SOURCE_LOCATION (fun);
1106 : 66 : inform (input_location,
1107 : : "%qD is not usable as a %<constexpr%> function because:", fun);
1108 : : }
1109 : : /* First check the declaration. */
1110 : 68 : if (is_valid_constexpr_fn (fun, true))
1111 : : {
1112 : : /* Then if it's OK, the body. */
1113 : 62 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1114 : 62 : && DECL_DEFAULTED_FN (fun))
1115 : 10 : explain_implicit_non_constexpr (fun);
1116 : : else
1117 : : {
1118 : 52 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1119 : 40 : body = fd->body;
1120 : : else
1121 : 12 : body = DECL_SAVED_TREE (fun);
1122 : 52 : tree massaged = massage_constexpr_body (fun, body);
1123 : 52 : require_potential_rvalue_constant_expression (massaged);
1124 : 104 : if (DECL_CONSTRUCTOR_P (fun))
1125 : : {
1126 : 11 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1127 : 11 : if (cxx_dialect > cxx11)
1128 : : /* Also check the body, not just the ctor-initializer. */
1129 : 9 : require_potential_rvalue_constant_expression (body);
1130 : : }
1131 : : }
1132 : : }
1133 : 68 : }
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 : 39401669 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1157 : : };
1158 : :
1159 : : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1160 : : {
1161 : : static hashval_t hash (constexpr_call *);
1162 : : static bool equal (constexpr_call *, constexpr_call *);
1163 : : };
1164 : :
1165 : : enum constexpr_switch_state {
1166 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1167 : : and default: label for that switch has not been seen yet. */
1168 : : css_default_not_seen,
1169 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1170 : : and default: label for that switch has been seen already. */
1171 : : css_default_seen,
1172 : : /* Used when processing a switch for the second time by
1173 : : cxx_eval_switch_expr, where default: label should match. */
1174 : : css_default_processing
1175 : : };
1176 : :
1177 : : /* The constexpr expansion context part which needs one instance per
1178 : : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1179 : : variables initialized within the expression. */
1180 : :
1181 : : class constexpr_global_ctx {
1182 : : /* Values for any temporaries or local variables within the
1183 : : constant-expression. Objects outside their lifetime have
1184 : : value 'void_node'. */
1185 : : hash_map<tree,tree> values;
1186 : : public:
1187 : : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1188 : : on simple constants or location wrappers) encountered during current
1189 : : cxx_eval_outermost_constant_expr call. */
1190 : : HOST_WIDE_INT constexpr_ops_count;
1191 : : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1192 : : expression. */
1193 : : auto_vec<tree, 16> heap_vars;
1194 : : /* Vector of caught exceptions, including exceptions still not active at
1195 : : the start of a handler (those are immediately followed up by HANDLER_TYPE
1196 : : until __cxa_begin_catch finishes). */
1197 : : auto_vec<tree, 2> caught_exceptions;
1198 : : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1199 : : vec<tree> *cleanups;
1200 : : /* If non-null, only allow modification of existing values of the variables
1201 : : in this set. Set by modifiable_tracker, below. */
1202 : : hash_set<tree> *modifiable;
1203 : : /* Number of heap VAR_DECL deallocations. */
1204 : : unsigned heap_dealloc_count;
1205 : : /* Number of uncaught exceptions. */
1206 : : unsigned uncaught_exceptions;
1207 : :
1208 : : /* Constructor. */
1209 : 385893052 : constexpr_global_ctx ()
1210 : 771786104 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1211 : 385893052 : heap_dealloc_count (0), uncaught_exceptions (0) {}
1212 : :
1213 : 209053416 : bool is_outside_lifetime (tree t)
1214 : : {
1215 : 209053416 : if (tree *p = values.get (t))
1216 : 50734544 : if (*p == void_node)
1217 : 178 : return true;
1218 : : return false;
1219 : : }
1220 : 247296172 : tree get_value (tree t)
1221 : : {
1222 : 247296172 : if (tree *p = values.get (t))
1223 : 79018868 : if (*p != void_node)
1224 : 77799735 : return *p;
1225 : : return NULL_TREE;
1226 : : }
1227 : 34045077 : tree *get_value_ptr (tree t, bool initializing)
1228 : : {
1229 : 34045077 : if (modifiable && !modifiable->contains (t))
1230 : : return nullptr;
1231 : 34045066 : if (tree *p = values.get (t))
1232 : : {
1233 : 34040830 : if (*p != void_node)
1234 : : return p;
1235 : 36 : else if (initializing)
1236 : : {
1237 : 6 : *p = NULL_TREE;
1238 : 6 : return p;
1239 : : }
1240 : : }
1241 : : return nullptr;
1242 : : }
1243 : 98611677 : void put_value (tree t, tree v)
1244 : : {
1245 : 98611677 : bool already_in_map = values.put (t, v);
1246 : 98611677 : if (!already_in_map && modifiable)
1247 : 32 : modifiable->add (t);
1248 : 98611677 : }
1249 : 71923793 : void destroy_value (tree t)
1250 : : {
1251 : 71923793 : if (TREE_CODE (t) == VAR_DECL
1252 : 71923793 : || TREE_CODE (t) == PARM_DECL
1253 : : || TREE_CODE (t) == RESULT_DECL)
1254 : 71917562 : values.put (t, void_node);
1255 : : else
1256 : 6231 : values.remove (t);
1257 : 71923793 : }
1258 : 3940319 : void clear_value (tree t)
1259 : : {
1260 : 7880638 : values.remove (t);
1261 : : }
1262 : : };
1263 : :
1264 : : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1265 : : side-effects from evaluation of a particular subexpression of a
1266 : : constant-expression. In such cases we use modifiable_tracker to prevent
1267 : : modification of variables created outside of that subexpression.
1268 : :
1269 : : ??? We could change the hash_set to a hash_map, allow and track external
1270 : : modifications, and roll them back in the destructor. It's not clear to me
1271 : : that this would be worthwhile. */
1272 : :
1273 : : class modifiable_tracker
1274 : : {
1275 : : hash_set<tree> set;
1276 : : constexpr_global_ctx *global;
1277 : : public:
1278 : 283590 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1279 : : {
1280 : 283590 : global->modifiable = &set;
1281 : 283590 : }
1282 : 283590 : ~modifiable_tracker ()
1283 : : {
1284 : 283622 : for (tree t: set)
1285 : 32 : global->clear_value (t);
1286 : 283590 : global->modifiable = nullptr;
1287 : 283590 : }
1288 : : };
1289 : :
1290 : : /* The constexpr expansion context. CALL is the current function
1291 : : expansion, CTOR is the current aggregate initializer, OBJECT is the
1292 : : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1293 : :
1294 : : struct constexpr_ctx {
1295 : : /* The part of the context that needs to be unique to the whole
1296 : : cxx_eval_outermost_constant_expr invocation. */
1297 : : constexpr_global_ctx *global;
1298 : : /* The innermost call we're evaluating. */
1299 : : constexpr_call *call;
1300 : : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1301 : : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1302 : : vec<tree> *save_exprs;
1303 : : /* The CONSTRUCTOR we're currently building up for an aggregate
1304 : : initializer. */
1305 : : tree ctor;
1306 : : /* The object we're building the CONSTRUCTOR for. */
1307 : : tree object;
1308 : : /* If inside SWITCH_EXPR. */
1309 : : constexpr_switch_state *css_state;
1310 : : /* The aggregate initialization context inside which this one is nested. This
1311 : : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1312 : : const constexpr_ctx *parent;
1313 : :
1314 : : /* Whether we should error on a non-constant expression or fail quietly.
1315 : : This flag needs to be here, but some of the others could move to global
1316 : : if they get larger than a word. */
1317 : : bool quiet;
1318 : : /* Whether we are strictly conforming to constant expression rules or
1319 : : trying harder to get a constant value. */
1320 : : bool strict;
1321 : : /* Whether __builtin_is_constant_evaluated () should be true. */
1322 : : mce_value manifestly_const_eval;
1323 : : };
1324 : :
1325 : : /* Predicates for the meaning of *jump_target. */
1326 : :
1327 : : static bool
1328 : 32869835 : returns (tree *jump_target)
1329 : : {
1330 : 5952160 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1331 : : }
1332 : :
1333 : : static bool
1334 : 32844347 : breaks (tree *jump_target)
1335 : : {
1336 : 32844347 : return (*jump_target
1337 : 32844347 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1338 : 52 : && LABEL_DECL_BREAK (*jump_target))
1339 : 1107501 : || TREE_CODE (*jump_target) == BREAK_STMT
1340 : 1061223 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1341 : : }
1342 : :
1343 : : static bool
1344 : 49169821 : continues (tree *jump_target)
1345 : : {
1346 : 49169821 : return (*jump_target
1347 : 49169821 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1348 : 88 : && LABEL_DECL_CONTINUE (*jump_target))
1349 : 1085994 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1350 : : }
1351 : :
1352 : : static bool
1353 : 6469376 : switches (tree *jump_target)
1354 : : {
1355 : 51524 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1356 : : }
1357 : :
1358 : : static bool
1359 : 1064305997 : throws (tree *jump_target)
1360 : : {
1361 : : /* void_node is for use in potential_constant_expression_1, otherwise
1362 : : it should an artificial VAR_DECL created by constant evaluation
1363 : : of __cxa_allocate_exception (). */
1364 : 59032429 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1365 : : }
1366 : :
1367 : : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1368 : : references and objects are in effect. */
1369 : :
1370 : : static bool
1371 : 160997913 : p2280_active_p (const constexpr_ctx *ctx)
1372 : : {
1373 : 28049566 : if (ctx->manifestly_const_eval != mce_true)
1374 : : /* Disable these relaxations during speculative constexpr folding,
1375 : : as it can significantly increase compile time/memory use
1376 : : (PR119387). */
1377 : : return false;
1378 : :
1379 : : /* P2280R4 was accepted as a DR against C++11. */
1380 : 0 : return cxx_dialect >= cxx11;
1381 : : }
1382 : :
1383 : : /* Remove T from the global values map, checking for attempts to destroy
1384 : : a value that has already finished its lifetime. */
1385 : :
1386 : : static void
1387 : 71891135 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1388 : : {
1389 : 71891135 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1390 : : return;
1391 : :
1392 : : /* Don't error again here if we've already reported a problem. */
1393 : 71891126 : if (!*non_constant_p
1394 : 50964763 : && DECL_P (t)
1395 : : /* Non-trivial destructors have their lifetimes ended explicitly
1396 : : with a clobber, so don't worry about it here. */
1397 : 50958711 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1398 : : /* ...except parameters are remapped in cxx_eval_call_expression,
1399 : : and the destructor call during cleanup won't be able to tell that
1400 : : this value has already been destroyed, so complain now. This is
1401 : : not quite unobservable, but is extremely unlikely to crop up in
1402 : : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1403 : 199986 : || TREE_CODE (t) == PARM_DECL)
1404 : 122650149 : && ctx->global->is_outside_lifetime (t))
1405 : : {
1406 : 54 : if (!ctx->quiet)
1407 : : {
1408 : 18 : auto_diagnostic_group d;
1409 : 18 : error ("destroying %qE outside its lifetime", t);
1410 : 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1411 : 18 : }
1412 : 54 : *non_constant_p = true;
1413 : : }
1414 : 71891126 : ctx->global->destroy_value (t);
1415 : : }
1416 : :
1417 : : /* This internal flag controls whether we should avoid doing anything during
1418 : : constexpr evaluation that would cause extra DECL_UID generation, such as
1419 : : template instantiation and function body copying. */
1420 : :
1421 : : static bool uid_sensitive_constexpr_evaluation_value;
1422 : :
1423 : : /* An internal counter that keeps track of the number of times
1424 : : uid_sensitive_constexpr_evaluation_p returned true. */
1425 : :
1426 : : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1427 : :
1428 : : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1429 : : increments the corresponding counter. */
1430 : :
1431 : : static bool
1432 : 5033680 : uid_sensitive_constexpr_evaluation_p ()
1433 : : {
1434 : 5029338 : if (uid_sensitive_constexpr_evaluation_value)
1435 : : {
1436 : 181357 : ++uid_sensitive_constexpr_evaluation_true_counter;
1437 : 181357 : return true;
1438 : : }
1439 : : else
1440 : : return false;
1441 : : }
1442 : :
1443 : : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1444 : : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1445 : : during the lifetime of the sentinel object. Upon its destruction, the
1446 : : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1447 : :
1448 : 66888239 : uid_sensitive_constexpr_evaluation_sentinel
1449 : 66888239 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1450 : 66888239 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1451 : : {
1452 : 66888239 : }
1453 : :
1454 : : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1455 : : records the current number of times that uid_sensitive_constexpr_evaluation_p
1456 : : has been called and returned true. */
1457 : :
1458 : 1879592360 : uid_sensitive_constexpr_evaluation_checker
1459 : 1879592360 : ::uid_sensitive_constexpr_evaluation_checker ()
1460 : 1879592360 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1461 : : {
1462 : 1879592360 : }
1463 : :
1464 : : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1465 : : some constexpr evaluation was restricted due to u_s_c_e_p being called
1466 : : and returning true during the lifetime of this checker object. */
1467 : :
1468 : : bool
1469 : 1326194428 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1470 : : {
1471 : 1326194428 : return (uid_sensitive_constexpr_evaluation_value
1472 : 1326194428 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1473 : : }
1474 : :
1475 : :
1476 : : /* A table of all constexpr calls that have been evaluated by the
1477 : : compiler in this translation unit. */
1478 : :
1479 : : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1480 : :
1481 : : /* Compute a hash value for a constexpr call representation. */
1482 : :
1483 : : inline hashval_t
1484 : 104335025 : constexpr_call_hasher::hash (constexpr_call *info)
1485 : : {
1486 : 104335025 : return info->hash;
1487 : : }
1488 : :
1489 : : /* Return true if the objects pointed to by P and Q represent calls
1490 : : to the same constexpr function with the same arguments.
1491 : : Otherwise, return false. */
1492 : :
1493 : : bool
1494 : 111266921 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1495 : : {
1496 : 111266921 : if (lhs == rhs)
1497 : : return true;
1498 : 111266921 : if (lhs->hash != rhs->hash)
1499 : : return false;
1500 : 16615548 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1501 : : return false;
1502 : 16615548 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1503 : : }
1504 : :
1505 : : /* Initialize the constexpr call table, if needed. */
1506 : :
1507 : : static void
1508 : 19921664 : maybe_initialize_constexpr_call_table (void)
1509 : : {
1510 : 19921664 : if (constexpr_call_table == NULL)
1511 : 17281 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1512 : 19921664 : }
1513 : :
1514 : : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1515 : : a function happens to get called recursively, we unshare the callee
1516 : : function's body and evaluate this unshared copy instead of evaluating the
1517 : : original body.
1518 : :
1519 : : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1520 : : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1521 : : that's keyed off of the original FUNCTION_DECL and whose value is a
1522 : : TREE_LIST of this function's unused copies awaiting reuse.
1523 : :
1524 : : This is not GC-deletable to avoid GC affecting UID generation. */
1525 : :
1526 : : static GTY(()) decl_tree_map *fundef_copies_table;
1527 : :
1528 : : /* Reuse a copy or create a new unshared copy of the function FUN.
1529 : : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1530 : : is parms, TYPE is result. */
1531 : :
1532 : : static tree
1533 : 29740059 : get_fundef_copy (constexpr_fundef *fundef)
1534 : : {
1535 : 29740059 : tree copy;
1536 : 29740059 : bool existed;
1537 : 29740059 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1538 : 29740059 : (fundef_copies_table, fundef->decl, &existed, 127));
1539 : :
1540 : 29740059 : if (!existed)
1541 : : {
1542 : : /* There is no cached function available, or in use. We can use
1543 : : the function directly. That the slot is now created records
1544 : : that this function is now in use. */
1545 : 4559031 : copy = build_tree_list (fundef->body, fundef->parms);
1546 : 4559031 : TREE_TYPE (copy) = fundef->result;
1547 : : }
1548 : 25181028 : else if (*slot == NULL_TREE)
1549 : : {
1550 : 4342 : if (uid_sensitive_constexpr_evaluation_p ())
1551 : 0 : return NULL_TREE;
1552 : :
1553 : : /* We've already used the function itself, so make a copy. */
1554 : 4342 : copy = build_tree_list (NULL, NULL);
1555 : 4342 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1556 : 4342 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1557 : 4342 : tree saved_result = DECL_RESULT (fundef->decl);
1558 : 4342 : tree saved_fn = current_function_decl;
1559 : 4342 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1560 : 4342 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1561 : 4342 : DECL_RESULT (fundef->decl) = fundef->result;
1562 : 4342 : current_function_decl = fundef->decl;
1563 : 4342 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1564 : 4342 : TREE_TYPE (copy));
1565 : 4342 : current_function_decl = saved_fn;
1566 : 4342 : DECL_RESULT (fundef->decl) = saved_result;
1567 : 4342 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1568 : 4342 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1569 : : }
1570 : : else
1571 : : {
1572 : : /* We have a cached function available. */
1573 : 25176686 : copy = *slot;
1574 : 25176686 : *slot = TREE_CHAIN (copy);
1575 : : }
1576 : :
1577 : : return copy;
1578 : : }
1579 : :
1580 : : /* Save the copy COPY of function FUN for later reuse by
1581 : : get_fundef_copy(). By construction, there will always be an entry
1582 : : to find. */
1583 : :
1584 : : static void
1585 : 29740057 : save_fundef_copy (tree fun, tree copy)
1586 : : {
1587 : 29740057 : tree *slot = fundef_copies_table->get (fun);
1588 : 29740057 : TREE_CHAIN (copy) = *slot;
1589 : 29740057 : *slot = copy;
1590 : 29740057 : }
1591 : :
1592 : : /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1593 : : a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1594 : :
1595 : : enum value_cat {
1596 : : vc_prvalue = 0,
1597 : : vc_glvalue = 1,
1598 : : vc_discard = 2
1599 : : };
1600 : :
1601 : : static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1602 : : value_cat, bool *, bool *, tree *);
1603 : : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1604 : : value_cat, bool *, bool *, tree *);
1605 : : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1606 : : bool *, tree *);
1607 : : static tree find_heap_var_refs (tree *, int *, void *);
1608 : :
1609 : : /* For exception object EXC if it has class type and usable what () method
1610 : : which returns cv char * return the xmalloced string literal which it returns
1611 : : if possible, otherwise return NULL. */
1612 : :
1613 : : static char *
1614 : 87 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1615 : : {
1616 : 87 : tree type = strip_array_types (TREE_TYPE (exc));
1617 : 87 : if (!CLASS_TYPE_P (type))
1618 : : return NULL;
1619 : 49 : tree std_exception = lookup_qualified_name (std_node, "exception",
1620 : : LOOK_want::NORMAL, false);
1621 : 49 : if (TREE_CODE (std_exception) != TYPE_DECL)
1622 : : return NULL;
1623 : 46 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1624 : : return NULL;
1625 : 46 : base_kind b_kind;
1626 : 46 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1627 : : tf_none);
1628 : 46 : if (binfo == NULL_TREE || binfo == error_mark_node)
1629 : : return NULL;
1630 : 42 : if (type != TREE_TYPE (exc))
1631 : 4 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1632 : 42 : tree call
1633 : 42 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1634 : : tf_none);
1635 : 42 : if (call == error_mark_node)
1636 : : return NULL;
1637 : 42 : releasing_vec what_args;
1638 : 42 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1639 : 42 : if (call == error_mark_node)
1640 : : return NULL;
1641 : 42 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1642 : 42 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1643 : 42 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1644 : 42 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1645 : 42 : TYPE_SIZE_UNIT (char_type_node))
1646 : 84 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1647 : 0 : return NULL;
1648 : 42 : if (!potential_constant_expression (call))
1649 : : return NULL;
1650 : 42 : bool non_constant_p = false, overflow_p = false;
1651 : 42 : tree jmp_target = NULL;
1652 : 42 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1653 : : &non_constant_p, &overflow_p,
1654 : : &jmp_target);
1655 : 42 : if (throws (&jmp_target) || non_constant_p)
1656 : : return NULL;
1657 : 42 : if (reduced_constant_expression_p (ptr))
1658 : 40 : if (const char *msg = c_getstr (ptr))
1659 : 40 : return xstrdup (msg);
1660 : 2 : auto_vec <char, 32> v;
1661 : 26 : for (unsigned i = 0; i < INT_MAX; ++i)
1662 : : {
1663 : 26 : tree t = call;
1664 : 26 : if (i)
1665 : 24 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1666 : 26 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1667 : 26 : non_constant_p = false;
1668 : 26 : overflow_p = false;
1669 : 26 : jmp_target = NULL;
1670 : 26 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1671 : : &non_constant_p, &overflow_p,
1672 : : &jmp_target);
1673 : 26 : if (throws (&jmp_target)
1674 : 26 : || non_constant_p
1675 : 26 : || !tree_fits_shwi_p (t2))
1676 : 0 : return NULL;
1677 : 26 : char c = tree_to_shwi (t2);
1678 : 26 : v.safe_push (c);
1679 : 26 : if (c == '\0')
1680 : : break;
1681 : : }
1682 : 4 : return xstrdup (v.address ());
1683 : 44 : }
1684 : :
1685 : : /* Diagnose constant expression evaluation encountering call to
1686 : : std::terminate due to exception EXC. */
1687 : :
1688 : : static void
1689 : 11 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1690 : : {
1691 : 11 : tree type = strip_array_types (TREE_TYPE (exc));
1692 : 11 : if (char *str = exception_what_str (ctx, exc))
1693 : : {
1694 : 2 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1695 : : "%<what()%>: %qs", "std::terminate", type, str);
1696 : 2 : free (str);
1697 : : }
1698 : : else
1699 : : {
1700 : 9 : if (type != TREE_TYPE (exc))
1701 : 9 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1702 : 9 : bool non_constant_p = false, overflow_p = false;
1703 : 9 : tree jmp_target = NULL;
1704 : 9 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1705 : : &non_constant_p, &overflow_p,
1706 : : &jmp_target);
1707 : 9 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1708 : 9 : if (reduced_constant_expression_p (val))
1709 : 9 : error_at (loc, "%qs called after throwing an exception %qE",
1710 : : "std::terminate", val);
1711 : : else
1712 : 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1713 : : "std::terminate", type);
1714 : : }
1715 : 11 : }
1716 : :
1717 : : /* Diagnose constant expression evaluation encountering call to
1718 : : uncaught exception EXC. */
1719 : :
1720 : : static void
1721 : 76 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1722 : : {
1723 : 76 : tree type = strip_array_types (TREE_TYPE (exc));
1724 : 76 : if (char *str = exception_what_str (ctx, exc))
1725 : : {
1726 : 40 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1727 : 40 : free (str);
1728 : : }
1729 : : else
1730 : : {
1731 : 36 : if (type != TREE_TYPE (exc))
1732 : 36 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1733 : 36 : bool non_constant_p = false, overflow_p = false;
1734 : 36 : tree jmp_target = NULL;
1735 : 36 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1736 : : &non_constant_p, &overflow_p,
1737 : : &jmp_target);
1738 : 36 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1739 : 36 : if (reduced_constant_expression_p (val))
1740 : 35 : error_at (loc, "uncaught exception %qE", val);
1741 : : else
1742 : 1 : error_at (loc, "uncaught exception of type %qT", type);
1743 : : }
1744 : 76 : }
1745 : :
1746 : : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1747 : : magic constexpr functions for C++26. */
1748 : :
1749 : : enum cxa_builtin {
1750 : : CXA_NONE = 0,
1751 : : CXA_ALLOCATE_EXCEPTION = 1,
1752 : : CXA_FREE_EXCEPTION = 2,
1753 : : CXA_THROW = 3,
1754 : : CXA_BEGIN_CATCH = 4,
1755 : : CXA_END_CATCH = 5,
1756 : : CXA_RETHROW = 6,
1757 : : CXA_GET_EXCEPTION_PTR = 7,
1758 : : CXA_BAD_CAST = 8,
1759 : : CXA_BAD_TYPEID = 9,
1760 : : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1761 : : STD_UNCAUGHT_EXCEPTIONS = 11,
1762 : : STD_CURRENT_EXCEPTION = 12,
1763 : : STD_RETHROW_EXCEPTION = 13,
1764 : : BUILTIN_EH_PTR_ADJUST_REF = 14
1765 : : };
1766 : :
1767 : : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1768 : : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1769 : :
1770 : : static enum cxa_builtin
1771 : 30695383 : cxx_cxa_builtin_fn_p (tree fndecl)
1772 : : {
1773 : 30695383 : if (cxx_dialect < cxx26)
1774 : : return CXA_NONE;
1775 : 5157907 : if (DECL_LANGUAGE (fndecl) != lang_c)
1776 : : {
1777 : 4711950 : if (!decl_in_std_namespace_p (fndecl))
1778 : : return CXA_NONE;
1779 : 1904203 : if (id_equal (DECL_NAME (fndecl), "uncaught_exceptions"))
1780 : : return STD_UNCAUGHT_EXCEPTIONS;
1781 : 1904082 : if (id_equal (DECL_NAME (fndecl), "current_exception"))
1782 : : return STD_CURRENT_EXCEPTION;
1783 : 1885332 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1784 : : return STD_RETHROW_EXCEPTION;
1785 : : return CXA_NONE;
1786 : : }
1787 : 445957 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1788 : : return CXA_NONE;
1789 : 40621 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1790 : : return CXA_ALLOCATE_EXCEPTION;
1791 : 6654 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1792 : : return CXA_FREE_EXCEPTION;
1793 : 6653 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1794 : : return CXA_THROW;
1795 : 6198 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1796 : : return CXA_BEGIN_CATCH;
1797 : 1104 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1798 : : return CXA_END_CATCH;
1799 : 930 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1800 : : return CXA_RETHROW;
1801 : 874 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1802 : : return CXA_GET_EXCEPTION_PTR;
1803 : 854 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1804 : : return CXA_BAD_CAST;
1805 : 570 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1806 : : return CXA_BAD_TYPEID;
1807 : 562 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1808 : : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1809 : : return CXA_NONE;
1810 : : }
1811 : :
1812 : : /* Helper function for cxx_eval_cxa_builtin_fn.
1813 : : Check if ARG is a valid first argument of __cxa_throw or
1814 : : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1815 : : not, otherwise return the artificial __cxa_allocate_exception allocated
1816 : : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1817 : :
1818 : : static tree
1819 : 409 : cxa_check_throw_arg (tree arg, bool free_exc)
1820 : : {
1821 : 409 : STRIP_NOPS (arg);
1822 : 409 : if (TREE_CODE (arg) != ADDR_EXPR)
1823 : : return NULL_TREE;
1824 : 409 : arg = TREE_OPERAND (arg, 0);
1825 : 409 : if (!VAR_P (arg)
1826 : 409 : || !DECL_ARTIFICIAL (arg)
1827 : 409 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1828 : 409 : && DECL_NAME (arg) != heap_identifier)
1829 : 818 : || !DECL_LANG_SPECIFIC (arg))
1830 : 0 : return NULL_TREE;
1831 : : return arg;
1832 : : }
1833 : :
1834 : : /* Helper function for cxx_eval_cxa_builtin_fn.
1835 : : "Allocate" on the constexpr heap an exception object of TYPE
1836 : : with REFCOUNT. */
1837 : :
1838 : : static tree
1839 : 16408 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1840 : : tree refcount)
1841 : : {
1842 : 16408 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1843 : 16408 : DECL_ARTIFICIAL (var) = 1;
1844 : 16408 : retrofit_lang_decl (var);
1845 : 16408 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1846 : 16408 : ctx->global->heap_vars.safe_push (var);
1847 : 16408 : return var;
1848 : : }
1849 : :
1850 : : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1851 : : C++26 constexpr exception support (P3068R5). */
1852 : :
1853 : : static tree
1854 : 27584 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1855 : : enum cxa_builtin kind, tree fndecl,
1856 : : bool *non_constant_p, bool *overflow_p,
1857 : : tree *jump_target)
1858 : : {
1859 : 27584 : int nargs = call_expr_nargs (call);
1860 : 27584 : location_t loc = cp_expr_loc_or_input_loc (call);
1861 : 27584 : tree args[4], arg;
1862 : 27584 : if (nargs > 4)
1863 : : {
1864 : 0 : invalid_nargs:
1865 : 0 : if (!ctx->quiet)
1866 : 0 : error_at (loc, "call to %qD function with incorrect "
1867 : : "number of arguments", fndecl);
1868 : 0 : *non_constant_p = true;
1869 : 0 : return call;
1870 : : }
1871 : 27584 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1872 : 2651 : && nargs == 1
1873 : 2651 : && (arg = CALL_EXPR_ARG (call, 0))
1874 : 2651 : && TREE_CODE (arg) == CALL_EXPR
1875 : 2651 : && call_expr_nargs (arg) == 1
1876 : 30235 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1877 : 2651 : if (tree fun = get_function_named_in_call (arg))
1878 : 2651 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1879 : : {
1880 : 2651 : if (ctx->global->caught_exceptions.length () < 2)
1881 : : {
1882 : 2463 : no_caught_exceptions:
1883 : 2463 : if (!ctx->quiet)
1884 : 0 : error_at (loc, "%qD called with no caught exceptions pending",
1885 : : fndecl);
1886 : 2463 : *non_constant_p = true;
1887 : 2463 : return call;
1888 : : }
1889 : : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1890 : : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1891 : : ctx->global->caught_exceptions vector to end with
1892 : : __cxa_allocate_exception created artificial VAR_DECL (the
1893 : : exception object) followed by handler type, pushed by TRY_BLOCK
1894 : : evaluation. The only difference between the functions is that
1895 : : __cxa_begin_catch pops the handler type from the vector and keeps
1896 : : the VAR_DECL last and decreases uncaught_exceptions. The
1897 : : VAR_DECL after __cxa_begin_catch serves as the current exception
1898 : : and is then popped in __cxa_end_catch evaluation. */
1899 : 188 : tree handler_type = ctx->global->caught_exceptions.last ();
1900 : 188 : if (handler_type && VAR_P (handler_type))
1901 : 0 : goto no_caught_exceptions;
1902 : 188 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1903 : 188 : arg = ctx->global->caught_exceptions[idx];
1904 : 188 : gcc_assert (VAR_P (arg));
1905 : 188 : if (kind == CXA_BEGIN_CATCH)
1906 : : {
1907 : 176 : ctx->global->caught_exceptions.pop ();
1908 : 176 : --ctx->global->uncaught_exceptions;
1909 : : }
1910 : 188 : if (handler_type == NULL_TREE)
1911 : : /* Used for catch (...). Just return void. */
1912 : 32 : return void_node;
1913 : 156 : else if (POINTER_TYPE_P (handler_type))
1914 : : {
1915 : : /* Used for catch of a pointer. */
1916 : 33 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1917 : 33 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1918 : : size_zero_node, NULL_TREE, NULL_TREE);
1919 : 33 : arg = cp_convert (handler_type, arg,
1920 : 33 : ctx->quiet ? tf_none : tf_warning_or_error);
1921 : 33 : if (arg == error_mark_node)
1922 : : {
1923 : 0 : *non_constant_p = true;
1924 : 0 : return call;
1925 : : }
1926 : : }
1927 : : else
1928 : : {
1929 : : /* Used for catch of a non-pointer type. */
1930 : 123 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1931 : 123 : tree exc_ptr_type = build_pointer_type (exc_type);
1932 : 123 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
1933 : 123 : if (CLASS_TYPE_P (handler_type))
1934 : : {
1935 : 82 : tree ptr_type = build_pointer_type (handler_type);
1936 : 82 : arg = cp_convert (ptr_type, arg,
1937 : 82 : ctx->quiet ? tf_none
1938 : : : tf_warning_or_error);
1939 : 82 : if (arg == error_mark_node)
1940 : : {
1941 : 0 : *non_constant_p = true;
1942 : 0 : return call;
1943 : : }
1944 : : }
1945 : : }
1946 : 156 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
1947 : : non_constant_p, overflow_p,
1948 : 156 : jump_target);
1949 : : }
1950 : 42323 : for (int i = 0; i < nargs; ++i)
1951 : : {
1952 : 17390 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1953 : : vc_prvalue, non_constant_p,
1954 : : overflow_p, jump_target);
1955 : 17390 : if (*non_constant_p)
1956 : : return call;
1957 : 17390 : if (*jump_target)
1958 : : return NULL_TREE;
1959 : : }
1960 : 24933 : switch (kind)
1961 : : {
1962 : 16280 : case CXA_ALLOCATE_EXCEPTION:
1963 : 16280 : if (nargs != 1)
1964 : 0 : goto invalid_nargs;
1965 : 16280 : if (!tree_fits_uhwi_p (args[0]))
1966 : : {
1967 : 0 : if (!ctx->quiet)
1968 : 0 : error_at (loc, "cannot allocate exception: size not constant");
1969 : 0 : *non_constant_p = true;
1970 : 0 : return call;
1971 : : }
1972 : : else
1973 : : {
1974 : 32560 : tree type = build_array_type_nelts (char_type_node,
1975 : 16280 : tree_to_uhwi (args[0]));
1976 : 16280 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
1977 : 16280 : ctx->global->put_value (var, NULL_TREE);
1978 : 16280 : return fold_convert (ptr_type_node, build_address (var));
1979 : : }
1980 : 1 : case CXA_FREE_EXCEPTION:
1981 : 1 : if (nargs != 1)
1982 : 0 : goto invalid_nargs;
1983 : 1 : arg = cxa_check_throw_arg (args[0], true);
1984 : 1 : if (arg == NULL_TREE)
1985 : : {
1986 : 0 : invalid_ptr:
1987 : 0 : if (!ctx->quiet)
1988 : 0 : error_at (loc, "first argument to %qD function not result of "
1989 : : "%<__cxa_allocate_exception%>", fndecl);
1990 : 0 : *non_constant_p = true;
1991 : 0 : return call;
1992 : : }
1993 : 1 : DECL_NAME (arg) = heap_deleted_identifier;
1994 : 1 : ctx->global->destroy_value (arg);
1995 : 1 : ctx->global->heap_dealloc_count++;
1996 : 1 : return void_node;
1997 : 315 : case CXA_THROW:
1998 : 315 : if (nargs != 3)
1999 : 0 : goto invalid_nargs;
2000 : 315 : arg = cxa_check_throw_arg (args[0], false);
2001 : 315 : if (arg == NULL_TREE)
2002 : 0 : goto invalid_ptr;
2003 : 315 : DECL_EXCEPTION_REFCOUNT (arg)
2004 : 315 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2005 : : size_one_node);
2006 : 315 : ++ctx->global->uncaught_exceptions;
2007 : 315 : *jump_target = arg;
2008 : 315 : return void_node;
2009 : 0 : case CXA_BEGIN_CATCH:
2010 : 0 : case CXA_GET_EXCEPTION_PTR:
2011 : 0 : goto invalid_nargs;
2012 : 174 : case CXA_END_CATCH:
2013 : 174 : if (nargs != 0)
2014 : 0 : goto invalid_nargs;
2015 : 174 : if (ctx->global->caught_exceptions.is_empty ())
2016 : : {
2017 : 0 : no_active_exc:
2018 : 0 : if (!ctx->quiet)
2019 : 0 : error_at (loc, "%qD called with no caught exceptions active",
2020 : : fndecl);
2021 : 0 : *non_constant_p = true;
2022 : 0 : return call;
2023 : : }
2024 : : else
2025 : : {
2026 : 174 : arg = ctx->global->caught_exceptions.pop ();
2027 : 174 : if (arg == NULL_TREE || !VAR_P (arg))
2028 : 0 : goto no_active_exc;
2029 : 174 : free_except:
2030 : 217 : DECL_EXCEPTION_REFCOUNT (arg)
2031 : 217 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2032 : : size_one_node);
2033 : 217 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2034 : : {
2035 : 111 : if (type_build_dtor_call (TREE_TYPE (arg)))
2036 : : {
2037 : 40 : tree cleanup
2038 : 40 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2039 : : : tf_warning_or_error));
2040 : 40 : if (cleanup == error_mark_node)
2041 : 0 : *non_constant_p = true;
2042 : 40 : tree jmp_target = NULL_TREE;
2043 : 40 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2044 : : non_constant_p, overflow_p,
2045 : : &jmp_target);
2046 : 40 : if (throws (&jmp_target))
2047 : 0 : *jump_target = jmp_target;
2048 : : }
2049 : 111 : DECL_NAME (arg) = heap_deleted_identifier;
2050 : 111 : ctx->global->destroy_value (arg);
2051 : 111 : ctx->global->heap_dealloc_count++;
2052 : : }
2053 : : }
2054 : 217 : return void_node;
2055 : 50 : case CXA_RETHROW:
2056 : 50 : if (nargs != 0)
2057 : 0 : goto invalid_nargs;
2058 : 50 : unsigned idx;
2059 : 100 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2060 : 36 : if (arg == NULL_TREE || !VAR_P (arg))
2061 : 0 : --idx;
2062 : : else
2063 : : break;
2064 : 50 : if (arg == NULL_TREE)
2065 : : {
2066 : 14 : if (!ctx->quiet)
2067 : 4 : error_at (loc, "%qD called with no caught exceptions active",
2068 : : fndecl);
2069 : 14 : *non_constant_p = true;
2070 : 14 : return call;
2071 : : }
2072 : 36 : DECL_EXCEPTION_REFCOUNT (arg)
2073 : 36 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2074 : 36 : ++ctx->global->uncaught_exceptions;
2075 : 36 : *jump_target = arg;
2076 : 36 : return void_node;
2077 : 143 : case CXA_BAD_CAST:
2078 : 143 : case CXA_BAD_TYPEID:
2079 : 143 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2080 : 143 : if (nargs != 0)
2081 : 0 : goto invalid_nargs;
2082 : : else
2083 : : {
2084 : 143 : tree name;
2085 : 143 : switch (kind)
2086 : : {
2087 : 117 : case CXA_BAD_CAST:
2088 : 117 : name = get_identifier ("bad_cast");
2089 : 117 : break;
2090 : 5 : case CXA_BAD_TYPEID:
2091 : 5 : name = get_identifier ("bad_typeid");
2092 : 5 : break;
2093 : 21 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2094 : 21 : name = get_identifier ("bad_array_new_length");
2095 : 21 : break;
2096 : : default:
2097 : : gcc_unreachable ();
2098 : : }
2099 : 143 : tree decl = lookup_qualified_name (std_node, name);
2100 : 143 : if (TREE_CODE (decl) != TYPE_DECL
2101 : 128 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2102 : 271 : || !type_build_ctor_call (TREE_TYPE (decl)))
2103 : : {
2104 : 15 : if (!ctx->quiet)
2105 : 4 : error_at (loc, "%qD called without %<std::%D%> being defined",
2106 : : fndecl, name);
2107 : 15 : *non_constant_p = true;
2108 : 15 : return call;
2109 : : }
2110 : 128 : tree type = TREE_TYPE (decl);
2111 : 128 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2112 : 128 : tree ctor
2113 : 128 : = build_special_member_call (var, complete_ctor_identifier,
2114 : : NULL, type, LOOKUP_NORMAL,
2115 : 128 : ctx->quiet ? tf_none
2116 : : : tf_warning_or_error);
2117 : 128 : if (ctor == error_mark_node)
2118 : : {
2119 : 0 : *non_constant_p = true;
2120 : 0 : return call;
2121 : : }
2122 : 128 : if (TREE_CONSTANT (ctor))
2123 : 0 : ctx->global->put_value (var, ctor);
2124 : : else
2125 : : {
2126 : 128 : ctx->global->put_value (var, NULL_TREE);
2127 : 128 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2128 : : non_constant_p, overflow_p,
2129 : : jump_target);
2130 : 128 : if (*non_constant_p)
2131 : : return call;
2132 : 128 : if (throws (jump_target))
2133 : : return NULL_TREE;
2134 : : }
2135 : 128 : ++ctx->global->uncaught_exceptions;
2136 : 128 : *jump_target = var;
2137 : : }
2138 : 128 : return void_node;
2139 : 59 : case STD_UNCAUGHT_EXCEPTIONS:
2140 : 59 : if (nargs != 0)
2141 : 0 : goto invalid_nargs;
2142 : : /* Similarly to __builtin_is_constant_evaluated (), we don't
2143 : : want to give a definite answer during mce_unknown evaluation,
2144 : : because that might prevent evaluation later on when some
2145 : : exceptions might be uncaught. But unlike that, we don't
2146 : : want to constant fold it even during cp_fold, because at runtime
2147 : : std::uncaught_exceptions () might still be non-zero. */
2148 : 59 : if (ctx->manifestly_const_eval != mce_true)
2149 : : {
2150 : 38 : *non_constant_p = true;
2151 : 38 : return call;
2152 : : }
2153 : 21 : return build_int_cst (integer_type_node,
2154 : 21 : ctx->global->uncaught_exceptions);
2155 : 7818 : case STD_CURRENT_EXCEPTION:
2156 : 7818 : if (nargs != 0)
2157 : 0 : goto invalid_nargs;
2158 : : else
2159 : : {
2160 : 7818 : tree name = get_identifier ("exception_ptr");
2161 : 7818 : tree decl = lookup_qualified_name (std_node, name);
2162 : 7818 : tree fld;
2163 : 7818 : if (TREE_CODE (decl) != TYPE_DECL
2164 : 7818 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2165 : 7818 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2166 : 7818 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2167 : 7818 : || DECL_ARTIFICIAL (fld)
2168 : 7818 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2169 : 7818 : || next_aggregate_field (DECL_CHAIN (fld))
2170 : 15636 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2171 : 7818 : TYPE_SIZE (TREE_TYPE (fld))))
2172 : : {
2173 : 0 : if (!ctx->quiet)
2174 : 0 : error_at (loc, "%qD called without supportable %qs",
2175 : : fndecl, "std::exception_ptr");
2176 : 0 : *non_constant_p = true;
2177 : 0 : return call;
2178 : : }
2179 : 15636 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2180 : 21 : if (arg == NULL_TREE || !VAR_P (arg))
2181 : 0 : --idx;
2182 : : else
2183 : : break;
2184 : : /* Similarly to __builtin_is_constant_evaluated (), we don't
2185 : : want to give a definite answer during mce_unknown evaluation,
2186 : : because that might prevent evaluation later on when some
2187 : : exceptions might be current. But unlike that, we don't
2188 : : want to constant fold it to null even during cp_fold, because
2189 : : at runtime std::current_exception () might still be non-null. */
2190 : 7818 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2191 : : {
2192 : 7786 : *non_constant_p = true;
2193 : 7786 : return call;
2194 : : }
2195 : 32 : if (arg == NULL_TREE)
2196 : 11 : arg = build_zero_cst (TREE_TYPE (fld));
2197 : : else
2198 : : {
2199 : 21 : DECL_EXCEPTION_REFCOUNT (arg)
2200 : 21 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2201 : : size_one_node);
2202 : 21 : arg = fold_convert (ptr_type_node, build_address (arg));
2203 : : }
2204 : 32 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2205 : : }
2206 : 22 : case STD_RETHROW_EXCEPTION:
2207 : 22 : if (nargs != 1)
2208 : 0 : goto invalid_nargs;
2209 : 22 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2210 : : {
2211 : 22 : arg = args[0];
2212 : 22 : STRIP_NOPS (arg);
2213 : 22 : if (TREE_CODE (arg) == ADDR_EXPR)
2214 : : {
2215 : 22 : args[0]
2216 : 22 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2217 : : vc_prvalue, non_constant_p,
2218 : : overflow_p, jump_target);
2219 : 22 : if (*non_constant_p)
2220 : : return call;
2221 : 22 : if (*jump_target)
2222 : : return NULL_TREE;
2223 : : }
2224 : : }
2225 : 22 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2226 : 22 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2227 : : {
2228 : 0 : invalid_std_rethrow:
2229 : 0 : if (!ctx->quiet)
2230 : 0 : error_at (loc, "%qD called with unexpected %qs argument",
2231 : : fndecl, "std::exception_ptr");
2232 : 0 : *non_constant_p = true;
2233 : 0 : return void_node;
2234 : : }
2235 : 22 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2236 : 22 : if (arg == NULL_TREE)
2237 : 0 : goto invalid_std_rethrow;
2238 : 22 : DECL_EXCEPTION_REFCOUNT (arg)
2239 : 22 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2240 : 22 : ++ctx->global->uncaught_exceptions;
2241 : 22 : *jump_target = arg;
2242 : 22 : return void_node;
2243 : 71 : case BUILTIN_EH_PTR_ADJUST_REF:
2244 : 71 : if (nargs != 2)
2245 : 0 : goto invalid_nargs;
2246 : 71 : arg = cxa_check_throw_arg (args[0], false);
2247 : 71 : if (arg == NULL_TREE)
2248 : 0 : goto invalid_ptr;
2249 : 71 : if (integer_onep (args[1]))
2250 : 28 : DECL_EXCEPTION_REFCOUNT (arg)
2251 : 56 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2252 : : size_one_node);
2253 : 43 : else if (integer_minus_onep (args[1]))
2254 : 43 : goto free_except;
2255 : : else
2256 : : {
2257 : 0 : if (!ctx->quiet)
2258 : 0 : error_at (loc, "%qD called with second argument "
2259 : : "other than 1 or -1", fndecl);
2260 : 0 : *non_constant_p = true;
2261 : : }
2262 : 28 : return void_node;
2263 : 0 : default:
2264 : 0 : gcc_unreachable ();
2265 : : }
2266 : : }
2267 : :
2268 : : /* Attempt to evaluate T which represents a call to a builtin function.
2269 : : We assume here that all builtin functions evaluate to scalar types
2270 : : represented by _CST nodes. */
2271 : :
2272 : : static tree
2273 : 11192823 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2274 : : value_cat lval,
2275 : : bool *non_constant_p, bool *overflow_p,
2276 : : tree *jump_target)
2277 : : {
2278 : 11192823 : const int nargs = call_expr_nargs (t);
2279 : 11192823 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2280 : 11192823 : tree new_call;
2281 : 11192823 : int i;
2282 : :
2283 : : /* Don't fold __builtin_constant_p within a constexpr function. */
2284 : 11192823 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2285 : :
2286 : : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2287 : : in a constexpr function until we have values for the parameters. */
2288 : 796886 : if (bi_const_p
2289 : 796886 : && ctx->manifestly_const_eval != mce_true
2290 : 783021 : && current_function_decl
2291 : 780847 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2292 : : {
2293 : 479117 : *non_constant_p = true;
2294 : 479117 : return t;
2295 : : }
2296 : :
2297 : : /* For __builtin_is_constant_evaluated, defer it if not
2298 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2299 : : without manifestly_const_eval even expressions or parts thereof which
2300 : : will later be manifestly const_eval evaluated), otherwise fold it to
2301 : : true. */
2302 : 10713706 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
2303 : : BUILT_IN_FRONTEND))
2304 : : {
2305 : 1980056 : if (ctx->manifestly_const_eval == mce_unknown)
2306 : : {
2307 : 1963308 : *non_constant_p = true;
2308 : 1963308 : return t;
2309 : : }
2310 : 16748 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2311 : 16748 : boolean_type_node);
2312 : : }
2313 : :
2314 : 8733650 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
2315 : : {
2316 : 436 : temp_override<tree> ovr (current_function_decl);
2317 : 436 : if (ctx->call && ctx->call->fundef)
2318 : 137 : current_function_decl = ctx->call->fundef->decl;
2319 : 436 : return fold_builtin_source_location (t);
2320 : 436 : }
2321 : :
2322 : 8733214 : if (fndecl_built_in_p (fun, CP_BUILT_IN_EH_PTR_ADJUST_REF,
2323 : : BUILT_IN_FRONTEND))
2324 : 71 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2325 : : fun, non_constant_p, overflow_p,
2326 : 71 : jump_target);
2327 : :
2328 : 8733143 : int strops = 0;
2329 : 8733143 : int strret = 0;
2330 : 8733143 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2331 : 8033385 : switch (DECL_FUNCTION_CODE (fun))
2332 : : {
2333 : : case BUILT_IN_STRLEN:
2334 : : case BUILT_IN_STRNLEN:
2335 : 8733045 : strops = 1;
2336 : : break;
2337 : 111760 : case BUILT_IN_MEMCHR:
2338 : 111760 : case BUILT_IN_STRCHR:
2339 : 111760 : case BUILT_IN_STRRCHR:
2340 : 111760 : strops = 1;
2341 : 111760 : strret = 1;
2342 : 111760 : break;
2343 : 50206 : case BUILT_IN_MEMCMP:
2344 : 50206 : case BUILT_IN_STRCMP:
2345 : 50206 : strops = 2;
2346 : 50206 : break;
2347 : 30894 : case BUILT_IN_STRSTR:
2348 : 30894 : strops = 2;
2349 : 30894 : strret = 1;
2350 : 30894 : break;
2351 : 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2352 : 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2353 : : /* These builtins shall be ignored during constant expression
2354 : : evaluation. */
2355 : 42 : return void_node;
2356 : 56 : case BUILT_IN_UNREACHABLE:
2357 : 56 : case BUILT_IN_TRAP:
2358 : 56 : if (!*non_constant_p && !ctx->quiet)
2359 : : {
2360 : : /* Do not allow__builtin_unreachable in constexpr function.
2361 : : The __builtin_unreachable call with BUILTINS_LOCATION
2362 : : comes from cp_maybe_instrument_return. */
2363 : 13 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2364 : 0 : error ("%<constexpr%> call flows off the end of the function");
2365 : : else
2366 : 13 : error ("%q+E is not a constant expression", t);
2367 : : }
2368 : 56 : *non_constant_p = true;
2369 : 56 : return t;
2370 : : default:
2371 : : break;
2372 : : }
2373 : :
2374 : : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2375 : : return constant false for a non-constant argument. */
2376 : 8733045 : constexpr_ctx new_ctx = *ctx;
2377 : 8733045 : new_ctx.quiet = true;
2378 : 23669703 : for (i = 0; i < nargs; ++i)
2379 : : {
2380 : 14936659 : tree arg = CALL_EXPR_ARG (t, i);
2381 : 14936659 : tree oarg = arg;
2382 : :
2383 : : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2384 : : expand_builtin doesn't know how to look in the values table. */
2385 : 14936659 : bool strop = i < strops;
2386 : 14936659 : if (strop)
2387 : : {
2388 : 368553 : STRIP_NOPS (arg);
2389 : 368553 : if (TREE_CODE (arg) == ADDR_EXPR)
2390 : 7812 : arg = TREE_OPERAND (arg, 0);
2391 : : else
2392 : : strop = false;
2393 : : }
2394 : :
2395 : : /* If builtin_valid_in_constant_expr_p is true,
2396 : : potential_constant_expression_1 has not recursed into the arguments
2397 : : of the builtin, verify it here. */
2398 : 14936659 : if (!builtin_valid_in_constant_expr_p (fun)
2399 : 14936659 : || potential_constant_expression (arg))
2400 : : {
2401 : 14936528 : bool dummy1 = false, dummy2 = false;
2402 : 14936528 : tree jmp_target = NULL_TREE;
2403 : 14936528 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2404 : : &dummy1, &dummy2, &jmp_target);
2405 : 14936527 : if (jmp_target)
2406 : : {
2407 : 0 : *jump_target = jmp_target;
2408 : 0 : return NULL_TREE;
2409 : : }
2410 : : }
2411 : :
2412 : 14936658 : if (bi_const_p)
2413 : : /* For __builtin_constant_p, fold all expressions with constant values
2414 : : even if they aren't C++ constant-expressions. */
2415 : 317768 : arg = cp_fold_rvalue (arg);
2416 : 14618890 : else if (strop)
2417 : : {
2418 : 7812 : if (TREE_CODE (arg) == CONSTRUCTOR)
2419 : 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2420 : 7812 : if (TREE_CODE (arg) == STRING_CST)
2421 : 2897 : arg = build_address (arg);
2422 : : else
2423 : : arg = oarg;
2424 : : }
2425 : :
2426 : 14936658 : args[i] = arg;
2427 : : }
2428 : :
2429 : 8733044 : bool save_ffbcp = force_folding_builtin_constant_p;
2430 : 8733044 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2431 : 8733044 : tree save_cur_fn = current_function_decl;
2432 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2433 : 8733044 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2434 : 38 : && ctx->call
2435 : 8733048 : && ctx->call->fundef)
2436 : 4 : current_function_decl = ctx->call->fundef->decl;
2437 : 8733044 : if (fndecl_built_in_p (fun,
2438 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2439 : : BUILT_IN_FRONTEND))
2440 : : {
2441 : 348 : location_t loc = EXPR_LOCATION (t);
2442 : 348 : if (nargs >= 1)
2443 : 345 : VERIFY_CONSTANT (args[0]);
2444 : 143 : new_call
2445 : 143 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2446 : : args);
2447 : : }
2448 : 8732696 : else if (fndecl_built_in_p (fun,
2449 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2450 : : BUILT_IN_FRONTEND))
2451 : : {
2452 : 459 : location_t loc = EXPR_LOCATION (t);
2453 : 459 : if (nargs >= 2)
2454 : : {
2455 : 453 : VERIFY_CONSTANT (args[0]);
2456 : 231 : VERIFY_CONSTANT (args[1]);
2457 : : }
2458 : 237 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2459 : : }
2460 : : else
2461 : 17464474 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2462 : 8732237 : CALL_EXPR_FN (t), nargs, args);
2463 : 8732617 : current_function_decl = save_cur_fn;
2464 : 8732617 : force_folding_builtin_constant_p = save_ffbcp;
2465 : 8732617 : if (new_call == NULL)
2466 : : {
2467 : 6438158 : if (!*non_constant_p && !ctx->quiet)
2468 : : {
2469 : 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2470 : 0 : CALL_EXPR_FN (t), nargs, args);
2471 : 0 : error ("%q+E is not a constant expression", new_call);
2472 : : }
2473 : 6438158 : *non_constant_p = true;
2474 : 6438158 : return t;
2475 : : }
2476 : :
2477 : 2294459 : if (!potential_constant_expression (new_call))
2478 : : {
2479 : 581 : if (!*non_constant_p && !ctx->quiet)
2480 : 4 : error ("%q+E is not a constant expression", new_call);
2481 : 581 : *non_constant_p = true;
2482 : 581 : return t;
2483 : : }
2484 : :
2485 : 2293878 : if (strret)
2486 : : {
2487 : : /* memchr returns a pointer into the first argument, but we replaced the
2488 : : argument above with a STRING_CST; put it back it now. */
2489 : 117 : tree op = CALL_EXPR_ARG (t, strret-1);
2490 : 117 : STRIP_NOPS (new_call);
2491 : 117 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2492 : 59 : TREE_OPERAND (new_call, 0) = op;
2493 : 58 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2494 : 2293878 : new_call = op;
2495 : : }
2496 : :
2497 : 2293878 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2498 : : non_constant_p, overflow_p,
2499 : 2293878 : jump_target);
2500 : : }
2501 : :
2502 : : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2503 : : the type of the value to match. */
2504 : :
2505 : : static tree
2506 : 49091335 : adjust_temp_type (tree type, tree temp)
2507 : : {
2508 : 49091335 : if (same_type_p (TREE_TYPE (temp), type))
2509 : : return temp;
2510 : : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2511 : 24452679 : if (TREE_CODE (temp) == CONSTRUCTOR)
2512 : : {
2513 : : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2514 : 2770647 : tree t = copy_node (temp);
2515 : 2770647 : TREE_TYPE (t) = type;
2516 : 2770647 : return t;
2517 : : }
2518 : 21682032 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2519 : 0 : return build0 (EMPTY_CLASS_EXPR, type);
2520 : 21682032 : gcc_assert (scalarish_type_p (type));
2521 : : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2522 : : type is cv-unqualified. */
2523 : 21682032 : return cp_fold_convert (cv_unqualified (type), temp);
2524 : : }
2525 : :
2526 : : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2527 : : sub-CONSTRUCTORs. Otherwise return T.
2528 : :
2529 : : We use this whenever we initialize an object as a whole, whether it's a
2530 : : parameter, a local variable, or a subobject, so that subsequent
2531 : : modifications don't affect other places where it was used. */
2532 : :
2533 : : tree
2534 : 36892403 : unshare_constructor (tree t MEM_STAT_DECL)
2535 : : {
2536 : 36892403 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2537 : : return t;
2538 : 8299300 : auto_vec <tree*, 4> ptrs;
2539 : 8299300 : ptrs.safe_push (&t);
2540 : 8299300 : while (!ptrs.is_empty ())
2541 : : {
2542 : 9787561 : tree *p = ptrs.pop ();
2543 : 9787561 : tree n = copy_node (*p PASS_MEM_STAT);
2544 : 14780205 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2545 : 9787561 : *p = n;
2546 : 9787561 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2547 : 9787561 : constructor_elt *ce;
2548 : 37928319 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2549 : 10053897 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2550 : 1488261 : ptrs.safe_push (&ce->value);
2551 : : }
2552 : 8299300 : return t;
2553 : 8299300 : }
2554 : :
2555 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2556 : :
2557 : : static void
2558 : 762163 : free_constructor (tree t)
2559 : : {
2560 : 762163 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2561 : 0 : return;
2562 : 762163 : releasing_vec ctors;
2563 : 762163 : vec_safe_push (ctors, t);
2564 : 1568158 : while (!ctors->is_empty ())
2565 : : {
2566 : 805995 : tree c = ctors->pop ();
2567 : 805995 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2568 : : {
2569 : : constructor_elt *ce;
2570 : 253985 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2571 : 168265 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2572 : 43832 : vec_safe_push (ctors, ce->value);
2573 : 85720 : ggc_free (elts);
2574 : : }
2575 : 805995 : ggc_free (c);
2576 : : }
2577 : 762163 : }
2578 : :
2579 : : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2580 : : if *TP is address of a static variable (or part of it) currently being
2581 : : constructed or of a heap artificial variable. */
2582 : :
2583 : : static tree
2584 : 13022048 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2585 : : {
2586 : 13022048 : if (TREE_CODE (*tp) == ADDR_EXPR)
2587 : 1953320 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2588 : 1953320 : if (VAR_P (var) && TREE_STATIC (var))
2589 : : {
2590 : 897780 : if (DECL_NAME (var) == heap_uninit_identifier
2591 : 897780 : || DECL_NAME (var) == heap_identifier
2592 : 897780 : || DECL_NAME (var) == heap_vec_uninit_identifier
2593 : 1795560 : || DECL_NAME (var) == heap_vec_identifier)
2594 : : return var;
2595 : :
2596 : 897780 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2597 : 897780 : if (global->get_value (var))
2598 : : return var;
2599 : : }
2600 : 12895667 : if (TYPE_P (*tp))
2601 : 1179 : *walk_subtrees = false;
2602 : : return NULL_TREE;
2603 : : }
2604 : :
2605 : : /* Subroutine of cxx_eval_call_expression.
2606 : : We are processing a call expression (either CALL_EXPR or
2607 : : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2608 : : all arguments and bind their values to correspondings
2609 : : parameters, making up the NEW_CALL context. */
2610 : :
2611 : : static tree
2612 : 77380463 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2613 : : tree orig_fun, bool *non_constant_p,
2614 : : bool *overflow_p, bool *non_constant_args,
2615 : : tree *jump_target)
2616 : : {
2617 : 77380463 : int nargs = call_expr_nargs (t);
2618 : 77380463 : tree parms = DECL_ARGUMENTS (fun);
2619 : 77380463 : int i, j = 0;
2620 : 77380463 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2621 : 1233 : ++nargs;
2622 : 77380463 : if (DECL_HAS_VTT_PARM_P (fun)
2623 : 1235 : && fun != orig_fun
2624 : 77381696 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2625 : 900 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2626 : 344 : ++nargs;
2627 : : /* We don't record ellipsis args below. */
2628 : 77380463 : int nparms = list_length (parms);
2629 : 77380463 : int nbinds = nargs < nparms ? nargs : nparms;
2630 : 77380463 : tree binds = make_tree_vec (nbinds);
2631 : :
2632 : : /* The call is not a constant expression if it involves the cdtor for a type
2633 : : with virtual bases before C++26. */
2634 : 77380463 : if (cxx_dialect < cxx26
2635 : 77380463 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2636 : : {
2637 : 17 : if (!ctx->quiet)
2638 : : {
2639 : 3 : error_at (cp_expr_loc_or_input_loc (t),
2640 : : "call to non-%<constexpr%> function %qD", fun);
2641 : 3 : explain_invalid_constexpr_fn (fun);
2642 : : }
2643 : 17 : *non_constant_p = true;
2644 : 17 : return binds;
2645 : : }
2646 : :
2647 : 121526344 : for (i = 0; i < nargs; ++i)
2648 : : {
2649 : 78017921 : tree x, arg;
2650 : 78017921 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2651 : 78017921 : if (parms && DECL_BY_REFERENCE (parms))
2652 : 6977 : type = TREE_TYPE (type);
2653 : 78017921 : if (i == 1
2654 : 78017921 : && j == 0
2655 : 13211308 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2656 : 78019043 : && orig_fun != fun)
2657 : : {
2658 : 1122 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2659 : 1122 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2660 : 324 : x = boolean_true_node;
2661 : : else
2662 : 798 : x = boolean_false_node;
2663 : : j = -1;
2664 : : }
2665 : 78016799 : else if (i == 2
2666 : 78016799 : && j == -1
2667 : 1122 : && DECL_HAS_VTT_PARM_P (fun)
2668 : 1122 : && orig_fun != fun
2669 : 78017921 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2670 : 809 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2671 : : {
2672 : 324 : x = build_zero_cst (type);
2673 : 324 : j = -2;
2674 : : }
2675 : : else
2676 : 78016475 : x = get_nth_callarg (t, i + j);
2677 : : /* For member function, the first argument is a pointer to the implied
2678 : : object. For a constructor, it might still be a dummy object, in
2679 : : which case we get the real argument from ctx. */
2680 : 124155116 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2681 : 87376291 : && is_dummy_object (x))
2682 : : {
2683 : 4144588 : x = ctx->object;
2684 : 4144588 : x = build_address (x);
2685 : : }
2686 : 78017921 : if (TREE_ADDRESSABLE (type))
2687 : : {
2688 : : /* Undo convert_for_arg_passing work here. */
2689 : 10706 : x = convert_from_reference (x);
2690 : 10706 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2691 : : non_constant_p, overflow_p,
2692 : : jump_target);
2693 : : }
2694 : : else
2695 : : /* Normally we would strip a TARGET_EXPR in an initialization context
2696 : : such as this, but here we do the elision differently: we keep the
2697 : : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2698 : 78007215 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2699 : : non_constant_p, overflow_p,
2700 : : jump_target);
2701 : 78017921 : if (*jump_target)
2702 : : break;
2703 : : /* Check we aren't dereferencing a null pointer when calling a non-static
2704 : : member function, which is undefined behaviour. */
2705 : 62077553 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2706 : 33770013 : && integer_zerop (arg)
2707 : : /* But ignore calls from within compiler-generated code, to handle
2708 : : cases like lambda function pointer conversion operator thunks
2709 : : which pass NULL as the 'this' pointer. */
2710 : 78063991 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2711 : : {
2712 : 292 : if (!ctx->quiet)
2713 : 6 : error_at (cp_expr_loc_or_input_loc (x),
2714 : : "dereferencing a null pointer");
2715 : 292 : *non_constant_p = true;
2716 : : }
2717 : : /* Don't VERIFY_CONSTANT here. */
2718 : 78017912 : if (*non_constant_p && ctx->quiet)
2719 : : break;
2720 : : /* Just discard ellipsis args after checking their constantitude. */
2721 : 44145898 : if (!parms)
2722 : 405 : continue;
2723 : :
2724 : 44145493 : if (!*non_constant_p)
2725 : : {
2726 : : /* Make sure the binding has the same type as the parm. But
2727 : : only for constant args. */
2728 : 44144713 : if (TREE_ADDRESSABLE (type))
2729 : : {
2730 : 7499 : if (!same_type_p (type, TREE_TYPE (arg)))
2731 : : {
2732 : 9 : arg = build_fold_addr_expr (arg);
2733 : 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2734 : 9 : arg = convert_from_reference (arg);
2735 : : }
2736 : : }
2737 : 44137214 : else if (!TYPE_REF_P (type))
2738 : 33483304 : arg = adjust_temp_type (type, arg);
2739 : 44144713 : if (!TREE_CONSTANT (arg))
2740 : 31432402 : *non_constant_args = true;
2741 : 12712311 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2742 : : /* The destructor needs to see any modifications the callee makes
2743 : : to the argument. */
2744 : 0 : *non_constant_args = true;
2745 : : /* If arg is or contains address of a heap artificial variable or
2746 : : of a static variable being constructed, avoid caching the
2747 : : function call, as those variables might be modified by the
2748 : : function, or might be modified by the callers in between
2749 : : the cached function and just read by the function. */
2750 : 12712311 : else if (!*non_constant_args
2751 : 12712311 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2752 : : NULL))
2753 : 126381 : *non_constant_args = true;
2754 : :
2755 : : /* For virtual calls, adjust the this argument, so that it is
2756 : : the object on which the method is called, rather than
2757 : : one of its bases. */
2758 : 44144713 : if (i == 0 && DECL_VIRTUAL_P (fun))
2759 : : {
2760 : 16540 : tree addr = arg;
2761 : 16540 : STRIP_NOPS (addr);
2762 : 16540 : if (TREE_CODE (addr) == ADDR_EXPR)
2763 : : {
2764 : 16520 : tree obj = TREE_OPERAND (addr, 0);
2765 : 16520 : while (TREE_CODE (obj) == COMPONENT_REF
2766 : 7662 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
2767 : 24185 : && !same_type_ignoring_top_level_qualifiers_p
2768 : 7611 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
2769 : 54 : obj = TREE_OPERAND (obj, 0);
2770 : 16520 : if (obj != TREE_OPERAND (addr, 0))
2771 : 51 : arg = build_fold_addr_expr_with_type (obj,
2772 : : TREE_TYPE (arg));
2773 : : }
2774 : : }
2775 : 44144713 : TREE_VEC_ELT (binds, i) = arg;
2776 : : }
2777 : 44145493 : parms = TREE_CHAIN (parms);
2778 : : }
2779 : :
2780 : : return binds;
2781 : : }
2782 : :
2783 : : /* Variables and functions to manage constexpr call expansion context.
2784 : : These do not need to be marked for PCH or GC. */
2785 : :
2786 : : /* FIXME remember and print actual constant arguments. */
2787 : : static vec<tree> call_stack;
2788 : : static int call_stack_tick;
2789 : : static int last_cx_error_tick;
2790 : :
2791 : : static int
2792 : 43206248 : push_cx_call_context (tree call)
2793 : : {
2794 : 43206248 : ++call_stack_tick;
2795 : 43206248 : if (!EXPR_HAS_LOCATION (call))
2796 : 20390 : SET_EXPR_LOCATION (call, input_location);
2797 : 43206248 : call_stack.safe_push (call);
2798 : 43206248 : int len = call_stack.length ();
2799 : 43206248 : if (len > max_constexpr_depth)
2800 : 30 : return false;
2801 : : return len;
2802 : : }
2803 : :
2804 : : static void
2805 : 43206246 : pop_cx_call_context (void)
2806 : : {
2807 : 43206246 : ++call_stack_tick;
2808 : 43206246 : call_stack.pop ();
2809 : 43206246 : }
2810 : :
2811 : : vec<tree>
2812 : 223074 : cx_error_context (void)
2813 : : {
2814 : 223074 : vec<tree> r = vNULL;
2815 : 223074 : if (call_stack_tick != last_cx_error_tick
2816 : 223074 : && !call_stack.is_empty ())
2817 : : r = call_stack;
2818 : 223074 : last_cx_error_tick = call_stack_tick;
2819 : 223074 : return r;
2820 : : }
2821 : :
2822 : : /* E is an operand of a failed assertion, fold it either with or without
2823 : : constexpr context. */
2824 : :
2825 : : static tree
2826 : 582 : fold_operand (tree e, const constexpr_ctx *ctx)
2827 : : {
2828 : 582 : if (ctx)
2829 : : {
2830 : 70 : bool new_non_constant_p = false, new_overflow_p = false;
2831 : 70 : tree jmp_target = NULL_TREE;
2832 : 70 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
2833 : : &new_non_constant_p,
2834 : : &new_overflow_p, &jmp_target);
2835 : : }
2836 : : else
2837 : 512 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
2838 : 582 : return e;
2839 : : }
2840 : :
2841 : : /* If we have a condition in conjunctive normal form (CNF), find the first
2842 : : failing clause. In other words, given an expression like
2843 : :
2844 : : true && true && false && true && false
2845 : :
2846 : : return the first 'false'. EXPR is the expression. */
2847 : :
2848 : : static tree
2849 : 380 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
2850 : : {
2851 : 490 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2852 : : {
2853 : : /* First check the left side... */
2854 : 216 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
2855 : 216 : if (e == NULL_TREE)
2856 : : /* ...if we didn't find a false clause, check the right side. */
2857 : 110 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
2858 : : return e;
2859 : : }
2860 : 274 : tree e = contextual_conv_bool (expr, tf_none);
2861 : 274 : e = fold_operand (e, ctx);
2862 : 274 : if (integer_zerop (e))
2863 : : /* This is the failing clause. */
2864 : 164 : return expr;
2865 : : return NULL_TREE;
2866 : : }
2867 : :
2868 : : /* Wrapper for find_failing_clause_r. */
2869 : :
2870 : : tree
2871 : 1646 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
2872 : : {
2873 : 1646 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2874 : 164 : if (tree e = find_failing_clause_r (ctx, expr))
2875 : 1646 : expr = e;
2876 : 1646 : return expr;
2877 : : }
2878 : :
2879 : : /* Emit additional diagnostics for failing condition BAD.
2880 : : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
2881 : : If SHOW_EXPR_P is true, print the condition (because it was
2882 : : instantiation-dependent). */
2883 : :
2884 : : void
2885 : 1646 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
2886 : : const constexpr_ctx *ctx /* = nullptr */)
2887 : : {
2888 : : /* Nobody wants to see the artificial (bool) cast. */
2889 : 1646 : bad = tree_strip_nop_conversions (bad);
2890 : 1646 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
2891 : 4 : bad = TREE_OPERAND (bad, 0);
2892 : :
2893 : 1646 : auto_diagnostic_nesting_level sentinel;
2894 : :
2895 : : /* Actually explain the failure if this is a concept check or a
2896 : : requires-expression. */
2897 : 1646 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
2898 : 301 : diagnose_constraints (cloc, bad, NULL_TREE);
2899 : : /* Similarly if this is a standard trait. */
2900 : 1345 : else if (maybe_diagnose_standard_trait (cloc, bad))
2901 : : ;
2902 : 1066 : else if (COMPARISON_CLASS_P (bad)
2903 : 1066 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
2904 : : {
2905 : 154 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
2906 : 154 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
2907 : 154 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
2908 : 154 : inform (cloc, "the comparison reduces to %qE", cond);
2909 : : }
2910 : 912 : else if (show_expr_p)
2911 : 675 : inform (cloc, "%qE evaluates to false", bad);
2912 : 1646 : }
2913 : :
2914 : : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
2915 : : do it without changing the current evaluation state. If it evaluates to
2916 : : false, complain and return false; otherwise, return true. */
2917 : :
2918 : : static bool
2919 : 283680 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
2920 : : location_t loc, bool evaluated,
2921 : : bool *non_constant_p, bool *overflow_p)
2922 : : {
2923 : 283680 : if (*non_constant_p)
2924 : : return true;
2925 : :
2926 : 283680 : tree eval, jmp_target = NULL_TREE;
2927 : 283680 : if (!evaluated)
2928 : : {
2929 : 283609 : if (!potential_rvalue_constant_expression (arg))
2930 : 19 : return true;
2931 : :
2932 : 283590 : constexpr_ctx new_ctx = *ctx;
2933 : 283590 : new_ctx.quiet = true;
2934 : 283590 : bool new_non_constant_p = false, new_overflow_p = false;
2935 : : /* Avoid modification of existing values. */
2936 : 283590 : modifiable_tracker ms (new_ctx.global);
2937 : 283590 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2938 : : &new_non_constant_p,
2939 : : &new_overflow_p, &jmp_target);
2940 : 283590 : }
2941 : : else
2942 : 71 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2943 : : non_constant_p,
2944 : : overflow_p, &jmp_target);
2945 : 283661 : if (jmp_target)
2946 : : return true;
2947 : :
2948 : 283661 : if (!*non_constant_p && integer_zerop (eval))
2949 : : {
2950 : 99 : if (!ctx->quiet)
2951 : : {
2952 : : /* See if we can find which clause was failing
2953 : : (for logical AND). */
2954 : 32 : tree bad = find_failing_clause (ctx, arg);
2955 : : /* If not, or its location is unusable, fall back to the
2956 : : previous location. */
2957 : 32 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
2958 : :
2959 : : /* Report the error. */
2960 : 32 : auto_diagnostic_group d;
2961 : 32 : error_at (cloc, msg);
2962 : 32 : diagnose_failing_condition (bad, cloc, true, ctx);
2963 : 32 : return bad;
2964 : 32 : }
2965 : 67 : *non_constant_p = true;
2966 : 67 : return false;
2967 : : }
2968 : :
2969 : : return true;
2970 : : }
2971 : :
2972 : : /* Evaluate a call T to a GCC internal function when possible and return
2973 : : the evaluated result or, under the control of CTX, give an error, set
2974 : : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2975 : :
2976 : : static tree
2977 : 332932 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2978 : : value_cat lval,
2979 : : bool *non_constant_p, bool *overflow_p,
2980 : : tree *jump_target)
2981 : : {
2982 : 332932 : enum tree_code opcode = ERROR_MARK;
2983 : :
2984 : 332932 : switch (CALL_EXPR_IFN (t))
2985 : : {
2986 : 163 : case IFN_UBSAN_NULL:
2987 : 163 : case IFN_UBSAN_BOUNDS:
2988 : 163 : case IFN_UBSAN_VPTR:
2989 : 163 : case IFN_FALLTHROUGH:
2990 : 163 : return void_node;
2991 : :
2992 : 283593 : case IFN_ASSUME:
2993 : 283593 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2994 : : G_("failed %<assume%> attribute assumption"),
2995 : 283593 : EXPR_LOCATION (t), /*eval*/false,
2996 : : non_constant_p, overflow_p))
2997 : : return t;
2998 : 283564 : return void_node;
2999 : :
3000 : : case IFN_ADD_OVERFLOW:
3001 : : opcode = PLUS_EXPR;
3002 : : break;
3003 : 339 : case IFN_SUB_OVERFLOW:
3004 : 339 : opcode = MINUS_EXPR;
3005 : 339 : break;
3006 : 48279 : case IFN_MUL_OVERFLOW:
3007 : 48279 : opcode = MULT_EXPR;
3008 : 48279 : break;
3009 : :
3010 : 74 : case IFN_LAUNDER:
3011 : 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3012 : : vc_prvalue, non_constant_p,
3013 : 74 : overflow_p, jump_target);
3014 : :
3015 : 0 : case IFN_DEFERRED_INIT:
3016 : 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3017 : :
3018 : 30 : case IFN_VEC_CONVERT:
3019 : 30 : {
3020 : 30 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3021 : : vc_prvalue, non_constant_p,
3022 : : overflow_p, jump_target);
3023 : 30 : if (*jump_target)
3024 : : return NULL_TREE;
3025 : 30 : if (TREE_CODE (arg) == VECTOR_CST)
3026 : 19 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3027 : : return r;
3028 : : }
3029 : : /* FALLTHRU */
3030 : :
3031 : 16 : default:
3032 : 16 : if (!ctx->quiet)
3033 : 0 : error_at (cp_expr_loc_or_input_loc (t),
3034 : : "call to internal function %qE", t);
3035 : 16 : *non_constant_p = true;
3036 : 16 : return t;
3037 : : }
3038 : :
3039 : : /* Evaluate constant arguments using OPCODE and return a complex
3040 : : number containing the result and the overflow bit. */
3041 : 49072 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3042 : : non_constant_p, overflow_p,
3043 : : jump_target);
3044 : 49072 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3045 : : non_constant_p, overflow_p,
3046 : : jump_target);
3047 : 49072 : if (*jump_target)
3048 : : return NULL_TREE;
3049 : 49072 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3050 : : {
3051 : 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3052 : 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3053 : 2 : tree result = fold_binary_loc (loc, opcode, type,
3054 : : fold_convert_loc (loc, type, arg0),
3055 : : fold_convert_loc (loc, type, arg1));
3056 : 2 : tree ovf
3057 : 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3058 : : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3059 : 2 : if (TREE_OVERFLOW (result))
3060 : 0 : TREE_OVERFLOW (result) = 0;
3061 : :
3062 : 2 : return build_complex (TREE_TYPE (t), result, ovf);
3063 : : }
3064 : :
3065 : 49070 : *non_constant_p = true;
3066 : 49070 : return t;
3067 : : }
3068 : :
3069 : : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3070 : :
3071 : : static void
3072 : 4283067 : clear_no_implicit_zero (tree ctor)
3073 : : {
3074 : 4283067 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3075 : : {
3076 : 1151872 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3077 : 5337976 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3078 : 1906462 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3079 : 329365 : clear_no_implicit_zero (e.value);
3080 : : }
3081 : 4283067 : }
3082 : :
3083 : : /* Complain about a const object OBJ being modified in a constant expression.
3084 : : EXPR is the MODIFY_EXPR expression performing the modification. */
3085 : :
3086 : : static void
3087 : 63 : modifying_const_object_error (tree expr, tree obj)
3088 : : {
3089 : 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3090 : 63 : auto_diagnostic_group d;
3091 : 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3092 : 63 : "a constant expression", TREE_OPERAND (expr, 0));
3093 : :
3094 : : /* Find the underlying object that was declared as const. */
3095 : 63 : location_t decl_loc = UNKNOWN_LOCATION;
3096 : 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3097 : 75 : switch (TREE_CODE (probe))
3098 : : {
3099 : 39 : case BIT_FIELD_REF:
3100 : 39 : case COMPONENT_REF:
3101 : 39 : {
3102 : 39 : tree elt = TREE_OPERAND (probe, 1);
3103 : 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3104 : 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3105 : 39 : probe = TREE_OPERAND (probe, 0);
3106 : : }
3107 : 39 : break;
3108 : :
3109 : 0 : case ARRAY_REF:
3110 : 0 : case REALPART_EXPR:
3111 : 0 : case IMAGPART_EXPR:
3112 : 0 : probe = TREE_OPERAND (probe, 0);
3113 : 0 : break;
3114 : :
3115 : 36 : default:
3116 : 36 : decl_loc = location_of (probe);
3117 : 36 : break;
3118 : : }
3119 : 63 : inform (decl_loc, "originally declared %<const%> here");
3120 : 63 : }
3121 : :
3122 : : /* Return true if FNDECL is a replaceable global allocation function that
3123 : : should be useable during constant expression evaluation. */
3124 : :
3125 : : static inline bool
3126 : 30988365 : cxx_replaceable_global_alloc_fn (tree fndecl)
3127 : : {
3128 : 30988365 : return (cxx_dialect >= cxx20
3129 : 9422260 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3130 : 361281 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3131 : 31349501 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3132 : 153866 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3133 : : }
3134 : :
3135 : : /* Return true if FNDECL is a placement new function that should be
3136 : : useable during constant expression evaluation of std::construct_at. */
3137 : :
3138 : : static inline bool
3139 : 30748749 : cxx_placement_new_fn (tree fndecl)
3140 : : {
3141 : 30748749 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3142 : : }
3143 : :
3144 : : /* Return true if FNDECL is std::construct_at. */
3145 : :
3146 : : static inline bool
3147 : 225282 : is_std_construct_at (tree fndecl)
3148 : : {
3149 : 225282 : if (!decl_in_std_namespace_p (fndecl))
3150 : : return false;
3151 : :
3152 : 215973 : tree name = DECL_NAME (fndecl);
3153 : 215973 : return name && id_equal (name, "construct_at");
3154 : : }
3155 : :
3156 : : /* Overload for the above taking constexpr_call*. */
3157 : :
3158 : : static inline bool
3159 : 202300 : is_std_construct_at (const constexpr_call *call)
3160 : : {
3161 : 202300 : return (call
3162 : 174316 : && call->fundef
3163 : 376616 : && is_std_construct_at (call->fundef->decl));
3164 : : }
3165 : :
3166 : : /* True if CTX is an instance of std::NAME class. */
3167 : :
3168 : : bool
3169 : 4257484 : is_std_class (tree ctx, const char *name)
3170 : : {
3171 : 4257484 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3172 : : return false;
3173 : :
3174 : 4256632 : tree decl = TYPE_MAIN_DECL (ctx);
3175 : 4256632 : tree dname = DECL_NAME (decl);
3176 : 4256632 : if (dname == NULL_TREE || !id_equal (dname, name))
3177 : : return false;
3178 : :
3179 : 200854 : return decl_in_std_namespace_p (decl);
3180 : : }
3181 : :
3182 : : /* True if CTX is an instance of std::allocator. */
3183 : :
3184 : : bool
3185 : 82158 : is_std_allocator (tree ctx)
3186 : : {
3187 : 82158 : return is_std_class (ctx, "allocator");
3188 : : }
3189 : :
3190 : : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3191 : :
3192 : : static inline bool
3193 : 91557 : is_std_allocator_allocate (tree fndecl)
3194 : : {
3195 : 91557 : tree name = DECL_NAME (fndecl);
3196 : 91557 : if (name == NULL_TREE
3197 : 91557 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3198 : : return false;
3199 : :
3200 : 78030 : return is_std_allocator (DECL_CONTEXT (fndecl));
3201 : : }
3202 : :
3203 : : /* Overload for the above taking constexpr_call*. */
3204 : :
3205 : : static inline bool
3206 : 52778 : is_std_allocator_allocate (const constexpr_call *call)
3207 : : {
3208 : 52778 : return (call
3209 : 20910 : && call->fundef
3210 : 73688 : && is_std_allocator_allocate (call->fundef->decl));
3211 : : }
3212 : :
3213 : : /* Return true if FNDECL is std::source_location::current. */
3214 : :
3215 : : static inline bool
3216 : 8592 : is_std_source_location_current (tree fndecl)
3217 : : {
3218 : 8592 : if (!decl_in_std_namespace_p (fndecl))
3219 : : return false;
3220 : :
3221 : 1256 : tree name = DECL_NAME (fndecl);
3222 : 1256 : if (name == NULL_TREE || !id_equal (name, "current"))
3223 : : return false;
3224 : :
3225 : 159 : tree ctx = DECL_CONTEXT (fndecl);
3226 : 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3227 : : return false;
3228 : :
3229 : 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3230 : 159 : return name && id_equal (name, "source_location");
3231 : : }
3232 : :
3233 : : /* Overload for the above taking constexpr_call*. */
3234 : :
3235 : : static inline bool
3236 : 20210 : is_std_source_location_current (const constexpr_call *call)
3237 : : {
3238 : 20210 : return (call
3239 : 8592 : && call->fundef
3240 : 28802 : && is_std_source_location_current (call->fundef->decl));
3241 : : }
3242 : :
3243 : : /* Return true if FNDECL is __dynamic_cast. */
3244 : :
3245 : : static inline bool
3246 : 30700399 : cxx_dynamic_cast_fn_p (tree fndecl)
3247 : : {
3248 : 30700399 : return (cxx_dialect >= cxx20
3249 : 9134271 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3250 : 5018 : && CP_DECL_CONTEXT (fndecl) == abi_node
3251 : : /* Only consider implementation-detail __dynamic_cast calls that
3252 : : correspond to a dynamic_cast, and ignore direct calls to
3253 : : abi::__dynamic_cast. */
3254 : 30705417 : && DECL_ARTIFICIAL (fndecl));
3255 : : }
3256 : :
3257 : : /* Often, we have an expression in the form of address + offset, e.g.
3258 : : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3259 : :
3260 : : static tree
3261 : 4478 : extract_obj_from_addr_offset (tree expr)
3262 : : {
3263 : 4478 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3264 : 2167 : expr = TREE_OPERAND (expr, 0);
3265 : 4478 : STRIP_NOPS (expr);
3266 : 4478 : if (TREE_CODE (expr) == ADDR_EXPR)
3267 : 4471 : expr = TREE_OPERAND (expr, 0);
3268 : 4478 : return expr;
3269 : : }
3270 : :
3271 : : /* Given a PATH like
3272 : :
3273 : : g.D.2181.D.2154.D.2102.D.2093
3274 : :
3275 : : find a component with type TYPE. Return NULL_TREE if not found, and
3276 : : error_mark_node if the component is not accessible. If STOP is non-null,
3277 : : this function will return NULL_TREE if STOP is found before TYPE. */
3278 : :
3279 : : static tree
3280 : 2212 : get_component_with_type (tree path, tree type, tree stop)
3281 : : {
3282 : 6398 : while (true)
3283 : : {
3284 : 4305 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3285 : : /* Found it. */
3286 : : return path;
3287 : 3142 : else if (stop
3288 : 3142 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3289 : : stop)))
3290 : : return NULL_TREE;
3291 : 3097 : else if (TREE_CODE (path) == COMPONENT_REF
3292 : 3097 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3293 : : {
3294 : : /* We need to check that the component we're accessing is in fact
3295 : : accessible. */
3296 : 3097 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3297 : 3097 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3298 : 1004 : return error_mark_node;
3299 : 2093 : path = TREE_OPERAND (path, 0);
3300 : : }
3301 : : else
3302 : : return NULL_TREE;
3303 : : }
3304 : : }
3305 : :
3306 : : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3307 : :
3308 : : The declaration of __dynamic_cast is:
3309 : :
3310 : : void* __dynamic_cast (const void* __src_ptr,
3311 : : const __class_type_info* __src_type,
3312 : : const __class_type_info* __dst_type,
3313 : : ptrdiff_t __src2dst);
3314 : :
3315 : : where src2dst has the following possible values
3316 : :
3317 : : >-1: src_type is a unique public non-virtual base of dst_type
3318 : : dst_ptr + src2dst == src_ptr
3319 : : -1: unspecified relationship
3320 : : -2: src_type is not a public base of dst_type
3321 : : -3: src_type is a multiple public non-virtual base of dst_type */
3322 : :
3323 : : static tree
3324 : 2402 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3325 : : bool *non_constant_p, bool *overflow_p,
3326 : : tree *jump_target)
3327 : : {
3328 : : /* T will be something like
3329 : : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3330 : : dismantle it. */
3331 : 2402 : gcc_assert (call_expr_nargs (call) == 4);
3332 : 2402 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3333 : 2402 : tree obj = CALL_EXPR_ARG (call, 0);
3334 : 2402 : tree type = CALL_EXPR_ARG (call, 2);
3335 : 2402 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3336 : 2402 : location_t loc = cp_expr_loc_or_input_loc (call);
3337 : :
3338 : : /* Get the target type of the dynamic_cast. */
3339 : 2402 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3340 : 2402 : type = TREE_OPERAND (type, 0);
3341 : 2402 : type = TREE_TYPE (DECL_NAME (type));
3342 : :
3343 : : /* TYPE can only be either T* or T&. We can't know which of these it
3344 : : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3345 : : and something like "(T*)(T&)(T*) x" in the second case.
3346 : : This is true for the reference cases in C++ < 26 or when exceptions
3347 : : aren't enabled, in that case we should diagnose errors. For C++26
3348 : : with exceptions we should silently evaluate to null pointer and
3349 : : let the callers call __cxa_bad_cast () later to throw an exception. */
3350 : 2402 : bool fail_for_non_constant_p = false;
3351 : 11086 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3352 : : {
3353 : 8684 : if (cxx_dialect < cxx26 || !flag_exceptions)
3354 : 5196 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3355 : 8684 : obj = TREE_OPERAND (obj, 0);
3356 : : }
3357 : :
3358 : : /* Evaluate the object so that we know its dynamic type. */
3359 : 2402 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3360 : : overflow_p, jump_target);
3361 : 2402 : if (*non_constant_p)
3362 : : return call;
3363 : 2311 : if (*jump_target)
3364 : : return NULL_TREE;
3365 : :
3366 : : /* For dynamic_cast from classes with virtual bases we can get something
3367 : : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3368 : : d.D.1234 using cxx_fold_indirect_ref. */
3369 : 2311 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3370 : : {
3371 : 609 : tree objo = obj;
3372 : 609 : STRIP_NOPS (objo);
3373 : 609 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3374 : : {
3375 : 576 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3376 : : obj, NULL, jump_target);
3377 : 576 : if (objo)
3378 : 576 : obj = build_fold_addr_expr (objo);
3379 : : }
3380 : : }
3381 : :
3382 : : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3383 : : but when HINT is > 0, it can also be something like
3384 : : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3385 : 2311 : obj = extract_obj_from_addr_offset (obj);
3386 : 2311 : const tree objtype = TREE_TYPE (obj);
3387 : : /* If OBJ doesn't refer to a base field, we're done. */
3388 : 4595 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3389 : 2311 : ? TREE_OPERAND (obj, 1) : obj))
3390 : 2311 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3391 : : {
3392 : 27 : if (fail_for_non_constant_p)
3393 : : {
3394 : 8 : if (!ctx->quiet)
3395 : : {
3396 : 2 : auto_diagnostic_group d;
3397 : 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3398 : 2 : inform (loc, "dynamic type %qT of its operand does "
3399 : : "not have a base class of type %qT",
3400 : : objtype, type);
3401 : 2 : }
3402 : 8 : *non_constant_p = true;
3403 : : }
3404 : 27 : return integer_zero_node;
3405 : : }
3406 : :
3407 : : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3408 : : or in a destructor ... if the operand of the dynamic_cast refers
3409 : : to the object under construction or destruction, this object is
3410 : : considered to be a most derived object that has the type of the
3411 : : constructor or destructor's class. */
3412 : 2284 : tree vtable = build_vfield_ref (obj, objtype);
3413 : 2284 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3414 : : non_constant_p, overflow_p,
3415 : : jump_target);
3416 : 2284 : if (*non_constant_p)
3417 : : return call;
3418 : 2179 : if (*jump_target)
3419 : : return NULL_TREE;
3420 : : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3421 : : so it's possible that we got a null pointer now. */
3422 : 2179 : if (integer_zerop (vtable))
3423 : : {
3424 : 12 : if (!ctx->quiet)
3425 : 3 : error_at (loc, "virtual table pointer is used uninitialized");
3426 : 12 : *non_constant_p = true;
3427 : 12 : return integer_zero_node;
3428 : : }
3429 : : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3430 : 2167 : vtable = extract_obj_from_addr_offset (vtable);
3431 : 2167 : const tree mdtype = DECL_CONTEXT (vtable);
3432 : :
3433 : : /* Given dynamic_cast<T>(v),
3434 : :
3435 : : [expr.dynamic.cast] If C is the class type to which T points or refers,
3436 : : the runtime check logically executes as follows:
3437 : :
3438 : : If, in the most derived object pointed (referred) to by v, v points
3439 : : (refers) to a public base class subobject of a C object, and if only
3440 : : one object of type C is derived from the subobject pointed (referred)
3441 : : to by v the result points (refers) to that C object.
3442 : :
3443 : : In this case, HINT >= 0 or -3. */
3444 : 2167 : if (hint >= 0 || hint == -3)
3445 : : {
3446 : : /* Look for a component with type TYPE. */
3447 : 657 : tree t = get_component_with_type (obj, type, mdtype);
3448 : : /* If not accessible, give an error. */
3449 : 657 : if (t == error_mark_node)
3450 : : {
3451 : 198 : if (fail_for_non_constant_p)
3452 : : {
3453 : 108 : if (!ctx->quiet)
3454 : : {
3455 : 12 : auto_diagnostic_group d;
3456 : 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3457 : 12 : inform (loc, "static type %qT of its operand is a "
3458 : : "non-public base class of dynamic type %qT",
3459 : : objtype, type);
3460 : :
3461 : 12 : }
3462 : 108 : *non_constant_p = true;
3463 : : }
3464 : 198 : return integer_zero_node;
3465 : : }
3466 : 459 : else if (t)
3467 : : /* The result points to the TYPE object. */
3468 : 414 : return cp_build_addr_expr (t, complain);
3469 : : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3470 : : Fall through to the normal processing. */
3471 : : }
3472 : :
3473 : : /* Otherwise, if v points (refers) to a public base class subobject of the
3474 : : most derived object, and the type of the most derived object has a base
3475 : : class, of type C, that is unambiguous and public, the result points
3476 : : (refers) to the C subobject of the most derived object.
3477 : :
3478 : : But it can also be an invalid case. */
3479 : :
3480 : : /* Get the most derived object. */
3481 : 1555 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3482 : 1555 : if (obj == error_mark_node)
3483 : : {
3484 : 806 : if (fail_for_non_constant_p)
3485 : : {
3486 : 350 : if (!ctx->quiet)
3487 : : {
3488 : 38 : auto_diagnostic_group d;
3489 : 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3490 : 38 : inform (loc, "static type %qT of its operand is a non-public"
3491 : : " base class of dynamic type %qT", objtype, mdtype);
3492 : 38 : }
3493 : 350 : *non_constant_p = true;
3494 : : }
3495 : 806 : return integer_zero_node;
3496 : : }
3497 : : else
3498 : 749 : gcc_assert (obj);
3499 : :
3500 : : /* Check that the type of the most derived object has a base class
3501 : : of type TYPE that is unambiguous and public. */
3502 : 749 : base_kind b_kind;
3503 : 749 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3504 : 749 : if (!binfo || binfo == error_mark_node)
3505 : : {
3506 : 339 : if (fail_for_non_constant_p)
3507 : : {
3508 : 134 : if (!ctx->quiet)
3509 : : {
3510 : 16 : auto_diagnostic_group d;
3511 : 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3512 : 16 : if (b_kind == bk_ambig)
3513 : 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3514 : : "type %qT of its operand", type, mdtype);
3515 : : else
3516 : 10 : inform (loc, "dynamic type %qT of its operand does not "
3517 : : "have an unambiguous public base class %qT",
3518 : : mdtype, type);
3519 : 16 : }
3520 : 134 : *non_constant_p = true;
3521 : : }
3522 : 339 : return integer_zero_node;
3523 : : }
3524 : : /* If so, return the TYPE subobject of the most derived object. */
3525 : 410 : obj = convert_to_base_statically (obj, binfo);
3526 : 410 : return cp_build_addr_expr (obj, complain);
3527 : : }
3528 : :
3529 : : /* Data structure used by replace_decl and replace_decl_r. */
3530 : :
3531 : : struct replace_decl_data
3532 : : {
3533 : : /* The _DECL we want to replace. */
3534 : : tree decl;
3535 : : /* The replacement for DECL. */
3536 : : tree replacement;
3537 : : /* Trees we've visited. */
3538 : : hash_set<tree> *pset;
3539 : : /* Whether we've performed any replacements. */
3540 : : bool changed;
3541 : : };
3542 : :
3543 : : /* Helper function for replace_decl, called through cp_walk_tree. */
3544 : :
3545 : : static tree
3546 : 5841239 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3547 : : {
3548 : 5841239 : replace_decl_data *d = (replace_decl_data *) data;
3549 : :
3550 : : /* We could be replacing
3551 : : &<retval>.bar -> &foo.bar
3552 : : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3553 : : on the ADDR_EXPR around it. */
3554 : 5841239 : if (TREE_CODE (*tp) == ADDR_EXPR)
3555 : : {
3556 : 611755 : d->pset->add (*tp);
3557 : 611755 : auto save_changed = d->changed;
3558 : 611755 : d->changed = false;
3559 : 611755 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3560 : 611755 : if (d->changed)
3561 : : {
3562 : 326 : cxx_mark_addressable (*tp);
3563 : 326 : recompute_tree_invariant_for_addr_expr (*tp);
3564 : : }
3565 : : else
3566 : 611429 : d->changed = save_changed;
3567 : 611755 : *walk_subtrees = 0;
3568 : : }
3569 : 5229484 : else if (*tp == d->decl)
3570 : : {
3571 : 661 : *tp = unshare_expr (d->replacement);
3572 : 661 : d->changed = true;
3573 : 661 : *walk_subtrees = 0;
3574 : : }
3575 : 5228823 : else if (TYPE_P (*tp)
3576 : 5228823 : || d->pset->add (*tp))
3577 : 740715 : *walk_subtrees = 0;
3578 : :
3579 : 5841239 : return NULL_TREE;
3580 : : }
3581 : :
3582 : : /* Replace every occurrence of DECL with (an unshared copy of)
3583 : : REPLACEMENT within the expression *TP. Returns true iff a
3584 : : replacement was performed. */
3585 : :
3586 : : bool
3587 : 784787 : replace_decl (tree *tp, tree decl, tree replacement)
3588 : : {
3589 : 784787 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3590 : : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3591 : 784787 : hash_set<tree> pset;
3592 : 784787 : replace_decl_data data = { decl, replacement, &pset, false };
3593 : 784787 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3594 : 784787 : return data.changed;
3595 : 784787 : }
3596 : :
3597 : : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3598 : :
3599 : : static tree
3600 : 27 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3601 : : value_cat lval,
3602 : : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3603 : : {
3604 : 27 : tree function = THUNK_TARGET (thunk_fndecl);
3605 : :
3606 : 27 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3607 : : {
3608 : 11 : if (!ctx->quiet)
3609 : : {
3610 : 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3611 : : {
3612 : 0 : error ("call to non-%<constexpr%> function %qD", function);
3613 : 0 : explain_invalid_constexpr_fn (function);
3614 : : }
3615 : : else
3616 : : /* virtual_offset is only set for virtual bases, which make the
3617 : : class non-literal, so we don't need to handle it here. */
3618 : 3 : error ("calling constexpr member function %qD through virtual "
3619 : : "base subobject", function);
3620 : : }
3621 : 11 : *non_constant_p = true;
3622 : 11 : return t;
3623 : : }
3624 : :
3625 : 16 : tree new_call = copy_node (t);
3626 : 16 : CALL_EXPR_FN (new_call) = function;
3627 : 16 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3628 : :
3629 : 16 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3630 : :
3631 : 16 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3632 : : {
3633 : : /* 'this'-adjusting thunk. */
3634 : 10 : tree this_arg = CALL_EXPR_ARG (t, 0);
3635 : 10 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3636 : : this_arg, offset);
3637 : 10 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3638 : : }
3639 : : else
3640 : : /* Return-adjusting thunk. */
3641 : 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3642 : : new_call, offset);
3643 : :
3644 : 16 : return cxx_eval_constant_expression (ctx, new_call, lval,
3645 : : non_constant_p, overflow_p,
3646 : 16 : jump_target);
3647 : : }
3648 : :
3649 : : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3650 : : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3651 : : 'tors to detect modifying const objects in a constexpr context. */
3652 : :
3653 : : static void
3654 : 4279057 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3655 : : bool readonly_p, bool *non_constant_p,
3656 : : bool *overflow_p, tree *jump_target)
3657 : : {
3658 : 8558114 : if (CLASS_TYPE_P (TREE_TYPE (object))
3659 : 8558114 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3660 : : {
3661 : : /* Subobjects might not be stored in ctx->global->values but we
3662 : : can get its CONSTRUCTOR by evaluating *this. */
3663 : 541737 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3664 : : non_constant_p, overflow_p,
3665 : : jump_target);
3666 : 541737 : if (!*non_constant_p
3667 : 4810035 : && !throws (jump_target)
3668 : 1072715 : && TREE_CODE (e) == CONSTRUCTOR)
3669 : 530978 : TREE_READONLY (e) = readonly_p;
3670 : : }
3671 : 4279057 : }
3672 : :
3673 : : /* Subroutine of cxx_eval_constant_expression.
3674 : : Evaluate the call expression tree T in the context of OLD_CALL expression
3675 : : evaluation. */
3676 : :
3677 : : static tree
3678 : 89161349 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3679 : : value_cat lval,
3680 : : bool *non_constant_p, bool *overflow_p,
3681 : : tree *jump_target)
3682 : : {
3683 : 89161349 : location_t loc = cp_expr_loc_or_input_loc (t);
3684 : 89161349 : tree fun = get_function_named_in_call (t);
3685 : :
3686 : 89161349 : if (fun == NULL_TREE)
3687 : 332932 : return cxx_eval_internal_function (ctx, t, lval,
3688 : : non_constant_p, overflow_p,
3689 : 332932 : jump_target);
3690 : :
3691 : 88828417 : if (TREE_CODE (fun) != FUNCTION_DECL)
3692 : : {
3693 : : /* Might be a constexpr function pointer. */
3694 : 121985 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3695 : : non_constant_p, overflow_p,
3696 : : jump_target);
3697 : 121985 : if (*jump_target)
3698 : : return NULL_TREE;
3699 : 121985 : STRIP_NOPS (fun);
3700 : 121985 : if (TREE_CODE (fun) == ADDR_EXPR)
3701 : 19915 : fun = TREE_OPERAND (fun, 0);
3702 : : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3703 : : indirection, the called expression is a pointer into the
3704 : : virtual table which should contain FDESC_EXPR. Extract the
3705 : : FUNCTION_DECL from there. */
3706 : : else if (TARGET_VTABLE_USES_DESCRIPTORS
3707 : : && TREE_CODE (fun) == POINTER_PLUS_EXPR
3708 : : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
3709 : : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
3710 : : {
3711 : : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
3712 : : if (VAR_P (d)
3713 : : && DECL_VTABLE_OR_VTT_P (d)
3714 : : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
3715 : : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
3716 : : && DECL_INITIAL (d)
3717 : : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
3718 : : {
3719 : : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
3720 : : TYPE_SIZE_UNIT (vtable_entry_type));
3721 : : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
3722 : : if (idx >= 0)
3723 : : {
3724 : : tree fdesc
3725 : : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
3726 : : if (TREE_CODE (fdesc) == FDESC_EXPR
3727 : : && integer_zerop (TREE_OPERAND (fdesc, 1)))
3728 : : fun = TREE_OPERAND (fdesc, 0);
3729 : : }
3730 : : }
3731 : : }
3732 : : }
3733 : 88828417 : if (TREE_CODE (fun) != FUNCTION_DECL)
3734 : : {
3735 : 102070 : if (!ctx->quiet && !*non_constant_p)
3736 : 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
3737 : : "function", fun);
3738 : 102070 : *non_constant_p = true;
3739 : 102070 : return t;
3740 : : }
3741 : 88726347 : tree orig_fun = fun;
3742 : 88726347 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
3743 : 9587858 : fun = DECL_CLONED_FUNCTION (fun);
3744 : :
3745 : 88726347 : if (is_ubsan_builtin_p (fun))
3746 : 58 : return void_node;
3747 : :
3748 : 88726289 : if (fndecl_built_in_p (fun))
3749 : 11192823 : return cxx_eval_builtin_function_call (ctx, t, fun,
3750 : : lval, non_constant_p, overflow_p,
3751 : 11192822 : jump_target);
3752 : 77533466 : if (DECL_THUNK_P (fun))
3753 : 27 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
3754 : 27 : jump_target);
3755 : 77533439 : bool non_constexpr_call = false;
3756 : 77533439 : if (!maybe_constexpr_fn (fun))
3757 : : {
3758 : 236080 : if (TREE_CODE (t) == CALL_EXPR
3759 : 222009 : && cxx_replaceable_global_alloc_fn (fun)
3760 : 319532 : && (CALL_FROM_NEW_OR_DELETE_P (t)
3761 : 28455 : || is_std_allocator_allocate (ctx->call)))
3762 : : {
3763 : 63202 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
3764 : 63202 : const int nargs = call_expr_nargs (t);
3765 : 63202 : tree arg0 = NULL_TREE;
3766 : 101542 : for (int i = 0; i < nargs; ++i)
3767 : : {
3768 : 63738 : tree arg = CALL_EXPR_ARG (t, i);
3769 : 63738 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3770 : : non_constant_p, overflow_p,
3771 : : jump_target);
3772 : 63738 : if (*jump_target)
3773 : : return NULL_TREE;
3774 : : /* Deleting a non-constant pointer has a better error message
3775 : : below. */
3776 : 63730 : if (new_op_p || i != 0)
3777 : 59066 : VERIFY_CONSTANT (arg);
3778 : 33676 : if (i == 0)
3779 : : arg0 = arg;
3780 : : }
3781 : 37804 : gcc_assert (arg0);
3782 : 37804 : if (new_op_p)
3783 : : {
3784 : 33140 : if (!tree_fits_uhwi_p (arg0))
3785 : : {
3786 : : /* We should not get here; the VERIFY_CONSTANT above
3787 : : should have already caught it. */
3788 : 0 : gcc_checking_assert (false);
3789 : : if (!ctx->quiet)
3790 : : error_at (loc, "cannot allocate array: size not constant");
3791 : : *non_constant_p = true;
3792 : : return t;
3793 : : }
3794 : 66280 : tree type = build_array_type_nelts (char_type_node,
3795 : 33140 : tree_to_uhwi (arg0));
3796 : 33140 : tree var = build_decl (loc, VAR_DECL,
3797 : 33140 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3798 : : & OVL_OP_FLAG_VEC)
3799 : : ? heap_vec_uninit_identifier
3800 : : : heap_uninit_identifier,
3801 : 33140 : type);
3802 : 33140 : DECL_ARTIFICIAL (var) = 1;
3803 : 33140 : ctx->global->heap_vars.safe_push (var);
3804 : 33140 : ctx->global->put_value (var, NULL_TREE);
3805 : 33140 : return fold_convert (ptr_type_node, build_address (var));
3806 : : }
3807 : : else
3808 : : {
3809 : 4664 : STRIP_NOPS (arg0);
3810 : 4664 : if (TREE_CODE (arg0) == ADDR_EXPR
3811 : 4664 : && VAR_P (TREE_OPERAND (arg0, 0)))
3812 : : {
3813 : 4664 : tree var = TREE_OPERAND (arg0, 0);
3814 : 4664 : if (DECL_NAME (var) == heap_uninit_identifier
3815 : 4664 : || DECL_NAME (var) == heap_identifier)
3816 : : {
3817 : 4498 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3818 : : & OVL_OP_FLAG_VEC)
3819 : : {
3820 : 9 : if (!ctx->quiet)
3821 : : {
3822 : 3 : auto_diagnostic_group d;
3823 : 3 : error_at (loc, "array deallocation of object "
3824 : : "allocated with non-array "
3825 : : "allocation");
3826 : 3 : inform (DECL_SOURCE_LOCATION (var),
3827 : : "allocation performed here");
3828 : 3 : }
3829 : 9 : *non_constant_p = true;
3830 : 9 : return t;
3831 : : }
3832 : 4489 : DECL_NAME (var) = heap_deleted_identifier;
3833 : 4489 : ctx->global->destroy_value (var);
3834 : 4489 : ctx->global->heap_dealloc_count++;
3835 : 4489 : return void_node;
3836 : : }
3837 : 166 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
3838 : 166 : || DECL_NAME (var) == heap_vec_identifier)
3839 : : {
3840 : 142 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3841 : : & OVL_OP_FLAG_VEC) == 0)
3842 : : {
3843 : 9 : if (!ctx->quiet)
3844 : : {
3845 : 3 : auto_diagnostic_group d;
3846 : 3 : error_at (loc, "non-array deallocation of "
3847 : : "object allocated with array "
3848 : : "allocation");
3849 : 3 : inform (DECL_SOURCE_LOCATION (var),
3850 : : "allocation performed here");
3851 : 3 : }
3852 : 9 : *non_constant_p = true;
3853 : 9 : return t;
3854 : : }
3855 : 133 : DECL_NAME (var) = heap_deleted_identifier;
3856 : 133 : ctx->global->destroy_value (var);
3857 : 133 : ctx->global->heap_dealloc_count++;
3858 : 133 : return void_node;
3859 : : }
3860 : 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
3861 : : {
3862 : 15 : if (!ctx->quiet)
3863 : 6 : error_at (loc, "deallocation of already deallocated "
3864 : : "storage");
3865 : 15 : *non_constant_p = true;
3866 : 15 : return t;
3867 : : }
3868 : : }
3869 : 9 : if (!ctx->quiet)
3870 : 3 : error_at (loc, "deallocation of storage that was "
3871 : : "not previously allocated");
3872 : 9 : *non_constant_p = true;
3873 : 9 : return t;
3874 : : }
3875 : : }
3876 : : /* Allow placement new in std::construct_at, just return the second
3877 : : argument. */
3878 : 172878 : if (TREE_CODE (t) == CALL_EXPR
3879 : 158807 : && cxx_placement_new_fn (fun)
3880 : 218184 : && is_std_construct_at (ctx->call))
3881 : : {
3882 : 28937 : const int nargs = call_expr_nargs (t);
3883 : 28937 : tree arg1 = NULL_TREE;
3884 : 86811 : for (int i = 0; i < nargs; ++i)
3885 : : {
3886 : 57874 : tree arg = CALL_EXPR_ARG (t, i);
3887 : 57874 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3888 : : non_constant_p, overflow_p,
3889 : : jump_target);
3890 : 57874 : if (*jump_target)
3891 : : return NULL_TREE;
3892 : 57874 : if (i == 1)
3893 : : arg1 = arg;
3894 : : else
3895 : 57874 : VERIFY_CONSTANT (arg);
3896 : : }
3897 : 28937 : gcc_assert (arg1);
3898 : : return arg1;
3899 : : }
3900 : 143941 : else if (cxx_dynamic_cast_fn_p (fun))
3901 : 2402 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
3902 : 2402 : jump_target);
3903 : 141539 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
3904 : 27513 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
3905 : : non_constant_p, overflow_p,
3906 : 27513 : jump_target);
3907 : :
3908 : : /* Calls to non-constexpr functions can be diagnosed right away
3909 : : before C++26, though in C++26 evaluation of the arguments might
3910 : : throw and if caught it could be still constant expression.
3911 : : So for C++26 this is diagnosed only after
3912 : : cxx_bind_parameters_in_call. */
3913 : 114026 : if (cxx_dialect >= cxx26)
3914 : : non_constexpr_call = true;
3915 : : else
3916 : : {
3917 : 30922 : if (!ctx->quiet)
3918 : : {
3919 : 188 : if (!lambda_static_thunk_p (fun))
3920 : 188 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3921 : 188 : explain_invalid_constexpr_fn (fun);
3922 : : }
3923 : 30922 : *non_constant_p = true;
3924 : 30922 : return t;
3925 : : }
3926 : : }
3927 : :
3928 : 77380463 : constexpr_ctx new_ctx = *ctx;
3929 : 77380463 : ctx = &new_ctx;
3930 : 86738850 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
3931 : 78614028 : && TREE_CODE (t) == AGGR_INIT_EXPR)
3932 : : {
3933 : : /* We want to have an initialization target for an AGGR_INIT_EXPR.
3934 : : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
3935 : 649 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
3936 : 649 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
3937 : 649 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
3938 : 649 : ctx->global->put_value (new_ctx.object, ctor);
3939 : : }
3940 : : /* An immediate invocation is manifestly constant evaluated including the
3941 : : arguments of the call, so use mce_true even for the argument
3942 : : evaluation. */
3943 : 154760926 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3944 : 1255415 : new_ctx.manifestly_const_eval = mce_true;
3945 : :
3946 : : /* We used to shortcut trivial constructor/op= here, but nowadays
3947 : : we can only get a trivial function here with -fno-elide-constructors. */
3948 : 77380463 : gcc_checking_assert (!trivial_fn_p (fun)
3949 : : || !flag_elide_constructors
3950 : : /* Or it's a call from maybe_thunk_body (111075). */
3951 : : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
3952 : : : AGGR_INIT_FROM_THUNK_P (t))
3953 : : /* We don't elide constructors when processing
3954 : : a noexcept-expression. */
3955 : : || cp_noexcept_operand);
3956 : :
3957 : 77380463 : bool non_constant_args = false;
3958 : 77380463 : constexpr_call new_call;
3959 : 77380463 : new_call.bindings
3960 : 77380463 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
3961 : : overflow_p, &non_constant_args,
3962 : : jump_target);
3963 : :
3964 : : /* We build up the bindings list before we know whether we already have this
3965 : : call cached. If we don't end up saving these bindings, ggc_free them when
3966 : : this function exits. */
3967 : 77380463 : class free_bindings
3968 : : {
3969 : : tree *bindings;
3970 : : public:
3971 : 77380463 : free_bindings (tree &b): bindings (&b) { }
3972 : 77380461 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
3973 : 3085284 : void preserve () { bindings = NULL; }
3974 : 77380463 : } fb (new_call.bindings);
3975 : :
3976 : 77380463 : if (*jump_target)
3977 : : return NULL_TREE;
3978 : 77380454 : if (*non_constant_p)
3979 : : return t;
3980 : 43507638 : if (non_constexpr_call)
3981 : : {
3982 : 7063 : if (!ctx->quiet)
3983 : : {
3984 : 207 : if (!lambda_static_thunk_p (fun))
3985 : 207 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3986 : 207 : explain_invalid_constexpr_fn (fun);
3987 : : }
3988 : 7063 : *non_constant_p = true;
3989 : 7063 : return t;
3990 : : }
3991 : :
3992 : : /* We can't defer instantiating the function any longer. */
3993 : 43500575 : if (!DECL_INITIAL (fun)
3994 : 1705964 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
3995 : 43500575 : && !uid_sensitive_constexpr_evaluation_p ())
3996 : : {
3997 : 1524465 : location_t save_loc = input_location;
3998 : 1524465 : input_location = loc;
3999 : 1524465 : ++function_depth;
4000 : 1524465 : if (ctx->manifestly_const_eval == mce_true)
4001 : 200056 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4002 : 1524465 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4003 : 1524457 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4004 : : else
4005 : 8 : synthesize_method (fun);
4006 : 1524465 : --function_depth;
4007 : 1524465 : input_location = save_loc;
4008 : : }
4009 : :
4010 : : /* If in direct recursive call, optimize definition search. */
4011 : 43500575 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4012 : 27534 : new_call.fundef = ctx->call->fundef;
4013 : : else
4014 : : {
4015 : 43473041 : new_call.fundef = retrieve_constexpr_fundef (fun);
4016 : 43473041 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4017 : 43198224 : || new_call.fundef->result == error_mark_node
4018 : 43178717 : || fun == current_function_decl)
4019 : : {
4020 : 294327 : if (!ctx->quiet)
4021 : : {
4022 : : /* We need to check for current_function_decl here in case we're
4023 : : being called during cp_fold_function, because at that point
4024 : : DECL_INITIAL is set properly and we have a fundef but we
4025 : : haven't lowered invisirefs yet (c++/70344). */
4026 : 157 : if (DECL_INITIAL (fun) == error_mark_node
4027 : 157 : || fun == current_function_decl)
4028 : 15 : error_at (loc, "%qD called in a constant expression before its "
4029 : : "definition is complete", fun);
4030 : 142 : else if (DECL_INITIAL (fun))
4031 : : {
4032 : : /* The definition of fun was somehow unsuitable. But pretend
4033 : : that lambda static thunks don't exist. */
4034 : 105 : if (!lambda_static_thunk_p (fun))
4035 : 103 : error_at (loc, "%qD called in a constant expression", fun);
4036 : 105 : explain_invalid_constexpr_fn (fun);
4037 : : }
4038 : : else
4039 : 37 : error_at (loc, "%qD used before its definition", fun);
4040 : : }
4041 : 294327 : *non_constant_p = true;
4042 : 294327 : return t;
4043 : : }
4044 : : }
4045 : :
4046 : : /* Don't complain about problems evaluating an ill-formed function. */
4047 : 43206248 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4048 : 43206248 : if (f->language->erroneous)
4049 : 561 : new_ctx.quiet = true;
4050 : :
4051 : 43206248 : int depth_ok = push_cx_call_context (t);
4052 : :
4053 : : /* Remember the object we are constructing or destructing. */
4054 : 43206248 : tree new_obj = NULL_TREE;
4055 : 86412496 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4056 : : {
4057 : : /* In a cdtor, it should be the first `this' argument.
4058 : : At this point it has already been evaluated in the call
4059 : : to cxx_bind_parameters_in_call. */
4060 : 5121876 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4061 : 5121876 : bool empty_base = false;
4062 : 5121876 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4063 : : &empty_base, jump_target);
4064 : 5121876 : if (*jump_target)
4065 : 0 : return NULL_TREE;
4066 : : /* If we're initializing an empty class, don't set constness, because
4067 : : cxx_fold_indirect_ref will return the wrong object to set constness
4068 : : of. */
4069 : 5121876 : if (empty_base)
4070 : : new_obj = NULL_TREE;
4071 : 2289508 : else if (ctx->call && ctx->call->fundef
4072 : 8884709 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4073 : : {
4074 : 1382128 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4075 : 1382128 : STRIP_NOPS (cur_obj);
4076 : 1382128 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4077 : 1380881 : cur_obj = TREE_OPERAND (cur_obj, 0);
4078 : 1382128 : if (new_obj == cur_obj)
4079 : : /* We're calling the target constructor of a delegating
4080 : : constructor, or accessing a base subobject through a
4081 : : NOP_EXPR as part of a call to a base constructor, so
4082 : : there is no new (sub)object. */
4083 : 842816 : new_obj = NULL_TREE;
4084 : : }
4085 : : }
4086 : :
4087 : 43206248 : tree result = NULL_TREE;
4088 : :
4089 : 43206248 : constexpr_call *entry = NULL;
4090 : 43206248 : if (depth_ok && !non_constant_args && ctx->strict)
4091 : : {
4092 : 19921664 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4093 : 19921664 : new_call.hash
4094 : 19921664 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4095 : :
4096 : : /* If we have seen this call before, we are done. */
4097 : 19921664 : maybe_initialize_constexpr_call_table ();
4098 : 19921664 : bool insert = depth_ok < constexpr_cache_depth;
4099 : 19921664 : constexpr_call **slot
4100 : 20472239 : = constexpr_call_table->find_slot (&new_call,
4101 : : insert ? INSERT : NO_INSERT);
4102 : 19921664 : entry = slot ? *slot : NULL;
4103 : 19700832 : if (entry == NULL)
4104 : : {
4105 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
4106 : 3306116 : if (insert)
4107 : : {
4108 : : /* We need to keep a pointer to the entry, not just the slot, as
4109 : : the slot can move during evaluation of the body. */
4110 : 3085284 : *slot = entry = ggc_alloc<constexpr_call> ();
4111 : 3085284 : *entry = new_call;
4112 : 3085284 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4113 : 3085284 : fb.preserve ();
4114 : : }
4115 : : }
4116 : : /* Calls that are in progress have their result set to unknown_type_node,
4117 : : so that we can detect circular dependencies. Now that we only cache
4118 : : up to constexpr_cache_depth this won't catch circular dependencies that
4119 : : start deeper, but they'll hit the recursion or ops limit. */
4120 : 16615548 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4121 : : {
4122 : 6 : if (!ctx->quiet)
4123 : 0 : error ("call has circular dependency");
4124 : 6 : *non_constant_p = true;
4125 : 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4126 : : }
4127 : : else
4128 : 16615542 : result = entry->result (ctx->manifestly_const_eval);
4129 : : }
4130 : :
4131 : 43206248 : if (!depth_ok)
4132 : : {
4133 : 30 : if (!ctx->quiet)
4134 : 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4135 : : "%<-fconstexpr-depth=%> to increase the maximum)",
4136 : : max_constexpr_depth);
4137 : 30 : *non_constant_p = true;
4138 : 30 : result = error_mark_node;
4139 : : }
4140 : : else
4141 : : {
4142 : 43206218 : bool cacheable = !!entry;
4143 : 43206218 : if (result && result != error_mark_node)
4144 : : /* OK */;
4145 : 29740059 : else if (!DECL_SAVED_TREE (fun))
4146 : : {
4147 : : /* When at_eof >= 3, cgraph has started throwing away
4148 : : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4149 : : late code generation for VEC_INIT_EXPR, which needs to be
4150 : : completely reconsidered. */
4151 : 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4152 : 0 : *non_constant_p = true;
4153 : : }
4154 : 29740059 : else if (tree copy = get_fundef_copy (new_call.fundef))
4155 : : {
4156 : 29740059 : tree body, parms, res;
4157 : 29740059 : releasing_vec ctors;
4158 : :
4159 : : /* Reuse or create a new unshared copy of this function's body. */
4160 : 29740059 : body = TREE_PURPOSE (copy);
4161 : 29740059 : parms = TREE_VALUE (copy);
4162 : 29740059 : res = TREE_TYPE (copy);
4163 : :
4164 : : /* Associate the bindings with the remapped parms. */
4165 : 29740059 : tree bound = new_call.bindings;
4166 : 29740059 : tree remapped = parms;
4167 : 65562606 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4168 : : {
4169 : 35822547 : tree arg = TREE_VEC_ELT (bound, i);
4170 : 35822547 : if (entry)
4171 : : {
4172 : : /* Unshare args going into the hash table to separate them
4173 : : from the caller's context, for better GC and to avoid
4174 : : problems with verify_gimple. */
4175 : 2018846 : arg = unshare_expr_without_location (arg);
4176 : 2018846 : TREE_VEC_ELT (bound, i) = arg;
4177 : :
4178 : : /* And then unshare again so the callee doesn't change the
4179 : : argument values in the hash table. XXX Could we unshare
4180 : : lazily in cxx_eval_store_expression? */
4181 : 2018846 : arg = unshare_constructor (arg);
4182 : 2018846 : if (TREE_CODE (arg) == CONSTRUCTOR)
4183 : 761681 : vec_safe_push (ctors, arg);
4184 : : }
4185 : 35822547 : ctx->global->put_value (remapped, arg);
4186 : 35822547 : remapped = DECL_CHAIN (remapped);
4187 : : }
4188 : 29740059 : if (remapped)
4189 : : {
4190 : : /* We shouldn't have any parms without args, but fail gracefully
4191 : : in error recovery. */
4192 : 6 : gcc_checking_assert (seen_error ());
4193 : 6 : *non_constant_p = true;
4194 : : }
4195 : : /* Add the RESULT_DECL to the values map, too. */
4196 : 29740059 : gcc_assert (!DECL_BY_REFERENCE (res));
4197 : 29740059 : ctx->global->put_value (res, NULL_TREE);
4198 : :
4199 : : /* Remember the current call we're evaluating. */
4200 : 29740059 : constexpr_ctx call_ctx = *ctx;
4201 : 29740059 : call_ctx.call = &new_call;
4202 : 29740059 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4203 : 29740059 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4204 : :
4205 : : /* Make sure we fold std::is_constant_evaluated to true in an
4206 : : immediate function. */
4207 : 59480118 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4208 : 663489 : call_ctx.manifestly_const_eval = mce_true;
4209 : :
4210 : : /* If this is a constexpr destructor, the object's const and volatile
4211 : : semantics are no longer in effect; see [class.dtor]p5. */
4212 : 34019116 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4213 : 209835 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4214 : : non_constant_p, overflow_p, jump_target);
4215 : :
4216 : : /* If this is a constructor, we are beginning the lifetime of the
4217 : : object we are initializing. */
4218 : 29740059 : if (new_obj
4219 : 8558114 : && DECL_CONSTRUCTOR_P (fun)
4220 : 4069222 : && TREE_CODE (new_obj) == COMPONENT_REF
4221 : 30763069 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4222 : : {
4223 : 3029 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4224 : 3029 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4225 : 3029 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4226 : : new_obj, ctor);
4227 : 3029 : cxx_eval_constant_expression (ctx, activate,
4228 : : lval, non_constant_p, overflow_p,
4229 : : jump_target);
4230 : 3029 : ggc_free (activate);
4231 : 3029 : if (*jump_target)
4232 : 0 : return NULL_TREE;
4233 : : }
4234 : :
4235 : 29740059 : tree jmp_target = NULL_TREE;
4236 : 29740059 : cxx_eval_constant_expression (&call_ctx, body,
4237 : : vc_discard, non_constant_p, overflow_p,
4238 : : &jmp_target);
4239 : :
4240 : 29740057 : if (!*non_constant_p && throws (&jmp_target))
4241 : : {
4242 : 293 : result = NULL_TREE;
4243 : 293 : cacheable = false;
4244 : 293 : *jump_target = jmp_target;
4245 : : }
4246 : 59479528 : else if (DECL_CONSTRUCTOR_P (fun))
4247 : : /* This can be null for a subobject constructor call, in
4248 : : which case what we care about is the initialization
4249 : : side-effects rather than the value. We could get at the
4250 : : value by evaluating *this, but we don't bother; there's
4251 : : no need to put such a call in the hash table. */
4252 : 4906528 : result = lval ? ctx->object : ctx->ctor;
4253 : 24833236 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4254 : 1450530 : result = void_node;
4255 : : else
4256 : : {
4257 : 23382706 : result = ctx->global->get_value (res);
4258 : 8094168 : if (result == NULL_TREE && !*non_constant_p
4259 : 23382896 : && !DECL_DESTRUCTOR_P (fun))
4260 : : {
4261 : 95 : if (!ctx->quiet)
4262 : 6 : error ("%<constexpr%> call flows off the end "
4263 : : "of the function");
4264 : 95 : *non_constant_p = true;
4265 : : }
4266 : : }
4267 : :
4268 : : /* At this point, the object's constructor will have run, so
4269 : : the object is no longer under construction, and its possible
4270 : : 'const' semantics now apply. Make a note of this fact by
4271 : : marking the CONSTRUCTOR TREE_READONLY. */
4272 : 34019114 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4273 : 4069222 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4274 : : non_constant_p, overflow_p, jump_target);
4275 : :
4276 : : /* Remove the parms/result from the values map. */
4277 : 29740057 : destroy_value_checked (ctx, res, non_constant_p);
4278 : 65562609 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4279 : 35822552 : destroy_value_checked (ctx, parm, non_constant_p);
4280 : :
4281 : : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4282 : 30501738 : while (!ctors->is_empty ())
4283 : : {
4284 : 761681 : tree c = ctors->pop ();
4285 : 761681 : if (c != result)
4286 : 761681 : free_constructor (c);
4287 : : }
4288 : :
4289 : : /* Make the unshared function copy we used available for re-use. */
4290 : 29740057 : save_fundef_copy (fun, copy);
4291 : :
4292 : : /* If the call allocated some heap object that hasn't been
4293 : : deallocated during the call, or if it deallocated some heap
4294 : : object it has not allocated, the call isn't really stateless
4295 : : for the constexpr evaluation and should not be cached.
4296 : : It is fine if the call allocates something and deallocates it
4297 : : too. */
4298 : 29740057 : if (cacheable
4299 : 35974504 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4300 : 6232873 : || (save_heap_dealloc_count
4301 : 6232873 : != ctx->global->heap_dealloc_count)))
4302 : : {
4303 : 1574 : tree heap_var;
4304 : 1574 : unsigned int i;
4305 : 1574 : if ((ctx->global->heap_vars.length ()
4306 : 1574 : - ctx->global->heap_dealloc_count)
4307 : 1574 : != save_heap_alloc_count - save_heap_dealloc_count)
4308 : : cacheable = false;
4309 : : else
4310 : 11924 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4311 : : save_heap_alloc_count)
4312 : 10572 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4313 : : {
4314 : : cacheable = false;
4315 : : break;
4316 : : }
4317 : : }
4318 : :
4319 : : /* Rewrite all occurrences of the function's RESULT_DECL with the
4320 : : current object under construction. */
4321 : 29740057 : if (!*non_constant_p
4322 : 19832508 : && ctx->object
4323 : 6221532 : && CLASS_TYPE_P (TREE_TYPE (res))
4324 : 31238829 : && !is_empty_class (TREE_TYPE (res)))
4325 : : {
4326 : 784615 : if (!same_type_ignoring_top_level_qualifiers_p
4327 : 784615 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4328 : 0 : *non_constant_p = true;
4329 : 784615 : else if (replace_decl (&result, res, ctx->object))
4330 : : {
4331 : 238 : cacheable = false;
4332 : 238 : result = cxx_eval_constant_expression (ctx, result, lval,
4333 : : non_constant_p,
4334 : : overflow_p,
4335 : : jump_target);
4336 : 238 : if (*jump_target)
4337 : : {
4338 : 0 : cacheable = false;
4339 : 0 : result = NULL_TREE;
4340 : : }
4341 : : }
4342 : : }
4343 : :
4344 : : /* Only cache a permitted result of a constant expression. */
4345 : 29739819 : if (cacheable && !reduced_constant_expression_p (result))
4346 : : cacheable = false;
4347 : 29740057 : }
4348 : : else
4349 : : /* Couldn't get a function copy to evaluate. */
4350 : 0 : *non_constant_p = true;
4351 : :
4352 : 43206216 : if (result == error_mark_node)
4353 : 0 : *non_constant_p = true;
4354 : 43206216 : if (*non_constant_p || *overflow_p)
4355 : 9907705 : result = error_mark_node;
4356 : 33298511 : else if (!result)
4357 : 1232133 : result = void_node;
4358 : 43206216 : if (entry)
4359 : : {
4360 : 39401662 : entry->result (ctx->manifestly_const_eval)
4361 : 19700831 : = cacheable ? result : error_mark_node;
4362 : :
4363 : 19700831 : if (result != error_mark_node
4364 : 16623759 : && ctx->manifestly_const_eval == mce_unknown)
4365 : : {
4366 : : /* Evaluation succeeded and was independent of whether we're in a
4367 : : manifestly constant-evaluated context, so we can also reuse
4368 : : this result when evaluating this call with a fixed context. */
4369 : 3431089 : if (!entry->result (mce_true))
4370 : 634561 : entry->result (mce_true) = entry->result (mce_unknown);
4371 : 3431089 : if (!entry->result (mce_false))
4372 : 622398 : entry->result (mce_false) = entry->result (mce_unknown);
4373 : : }
4374 : : }
4375 : : }
4376 : :
4377 : : /* The result of a constexpr function must be completely initialized.
4378 : :
4379 : : However, in C++20, a constexpr constructor doesn't necessarily have
4380 : : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4381 : : in order to detect reading an unitialized object in constexpr instead
4382 : : of value-initializing it. (reduced_constant_expression_p is expected to
4383 : : take care of clearing the flag.) */
4384 : 43206246 : if (TREE_CODE (result) == CONSTRUCTOR
4385 : 43206246 : && (cxx_dialect < cxx20
4386 : 8126794 : || !DECL_CONSTRUCTOR_P (fun)))
4387 : 3953702 : clear_no_implicit_zero (result);
4388 : :
4389 : 43206246 : pop_cx_call_context ();
4390 : 43206246 : return result;
4391 : 77380461 : }
4392 : :
4393 : : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4394 : : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4395 : : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4396 : : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4397 : : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4398 : :
4399 : : bool
4400 : 643678239 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4401 : : {
4402 : 643678239 : if (t == NULL_TREE)
4403 : : return false;
4404 : :
4405 : 640602778 : switch (TREE_CODE (t))
4406 : : {
4407 : : case PTRMEM_CST:
4408 : : /* Even if we can't lower this yet, it's constant. */
4409 : : return true;
4410 : :
4411 : : case OMP_DECLARE_MAPPER:
4412 : : return true;
4413 : :
4414 : 44139803 : case CONSTRUCTOR:
4415 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4416 : 44139803 : tree field;
4417 : 44139803 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4418 : : /* A constant vector would be folded to VECTOR_CST.
4419 : : A CONSTRUCTOR of scalar type means uninitialized. */
4420 : : return false;
4421 : 44124058 : if (CONSTRUCTOR_NO_CLEARING (t))
4422 : : {
4423 : 1383279 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4424 : : {
4425 : : /* There must be a valid constant initializer at every array
4426 : : index. */
4427 : 1520 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4428 : 1520 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4429 : 1520 : tree cursor = min;
4430 : 24669 : for (auto &e: CONSTRUCTOR_ELTS (t))
4431 : : {
4432 : 20219 : if (!reduced_constant_expression_p (e.value))
4433 : : return false;
4434 : 20169 : if (array_index_cmp (cursor, e.index) != 0)
4435 : : return false;
4436 : 20109 : if (TREE_CODE (e.index) == RANGE_EXPR)
4437 : 0 : cursor = TREE_OPERAND (e.index, 1);
4438 : 20109 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4439 : 0 : cursor
4440 : 0 : = int_const_binop (PLUS_EXPR, cursor,
4441 : 0 : size_int (RAW_DATA_LENGTH (e.value)));
4442 : : else
4443 : 20109 : cursor = int_const_binop (PLUS_EXPR, cursor,
4444 : 20109 : size_one_node);
4445 : : }
4446 : 1410 : if (find_array_ctor_elt (t, max) == -1)
4447 : : return false;
4448 : 1324 : goto ok;
4449 : : }
4450 : 1381759 : else if (cxx_dialect >= cxx20
4451 : 1381759 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4452 : : {
4453 : 4313977 : if (CONSTRUCTOR_NELTS (t) == 0)
4454 : : /* An initialized union has a constructor element. */
4455 : : return false;
4456 : : /* And it only initializes one member. */
4457 : : field = NULL_TREE;
4458 : : }
4459 : : else
4460 : 1379966 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4461 : : }
4462 : : else
4463 : : field = NULL_TREE;
4464 : 284529147 : for (auto &e: CONSTRUCTOR_ELTS (t))
4465 : : {
4466 : : /* If VAL is null, we're in the middle of initializing this
4467 : : element. */
4468 : 182582999 : if (!reduced_constant_expression_p (e.value,
4469 : 182582999 : (e.index
4470 : 182582999 : && (TREE_CODE (e.index)
4471 : : == FIELD_DECL))
4472 : 56630098 : ? DECL_SIZE (e.index)
4473 : : : NULL_TREE))
4474 : : return false;
4475 : : /* We want to remove initializers for empty fields in a struct to
4476 : : avoid confusing output_constructor. */
4477 : 182009426 : if (is_empty_field (e.index)
4478 : 182009426 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4479 : : return false;
4480 : : /* Check for non-empty fields between initialized fields when
4481 : : CONSTRUCTOR_NO_CLEARING. */
4482 : 181471436 : for (; field && e.index != field;
4483 : 106803 : field = next_subobject_field (DECL_CHAIN (field)))
4484 : 107046 : if (!is_really_empty_class (TREE_TYPE (field),
4485 : : /*ignore_vptr*/false))
4486 : : return false;
4487 : 181364390 : if (field)
4488 : 1501562 : field = next_subobject_field (DECL_CHAIN (field));
4489 : : }
4490 : : /* There could be a non-empty field at the end. */
4491 : 42982665 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4492 : 82954 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4493 : : {
4494 : : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4495 : : the parent FIELD_DECL (if any) for classes with virtual
4496 : : bases. */
4497 : 2425 : if (cxx_dialect >= cxx26
4498 : 1504 : && sz
4499 : 2804 : && tree_int_cst_le (sz, bit_position (field)))
4500 : : break;
4501 : 2173 : return false;
4502 : : }
4503 : 42899711 : ok:
4504 : 42901287 : if (CONSTRUCTOR_NO_CLEARING (t))
4505 : : /* All the fields are initialized. */
4506 : 1236877 : CONSTRUCTOR_NO_CLEARING (t) = false;
4507 : : return true;
4508 : :
4509 : 596428446 : default:
4510 : : /* FIXME are we calling this too much? */
4511 : 596428446 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4512 : : }
4513 : : }
4514 : :
4515 : : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4516 : : why and suggest what could be done about it. */
4517 : :
4518 : : static tree
4519 : 952 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4520 : : {
4521 : 952 : bool ref_p = false;
4522 : :
4523 : : /* No need to look into types or unevaluated operands. */
4524 : 952 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4525 : : {
4526 : 0 : *walk_subtrees = false;
4527 : 0 : return NULL_TREE;
4528 : : }
4529 : :
4530 : 952 : switch (TREE_CODE (*tp))
4531 : : {
4532 : 70 : CASE_CONVERT:
4533 : 70 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4534 : : break;
4535 : 51 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4536 : 51 : *tp = TREE_OPERAND (*tp, 0);
4537 : 183 : gcc_fallthrough ();
4538 : 183 : case ADDR_EXPR:
4539 : 183 : {
4540 : 183 : tree op = TREE_OPERAND (*tp, 0);
4541 : 183 : if (VAR_P (op)
4542 : 114 : && DECL_DECLARED_CONSTEXPR_P (op)
4543 : 42 : && !TREE_STATIC (op)
4544 : : /* ??? We should also say something about temporaries. */
4545 : 222 : && !DECL_ARTIFICIAL (op))
4546 : : {
4547 : 36 : if (ref_p)
4548 : 15 : inform (location_of (*tp), "reference to %qD is not a constant "
4549 : : "expression", op);
4550 : : else
4551 : 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4552 : : "expression", op);
4553 : 36 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4554 : 36 : rich_location richloc (line_table, op_loc);
4555 : 36 : richloc.add_fixit_insert_before (op_loc, "static ");
4556 : 36 : inform (&richloc,
4557 : : "address of non-static constexpr variable %qD may differ on "
4558 : : "each invocation of the enclosing function; add %<static%> "
4559 : : "to give it a constant address", op);
4560 : 36 : }
4561 : : break;
4562 : : }
4563 : : default:
4564 : : break;
4565 : : }
4566 : :
4567 : : return NULL_TREE;
4568 : : }
4569 : :
4570 : : /* Some expressions may have constant operands but are not constant
4571 : : themselves, such as 1/0. Call this function to check for that
4572 : : condition.
4573 : :
4574 : : We only call this in places that require an arithmetic constant, not in
4575 : : places where we might have a non-constant expression that can be a
4576 : : component of a constant expression, such as the address of a constexpr
4577 : : variable that might be dereferenced later. */
4578 : :
4579 : : static bool
4580 : 396825856 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4581 : : bool *overflow_p)
4582 : : {
4583 : 388321952 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4584 : 399050457 : && t != void_node)
4585 : : {
4586 : 2224526 : if (!allow_non_constant)
4587 : : {
4588 : 256 : auto_diagnostic_group d;
4589 : 360 : error_at (cp_expr_loc_or_input_loc (t),
4590 : : "%q+E is not a constant expression", t);
4591 : 256 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4592 : : nullptr);
4593 : 256 : }
4594 : 2224526 : *non_constant_p = true;
4595 : : }
4596 : 396825856 : if (TREE_OVERFLOW_P (t))
4597 : : {
4598 : 678 : if (!allow_non_constant)
4599 : : {
4600 : 155 : permerror (input_location, "overflow in constant expression");
4601 : : /* If we're being permissive (and are in an enforcing
4602 : : context), ignore the overflow. */
4603 : 155 : if (flag_permissive)
4604 : 75 : return *non_constant_p;
4605 : : }
4606 : 603 : *overflow_p = true;
4607 : : }
4608 : 396825781 : return *non_constant_p;
4609 : : }
4610 : :
4611 : : /* Check whether the shift operation with code CODE and type TYPE on LHS
4612 : : and RHS is undefined. If it is, give an error with an explanation,
4613 : : and return true; return false otherwise. */
4614 : :
4615 : : static bool
4616 : 33688296 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4617 : : enum tree_code code, tree type, tree lhs, tree rhs)
4618 : : {
4619 : 33688296 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4620 : 3084283 : || TREE_CODE (lhs) != INTEGER_CST
4621 : 3084283 : || TREE_CODE (rhs) != INTEGER_CST)
4622 : : return false;
4623 : :
4624 : 3084283 : tree lhstype = TREE_TYPE (lhs);
4625 : 3084283 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4626 : :
4627 : : /* [expr.shift] The behavior is undefined if the right operand
4628 : : is negative, or greater than or equal to the length in bits
4629 : : of the promoted left operand. */
4630 : 3084283 : if (tree_int_cst_sgn (rhs) == -1)
4631 : : {
4632 : 126 : if (!ctx->quiet)
4633 : 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4634 : : build2_loc (loc, code, type, lhs, rhs));
4635 : 126 : return (!flag_permissive || ctx->quiet);
4636 : : }
4637 : 3084157 : if (compare_tree_int (rhs, uprec) >= 0)
4638 : : {
4639 : 270 : if (!ctx->quiet)
4640 : 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4641 : : "than or equal to the precision %wu of the left operand",
4642 : : build2_loc (loc, code, type, lhs, rhs), uprec);
4643 : 270 : return (!flag_permissive || ctx->quiet);
4644 : : }
4645 : :
4646 : : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4647 : : if E1 has a signed type and non-negative value, and E1x2^E2 is
4648 : : representable in the corresponding unsigned type of the result type,
4649 : : then that value, converted to the result type, is the resulting value;
4650 : : otherwise, the behavior is undefined.
4651 : : For C++20:
4652 : : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4653 : : 2^N, where N is the range exponent of the type of the result. */
4654 : 3083887 : if (code == LSHIFT_EXPR
4655 : 2956100 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4656 : 1989745 : && cxx_dialect >= cxx11
4657 : 5056655 : && cxx_dialect < cxx20)
4658 : : {
4659 : 1455642 : if (tree_int_cst_sgn (lhs) == -1)
4660 : : {
4661 : 128 : if (!ctx->quiet)
4662 : 23 : permerror (loc,
4663 : : "left operand of shift expression %q+E is negative",
4664 : : build2_loc (loc, code, type, lhs, rhs));
4665 : 128 : return (!flag_permissive || ctx->quiet);
4666 : : }
4667 : : /* For signed x << y the following:
4668 : : (unsigned) x >> ((prec (lhs) - 1) - y)
4669 : : if > 1, is undefined. The right-hand side of this formula
4670 : : is the highest bit of the LHS that can be set (starting from 0),
4671 : : so that the shift doesn't overflow. We then right-shift the LHS
4672 : : to see whether any other bit is set making the original shift
4673 : : undefined -- the result is not representable in the corresponding
4674 : : unsigned type. */
4675 : 1455514 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
4676 : 1455514 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
4677 : 1455514 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
4678 : 1455514 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
4679 : 1455514 : if (tree_int_cst_lt (integer_one_node, t))
4680 : : {
4681 : 86 : if (!ctx->quiet)
4682 : 8 : permerror (loc, "shift expression %q+E overflows",
4683 : : build2_loc (loc, code, type, lhs, rhs));
4684 : 86 : return (!flag_permissive || ctx->quiet);
4685 : : }
4686 : : }
4687 : : return false;
4688 : : }
4689 : :
4690 : : /* Subroutine of cxx_eval_constant_expression.
4691 : : Attempt to reduce the unary expression tree T to a compile time value.
4692 : : If successful, return the value. Otherwise issue a diagnostic
4693 : : and return error_mark_node. */
4694 : :
4695 : : static tree
4696 : 20939000 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
4697 : : bool /*lval*/,
4698 : : bool *non_constant_p, bool *overflow_p,
4699 : : tree *jump_target)
4700 : : {
4701 : 20939000 : tree r;
4702 : 20939000 : tree orig_arg = TREE_OPERAND (t, 0);
4703 : 20939000 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
4704 : : non_constant_p, overflow_p,
4705 : : jump_target);
4706 : 20938999 : if (*jump_target)
4707 : : return NULL_TREE;
4708 : 20938998 : VERIFY_CONSTANT (arg);
4709 : 17323356 : location_t loc = EXPR_LOCATION (t);
4710 : 17323356 : enum tree_code code = TREE_CODE (t);
4711 : 17323356 : tree type = TREE_TYPE (t);
4712 : 17323356 : r = fold_unary_loc (loc, code, type, arg);
4713 : 17323356 : if (r == NULL_TREE)
4714 : : {
4715 : 17 : if (arg == orig_arg)
4716 : : r = t;
4717 : : else
4718 : 13 : r = build1_loc (loc, code, type, arg);
4719 : : }
4720 : 17323356 : VERIFY_CONSTANT (r);
4721 : : return r;
4722 : : }
4723 : :
4724 : : /* Helper function for cxx_eval_binary_expression. Try to optimize
4725 : : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
4726 : : generic folding should be used. */
4727 : :
4728 : : static tree
4729 : 2964657 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
4730 : : tree lhs, tree rhs, bool *non_constant_p,
4731 : : bool *overflow_p, tree *jump_target)
4732 : : {
4733 : 2964657 : STRIP_NOPS (lhs);
4734 : 2964657 : if (TREE_CODE (lhs) != ADDR_EXPR)
4735 : : return NULL_TREE;
4736 : :
4737 : 2618520 : lhs = TREE_OPERAND (lhs, 0);
4738 : :
4739 : : /* &A[i] p+ j => &A[i + j] */
4740 : 2618520 : if (TREE_CODE (lhs) == ARRAY_REF
4741 : 6858 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
4742 : 6858 : && TREE_CODE (rhs) == INTEGER_CST
4743 : 6858 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
4744 : 2625378 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
4745 : : {
4746 : 6858 : tree orig_type = TREE_TYPE (t);
4747 : 6858 : location_t loc = EXPR_LOCATION (t);
4748 : 6858 : tree type = TREE_TYPE (lhs);
4749 : :
4750 : 6858 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
4751 : 6858 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
4752 : 6858 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4753 : : non_constant_p, overflow_p,
4754 : : jump_target);
4755 : 6858 : if (*non_constant_p)
4756 : : return NULL_TREE;
4757 : 6846 : if (*jump_target)
4758 : : return NULL_TREE;
4759 : : /* Don't fold an out-of-bound access. */
4760 : 6846 : if (!tree_int_cst_le (t, nelts))
4761 : : return NULL_TREE;
4762 : 6846 : rhs = cp_fold_convert (ssizetype, rhs);
4763 : : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
4764 : : constexpr int A[1]; ... (char *)&A[0] + 1 */
4765 : 6846 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
4766 : 6846 : rhs, TYPE_SIZE_UNIT (type))))
4767 : : return NULL_TREE;
4768 : : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
4769 : : as signed. */
4770 : 6814 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
4771 : 6814 : TYPE_SIZE_UNIT (type));
4772 : 6814 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
4773 : 6814 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
4774 : : t, NULL_TREE, NULL_TREE);
4775 : 6814 : t = cp_build_addr_expr (t, tf_warning_or_error);
4776 : 6814 : t = cp_fold_convert (orig_type, t);
4777 : 6814 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
4778 : : non_constant_p, overflow_p,
4779 : 6814 : jump_target);
4780 : : }
4781 : :
4782 : : return NULL_TREE;
4783 : : }
4784 : :
4785 : : /* Try to fold expressions like
4786 : : (struct S *) (&a[0].D.2378 + 12)
4787 : : into
4788 : : &MEM <struct T> [(void *)&a + 12B]
4789 : : This is something normally done by gimple_fold_stmt_to_constant_1
4790 : : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
4791 : : dereference the address because some details are lost.
4792 : : For pointer comparisons we want such folding though so that
4793 : : match.pd address_compare optimization works. */
4794 : :
4795 : : static tree
4796 : 2648610 : cxx_maybe_fold_addr_pointer_plus (tree t)
4797 : : {
4798 : 5297220 : while (CONVERT_EXPR_P (t)
4799 : 2909506 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
4800 : 260896 : t = TREE_OPERAND (t, 0);
4801 : 2648610 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
4802 : : return NULL_TREE;
4803 : 2135797 : tree op0 = TREE_OPERAND (t, 0);
4804 : 2135797 : tree op1 = TREE_OPERAND (t, 1);
4805 : 2135797 : if (TREE_CODE (op1) != INTEGER_CST)
4806 : : return NULL_TREE;
4807 : 2135797 : while (CONVERT_EXPR_P (op0)
4808 : 4269409 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
4809 : 2133612 : op0 = TREE_OPERAND (op0, 0);
4810 : 2135797 : if (TREE_CODE (op0) != ADDR_EXPR)
4811 : : return NULL_TREE;
4812 : 2135797 : op1 = fold_convert (ptr_type_node, op1);
4813 : 2135797 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
4814 : 2135797 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
4815 : : }
4816 : :
4817 : : /* Subroutine of cxx_eval_constant_expression.
4818 : : Like cxx_eval_unary_expression, except for binary expressions. */
4819 : :
4820 : : static tree
4821 : 50311023 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
4822 : : value_cat lval,
4823 : : bool *non_constant_p, bool *overflow_p,
4824 : : tree *jump_target)
4825 : : {
4826 : 50311023 : tree r = NULL_TREE;
4827 : 50311023 : tree orig_lhs = TREE_OPERAND (t, 0);
4828 : 50311023 : tree orig_rhs = TREE_OPERAND (t, 1);
4829 : 50311023 : tree lhs, rhs;
4830 : 50311023 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
4831 : : non_constant_p, overflow_p,
4832 : : jump_target);
4833 : : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
4834 : : subtraction. */
4835 : 50311022 : if (*non_constant_p)
4836 : : return t;
4837 : 36998847 : if (*jump_target)
4838 : : return NULL_TREE;
4839 : 36998801 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
4840 : : non_constant_p, overflow_p,
4841 : : jump_target);
4842 : 36998801 : if (*non_constant_p)
4843 : : return t;
4844 : 36062392 : if (*jump_target)
4845 : : return NULL_TREE;
4846 : :
4847 : 36062387 : location_t loc = EXPR_LOCATION (t);
4848 : 36062387 : enum tree_code code = TREE_CODE (t);
4849 : 36062387 : tree type = TREE_TYPE (t);
4850 : :
4851 : 36062387 : if (code == EQ_EXPR || code == NE_EXPR)
4852 : : {
4853 : 4430012 : bool is_code_eq = (code == EQ_EXPR);
4854 : :
4855 : 4430012 : if (TREE_CODE (lhs) == PTRMEM_CST
4856 : 185 : && TREE_CODE (rhs) == PTRMEM_CST)
4857 : : {
4858 : 54 : tree lmem = PTRMEM_CST_MEMBER (lhs);
4859 : 54 : tree rmem = PTRMEM_CST_MEMBER (rhs);
4860 : 54 : bool eq;
4861 : 54 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
4862 : 54 : && TREE_CODE (lmem) == FIELD_DECL
4863 : 54 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
4864 : 81 : && same_type_p (DECL_CONTEXT (lmem),
4865 : : DECL_CONTEXT (rmem)))
4866 : : /* If both refer to (possibly different) members of the same union
4867 : : (12.3), they compare equal. */
4868 : : eq = true;
4869 : : else
4870 : 27 : eq = cp_tree_equal (lhs, rhs);
4871 : 54 : r = constant_boolean_node (eq == is_code_eq, type);
4872 : 54 : }
4873 : 4429958 : else if ((TREE_CODE (lhs) == PTRMEM_CST
4874 : 4429827 : || TREE_CODE (rhs) == PTRMEM_CST)
4875 : 4429958 : && (null_member_pointer_value_p (lhs)
4876 : 131 : || null_member_pointer_value_p (rhs)))
4877 : 131 : r = constant_boolean_node (!is_code_eq, type);
4878 : 4429827 : else if (TREE_CODE (lhs) == PTRMEM_CST)
4879 : 0 : lhs = cplus_expand_constant (lhs);
4880 : 4429827 : else if (TREE_CODE (rhs) == PTRMEM_CST)
4881 : 0 : rhs = cplus_expand_constant (rhs);
4882 : : }
4883 : 0 : if (r == NULL_TREE
4884 : 36062202 : && TREE_CODE_CLASS (code) == tcc_comparison
4885 : 12648181 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
4886 : : {
4887 : 1324305 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
4888 : 1036059 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
4889 : 1324305 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
4890 : 1099738 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
4891 : : }
4892 : 2964812 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
4893 : 39027199 : && integer_zerop (lhs) && !integer_zerop (rhs))
4894 : : {
4895 : 155 : if (!ctx->quiet)
4896 : 45 : error ("arithmetic involving a null pointer in %qE", lhs);
4897 : 155 : *non_constant_p = true;
4898 : 155 : return t;
4899 : : }
4900 : 36062232 : else if (code == POINTER_PLUS_EXPR)
4901 : : {
4902 : 2964657 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
4903 : : overflow_p, jump_target);
4904 : 2964657 : if (*jump_target)
4905 : : return NULL_TREE;
4906 : : }
4907 : 33097575 : else if (code == SPACESHIP_EXPR)
4908 : : {
4909 : 24873 : r = genericize_spaceship (loc, type, lhs, rhs);
4910 : 24873 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
4911 : 24873 : overflow_p, jump_target);
4912 : : }
4913 : :
4914 : 36037359 : if (r == NULL_TREE)
4915 : : {
4916 : 36030360 : if (ctx->manifestly_const_eval == mce_true
4917 : 21889714 : && (flag_constexpr_fp_except
4918 : 21889711 : || TREE_CODE (type) != REAL_TYPE))
4919 : : {
4920 : 21876610 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
4921 : 21876610 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
4922 : 21876610 : }
4923 : : else
4924 : 14153750 : r = fold_binary_loc (loc, code, type, lhs, rhs);
4925 : : }
4926 : :
4927 : 36030360 : if (r == NULL_TREE
4928 : 2349161 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
4929 : 100 : && TREE_CODE (lhs) == INTEGER_CST
4930 : 98 : && TREE_CODE (rhs) == INTEGER_CST
4931 : 38386520 : && wi::neg_p (wi::to_wide (rhs)))
4932 : : {
4933 : : /* For diagnostics and -fpermissive emulate previous behavior of
4934 : : handling shifts by negative amount. */
4935 : 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
4936 : 98 : if (nrhs)
4937 : 119 : r = fold_binary_loc (loc,
4938 : : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
4939 : : type, lhs, nrhs);
4940 : : }
4941 : :
4942 : 36037359 : if (r == NULL_TREE)
4943 : : {
4944 : 2349063 : if (lhs == orig_lhs && rhs == orig_rhs)
4945 : : r = t;
4946 : : else
4947 : 799060 : r = build2_loc (loc, code, type, lhs, rhs);
4948 : : }
4949 : 33688296 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
4950 : 600 : *non_constant_p = true;
4951 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4952 : : a local array in a constexpr function. */
4953 : 36037359 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
4954 : 31602068 : if (!ptr)
4955 : 31602068 : VERIFY_CONSTANT (r);
4956 : : return r;
4957 : : }
4958 : :
4959 : : /* Subroutine of cxx_eval_constant_expression.
4960 : : Attempt to evaluate condition expressions. */
4961 : :
4962 : : static tree
4963 : 9364825 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
4964 : : value_cat lval,
4965 : : bool *non_constant_p, bool *overflow_p,
4966 : : tree *jump_target)
4967 : : {
4968 : 9364825 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4969 : : vc_prvalue,
4970 : : non_constant_p, overflow_p,
4971 : : jump_target);
4972 : 9364825 : if (*jump_target)
4973 : : return NULL_TREE;
4974 : 9364816 : VERIFY_CONSTANT (val);
4975 : 7621104 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
4976 : : {
4977 : : /* Evaluate the condition as if it was
4978 : : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
4979 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
4980 : : without manifestly_const_eval even expressions or parts thereof which
4981 : : will later be manifestly const_eval evaluated), otherwise fold it to
4982 : : true. */
4983 : 1101230 : if (ctx->manifestly_const_eval == mce_unknown)
4984 : : {
4985 : 1091716 : *non_constant_p = true;
4986 : 1091716 : return t;
4987 : : }
4988 : 9514 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
4989 : : boolean_type_node);
4990 : : }
4991 : : /* Don't VERIFY_CONSTANT the other operands. */
4992 : 6529388 : const bool zero_p = integer_zerop (val);
4993 : 6529388 : if (zero_p)
4994 : 3716471 : val = TREE_OPERAND (t, 2);
4995 : : else
4996 : 2812917 : val = TREE_OPERAND (t, 1);
4997 : 6529388 : if (TREE_CODE (t) == IF_STMT && !val)
4998 : 1783099 : val = void_node;
4999 : :
5000 : : /* P2564: If we aren't in immediate function context (including a manifestly
5001 : : constant-evaluated expression), check any uses of immediate functions in
5002 : : the arm we're discarding. But don't do this inside a call; we already
5003 : : checked when parsing the function. */
5004 : 6529388 : if (ctx->manifestly_const_eval != mce_true
5005 : 1932024 : && !in_immediate_context ()
5006 : 1862671 : && !ctx->call
5007 : 7126140 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5008 : 596752 : ctx->manifestly_const_eval))
5009 : : {
5010 : 7 : *non_constant_p = true;
5011 : 7 : return t;
5012 : : }
5013 : :
5014 : : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5015 : : serve as the initializer for the same object as the outer TARGET_EXPR,
5016 : : as in
5017 : : A a = true ? A{} : A{};
5018 : : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5019 : 6529381 : if (TREE_CODE (val) == TARGET_EXPR)
5020 : 2433 : val = TARGET_EXPR_INITIAL (val);
5021 : 6529381 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5022 : 6529381 : overflow_p, jump_target);
5023 : : }
5024 : :
5025 : : /* Subroutine of cxx_eval_constant_expression.
5026 : : Attempt to evaluate vector condition expressions. Unlike
5027 : : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5028 : : ternary arithmetics operation, where all 3 arguments have to be
5029 : : evaluated as constants and then folding computes the result from
5030 : : them. */
5031 : :
5032 : : static tree
5033 : 866 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5034 : : bool *non_constant_p, bool *overflow_p,
5035 : : tree *jump_target)
5036 : : {
5037 : 866 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5038 : : vc_prvalue,
5039 : : non_constant_p, overflow_p,
5040 : : jump_target);
5041 : 866 : if (*jump_target)
5042 : : return NULL_TREE;
5043 : 866 : VERIFY_CONSTANT (arg1);
5044 : 657 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5045 : : vc_prvalue,
5046 : : non_constant_p, overflow_p,
5047 : : jump_target);
5048 : 657 : if (*jump_target)
5049 : : return NULL_TREE;
5050 : 657 : VERIFY_CONSTANT (arg2);
5051 : 657 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5052 : : vc_prvalue,
5053 : : non_constant_p, overflow_p,
5054 : : jump_target);
5055 : 657 : if (*jump_target)
5056 : : return NULL_TREE;
5057 : 657 : VERIFY_CONSTANT (arg3);
5058 : 657 : location_t loc = EXPR_LOCATION (t);
5059 : 657 : tree type = TREE_TYPE (t);
5060 : 657 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5061 : 657 : if (r == NULL_TREE)
5062 : : {
5063 : 0 : if (arg1 == TREE_OPERAND (t, 0)
5064 : 0 : && arg2 == TREE_OPERAND (t, 1)
5065 : 0 : && arg3 == TREE_OPERAND (t, 2))
5066 : : r = t;
5067 : : else
5068 : 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5069 : : }
5070 : 657 : VERIFY_CONSTANT (r);
5071 : : return r;
5072 : : }
5073 : :
5074 : : /* Returns less than, equal to, or greater than zero if KEY is found to be
5075 : : less than, to match, or to be greater than the constructor_elt's INDEX. */
5076 : :
5077 : : static int
5078 : 96928 : array_index_cmp (tree key, tree index)
5079 : : {
5080 : 96928 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5081 : :
5082 : 96928 : switch (TREE_CODE (index))
5083 : : {
5084 : 94059 : case INTEGER_CST:
5085 : 94059 : return tree_int_cst_compare (key, index);
5086 : 2869 : case RANGE_EXPR:
5087 : 2869 : {
5088 : 2869 : tree lo = TREE_OPERAND (index, 0);
5089 : 2869 : tree hi = TREE_OPERAND (index, 1);
5090 : 2869 : if (tree_int_cst_lt (key, lo))
5091 : : return -1;
5092 : 2583 : else if (tree_int_cst_lt (hi, key))
5093 : : return 1;
5094 : : else
5095 : 2583 : return 0;
5096 : : }
5097 : 0 : default:
5098 : 0 : gcc_unreachable ();
5099 : : }
5100 : : }
5101 : :
5102 : : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5103 : : relative index OFF. */
5104 : :
5105 : : static tree
5106 : 29084 : raw_data_cst_elt (tree raw_data, unsigned int off)
5107 : : {
5108 : 29084 : return build_int_cst (TREE_TYPE (raw_data),
5109 : 29084 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5110 : 58168 : ? (HOST_WIDE_INT)
5111 : 29084 : RAW_DATA_UCHAR_ELT (raw_data, off)
5112 : : : (HOST_WIDE_INT)
5113 : 0 : RAW_DATA_SCHAR_ELT (raw_data, off));
5114 : : }
5115 : :
5116 : : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5117 : : if none. If INSERT is true, insert a matching element rather than fail. */
5118 : :
5119 : : static HOST_WIDE_INT
5120 : 1897484 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5121 : : {
5122 : 1897484 : if (tree_int_cst_sgn (dindex) < 0)
5123 : : return -1;
5124 : :
5125 : 1897484 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5126 : 1897484 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5127 : 1897484 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5128 : :
5129 : 2829158 : unsigned HOST_WIDE_INT end = len;
5130 : 2829158 : unsigned HOST_WIDE_INT begin = 0;
5131 : :
5132 : : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5133 : : that the same is true of the other elements and index directly. */
5134 : 1786155 : if (end > 0)
5135 : : {
5136 : 1758745 : tree cindex = (*elts)[end - 1].index;
5137 : 1758745 : if (cindex == NULL_TREE)
5138 : : {
5139 : : /* Verify that if the last index is missing, all indexes
5140 : : are missing and there is no RAW_DATA_CST. */
5141 : 19033 : if (flag_checking)
5142 : 201926 : for (unsigned int j = 0; j < len - 1; ++j)
5143 : 182893 : gcc_assert ((*elts)[j].index == NULL_TREE
5144 : : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5145 : 19033 : if (i < end)
5146 : 19033 : return i;
5147 : : else
5148 : : {
5149 : 0 : begin = end;
5150 : 0 : if (i == end)
5151 : : /* If the element is to be added right at the end,
5152 : : make sure it is added with cleared index too. */
5153 : 1043003 : dindex = NULL_TREE;
5154 : 0 : else if (insert)
5155 : : /* Otherwise, in order not to break the assumption
5156 : : that CONSTRUCTOR either has all indexes or none,
5157 : : we need to add indexes to all elements. */
5158 : 0 : for (unsigned int j = 0; j < len; ++j)
5159 : 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5160 : : }
5161 : : }
5162 : 1739712 : else if (TREE_CODE (cindex) == INTEGER_CST
5163 : 1739712 : && compare_tree_int (cindex, end - 1) == 0)
5164 : : {
5165 : 1713519 : tree value = (*elts)[end - 1].value;
5166 : 1713519 : if (i < end)
5167 : : {
5168 : 835538 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5169 : : begin = end - 1;
5170 : : else
5171 : 835448 : return i;
5172 : : }
5173 : 877981 : else if (TREE_CODE (value) == RAW_DATA_CST
5174 : 910159 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5175 : 32178 : + RAW_DATA_LENGTH (value)))
5176 : 16089 : begin = end - 1;
5177 : : else
5178 : 861892 : begin = end;
5179 : : }
5180 : : }
5181 : :
5182 : : /* Otherwise, find a matching index by means of a binary search. */
5183 : 1085484 : while (begin != end)
5184 : : {
5185 : 76756 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5186 : 76756 : constructor_elt &elt = (*elts)[middle];
5187 : 76756 : tree idx = elt.index;
5188 : :
5189 : 76756 : int cmp = array_index_cmp (dindex, idx);
5190 : 76756 : if (cmp > 0
5191 : 56230 : && TREE_CODE (elt.value) == RAW_DATA_CST
5192 : 159630 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5193 : 82874 : + RAW_DATA_LENGTH (elt.value)))
5194 : 28950 : cmp = 0;
5195 : 76756 : if (cmp < 0)
5196 : : end = middle;
5197 : 61555 : else if (cmp > 0)
5198 : 27280 : begin = middle + 1;
5199 : : else
5200 : : {
5201 : 34275 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5202 : : {
5203 : : /* We need to split the RAW_DATA_CST elt. */
5204 : 122 : constructor_elt e;
5205 : 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5206 : 244 : unsigned int off = (wi::to_offset (dindex)
5207 : 244 : - wi::to_offset (idx)).to_uhwi ();
5208 : 122 : tree value = elt.value;
5209 : 122 : unsigned int len = RAW_DATA_LENGTH (value);
5210 : 122 : if (off > 1 && len >= off + 3)
5211 : 22 : value = copy_node (elt.value);
5212 : 122 : if (off)
5213 : : {
5214 : 33 : if (off > 1)
5215 : 33 : RAW_DATA_LENGTH (elt.value) = off;
5216 : : else
5217 : 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5218 : 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5219 : : build_int_cst (TREE_TYPE (elt.index),
5220 : : off));
5221 : 33 : e.value = NULL_TREE;
5222 : 33 : ++middle;
5223 : 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5224 : : }
5225 : 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5226 : 122 : if (len >= off + 2)
5227 : : {
5228 : 111 : e.index = (*elts)[middle].index;
5229 : 111 : e.index = size_binop (PLUS_EXPR, e.index,
5230 : : build_one_cst (TREE_TYPE (e.index)));
5231 : 111 : if (len >= off + 3)
5232 : : {
5233 : 111 : RAW_DATA_LENGTH (value) -= off + 1;
5234 : 111 : RAW_DATA_POINTER (value) += off + 1;
5235 : 111 : e.value = value;
5236 : : }
5237 : : else
5238 : 0 : e.value = raw_data_cst_elt (value, off + 1);
5239 : 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5240 : : }
5241 : 122 : return middle;
5242 : : }
5243 : 1298 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5244 : : {
5245 : : /* We need to split the range. */
5246 : 347 : constructor_elt e;
5247 : 347 : tree lo = TREE_OPERAND (idx, 0);
5248 : 347 : tree hi = TREE_OPERAND (idx, 1);
5249 : 347 : tree value = elt.value;
5250 : 347 : dindex = fold_convert (sizetype, dindex);
5251 : 347 : if (tree_int_cst_lt (lo, dindex))
5252 : : {
5253 : : /* There are still some lower elts; shorten the range. */
5254 : 178 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5255 : 89 : size_one_node);
5256 : 89 : if (tree_int_cst_equal (lo, new_hi))
5257 : : /* Only one element left, no longer a range. */
5258 : 37 : elt.index = lo;
5259 : : else
5260 : 52 : TREE_OPERAND (idx, 1) = new_hi;
5261 : : /* Append the element we want to insert. */
5262 : 89 : ++middle;
5263 : 89 : e.index = dindex;
5264 : 89 : e.value = unshare_constructor (value);
5265 : 89 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5266 : : }
5267 : : else
5268 : : /* No lower elts, the range elt is now ours. */
5269 : 258 : elt.index = dindex;
5270 : :
5271 : 347 : if (tree_int_cst_lt (dindex, hi))
5272 : : {
5273 : : /* There are still some higher elts; append a range. */
5274 : 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5275 : 239 : size_one_node);
5276 : 239 : if (tree_int_cst_equal (new_lo, hi))
5277 : 98 : e.index = hi;
5278 : : else
5279 : 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5280 : 239 : e.value = unshare_constructor (value);
5281 : 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5282 : : }
5283 : : }
5284 : 34153 : return middle;
5285 : : }
5286 : : }
5287 : :
5288 : 1008728 : if (insert)
5289 : : {
5290 : 1004906 : constructor_elt e = { dindex, NULL_TREE };
5291 : 1004906 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5292 : 1004906 : return end;
5293 : : }
5294 : :
5295 : : return -1;
5296 : : }
5297 : :
5298 : : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5299 : : matching constructor_elt exists, then add one to CTOR.
5300 : :
5301 : : As an optimization, if POS_HINT is non-negative then it is used as a guess
5302 : : for the (integer) index of the matching constructor_elt within CTOR.
5303 : :
5304 : : If POS_HINT is -2, it means do not insert. */
5305 : :
5306 : : static constructor_elt *
5307 : 11040342 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5308 : : {
5309 : : /* Check the hint first. */
5310 : 899478 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5311 : 11939820 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5312 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
5313 : :
5314 : 10140870 : bool insertp = (pos_hint != -2);
5315 : 10140870 : tree type = TREE_TYPE (ctor);
5316 : 10140870 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5317 : : {
5318 : 128 : gcc_assert (insertp);
5319 : 128 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5320 : 128 : return &CONSTRUCTOR_ELTS (ctor)->last();
5321 : : }
5322 : 10140742 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5323 : : {
5324 : 1391880 : if (TREE_CODE (index) == RANGE_EXPR)
5325 : : {
5326 : : /* Support for RANGE_EXPR index lookups is currently limited to
5327 : : accessing an existing element via POS_HINT, or appending a new
5328 : : element to the end of CTOR. ??? Support for other access
5329 : : patterns may also be needed. */
5330 : 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5331 : 3 : if (vec_safe_length (elts))
5332 : : {
5333 : 3 : tree lo = TREE_OPERAND (index, 0);
5334 : 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5335 : : }
5336 : 3 : gcc_assert (insertp);
5337 : 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5338 : 3 : return &elts->last();
5339 : : }
5340 : :
5341 : 1391877 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5342 : 1391877 : if (i < 0)
5343 : : {
5344 : 620 : gcc_assert (!insertp);
5345 : : return nullptr;
5346 : : }
5347 : 1391257 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5348 : 1391257 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5349 : : {
5350 : : /* Split a range even if we aren't inserting new entries. */
5351 : 0 : gcc_assert (!insertp);
5352 : 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5353 : 0 : gcc_assert (i >= 0);
5354 : 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5355 : : }
5356 : 1391257 : gcc_assert (cep->index == NULL_TREE
5357 : : || TREE_CODE (cep->index) != RANGE_EXPR);
5358 : : return cep;
5359 : : }
5360 : : else
5361 : : {
5362 : 8748862 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5363 : : && (same_type_ignoring_top_level_qualifiers_p
5364 : : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5365 : :
5366 : : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5367 : : Usually we meet initializers in that order, but it is
5368 : : possible for base types to be placed not in program
5369 : : order. */
5370 : 8748862 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5371 : 8748862 : unsigned HOST_WIDE_INT idx = 0;
5372 : 8748862 : constructor_elt *cep = NULL;
5373 : :
5374 : : /* Check if we're changing the active member of a union. */
5375 : 193200 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5376 : 8826233 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5377 : 2655 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5378 : : /* If the bit offset of INDEX is larger than that of the last
5379 : : constructor_elt, then we can just immediately append a new
5380 : : constructor_elt to the end of CTOR. */
5381 : 8746207 : else if (CONSTRUCTOR_NELTS (ctor)
5382 : 10302469 : && tree_int_cst_compare (bit_position (index),
5383 : 10025320 : bit_position (CONSTRUCTOR_ELTS (ctor)
5384 : 5012660 : ->last().index)) > 0)
5385 : : {
5386 : 1974702 : idx = CONSTRUCTOR_NELTS (ctor);
5387 : 1974702 : goto insert;
5388 : : }
5389 : :
5390 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5391 : : appropriately. */
5392 : :
5393 : 7922662 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5394 : 1148502 : idx++, fields = DECL_CHAIN (fields))
5395 : : {
5396 : 4186460 : if (index == cep->index)
5397 : 3017715 : goto found;
5398 : :
5399 : : /* The field we're initializing must be on the field
5400 : : list. Look to see if it is present before the
5401 : : field the current ELT initializes. */
5402 : 15082187 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5403 : 13933685 : if (index == fields)
5404 : 20243 : goto insert;
5405 : : }
5406 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
5407 : : entry at the end. */
5408 : :
5409 : 5731147 : insert:
5410 : 5731147 : if (!insertp)
5411 : : return nullptr;
5412 : :
5413 : 5731144 : {
5414 : 5731144 : constructor_elt ce = { index, NULL_TREE };
5415 : :
5416 : 5731144 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5417 : 5731144 : cep = CONSTRUCTOR_ELT (ctor, idx);
5418 : : }
5419 : 8748859 : found:;
5420 : :
5421 : 8748859 : return cep;
5422 : : }
5423 : : }
5424 : :
5425 : : /* Under the control of CTX, issue a detailed diagnostic for
5426 : : an out-of-bounds subscript INDEX into the expression ARRAY. */
5427 : :
5428 : : static void
5429 : 558 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5430 : : {
5431 : 558 : if (!ctx->quiet)
5432 : : {
5433 : 118 : tree arraytype = TREE_TYPE (array);
5434 : :
5435 : : /* Convert the unsigned array subscript to a signed integer to avoid
5436 : : printing huge numbers for small negative values. */
5437 : 118 : tree sidx = fold_convert (ssizetype, index);
5438 : 118 : STRIP_ANY_LOCATION_WRAPPER (array);
5439 : 118 : if (DECL_P (array))
5440 : : {
5441 : 75 : auto_diagnostic_group d;
5442 : 75 : if (TYPE_DOMAIN (arraytype))
5443 : 69 : error_at (loc, "array subscript value %qE is outside the bounds "
5444 : : "of array %qD of type %qT", sidx, array, arraytype);
5445 : : else
5446 : 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5447 : : "type %qT with unknown bounds", sidx, array, arraytype);
5448 : 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5449 : 75 : }
5450 : 43 : else if (TYPE_DOMAIN (arraytype))
5451 : 40 : error_at (loc, "array subscript value %qE is outside the bounds "
5452 : : "of array type %qT", sidx, arraytype);
5453 : : else
5454 : 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5455 : : "with unknown bounds", sidx, arraytype);
5456 : : }
5457 : 558 : }
5458 : :
5459 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5460 : : a VECTOR_TYPE). */
5461 : :
5462 : : static tree
5463 : 4095348 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5464 : : bool *non_constant_p, bool *overflow_p,
5465 : : tree *jump_target)
5466 : : {
5467 : 4095348 : tree nelts;
5468 : 4095348 : if (TREE_CODE (type) == ARRAY_TYPE)
5469 : : {
5470 : 4095348 : if (TYPE_DOMAIN (type))
5471 : 4095112 : nelts = array_type_nelts_top (type);
5472 : : else
5473 : 236 : nelts = size_zero_node;
5474 : : }
5475 : 0 : else if (VECTOR_TYPE_P (type))
5476 : 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5477 : : else
5478 : 0 : gcc_unreachable ();
5479 : :
5480 : : /* For VLAs, the number of elements won't be an integer constant. */
5481 : 4095348 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5482 : : non_constant_p, overflow_p,
5483 : : jump_target);
5484 : 4095348 : return nelts;
5485 : : }
5486 : :
5487 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5488 : : STRING_CST STRING. */
5489 : :
5490 : : static tree
5491 : 1060940 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5492 : : {
5493 : 1060940 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5494 : 1060940 : tree r;
5495 : :
5496 : 1060940 : if (chars_per_elt == 1)
5497 : 797283 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5498 : : else
5499 : : {
5500 : 263657 : const unsigned char *ptr
5501 : 263657 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5502 : 263657 : + index * chars_per_elt);
5503 : 263657 : r = native_interpret_expr (type, ptr, chars_per_elt);
5504 : : }
5505 : 1060940 : return r;
5506 : : }
5507 : :
5508 : : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5509 : : subscript, diagnose any problems with it, and return the result. */
5510 : :
5511 : : static tree
5512 : 4096406 : eval_and_check_array_index (const constexpr_ctx *ctx,
5513 : : tree t, bool allow_one_past,
5514 : : bool *non_constant_p, bool *overflow_p,
5515 : : tree *jump_target)
5516 : : {
5517 : 4096406 : location_t loc = cp_expr_loc_or_input_loc (t);
5518 : 4096406 : tree ary = TREE_OPERAND (t, 0);
5519 : 4096406 : t = TREE_OPERAND (t, 1);
5520 : 4096406 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5521 : : non_constant_p, overflow_p,
5522 : : jump_target);
5523 : 4096406 : if (*jump_target)
5524 : : return NULL_TREE;
5525 : 4096406 : VERIFY_CONSTANT (index);
5526 : :
5527 : 4094831 : if (!tree_fits_shwi_p (index)
5528 : 4094831 : || tree_int_cst_sgn (index) < 0)
5529 : : {
5530 : 108 : diag_array_subscript (loc, ctx, ary, index);
5531 : 108 : *non_constant_p = true;
5532 : 108 : return t;
5533 : : }
5534 : :
5535 : 4094723 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5536 : : overflow_p, jump_target);
5537 : 4094723 : if (*jump_target)
5538 : : return NULL_TREE;
5539 : 4094723 : VERIFY_CONSTANT (nelts);
5540 : 4094660 : if (allow_one_past
5541 : 4094660 : ? !tree_int_cst_le (index, nelts)
5542 : 2864508 : : !tree_int_cst_lt (index, nelts))
5543 : : {
5544 : 450 : diag_array_subscript (loc, ctx, ary, index);
5545 : 450 : *non_constant_p = true;
5546 : 450 : return t;
5547 : : }
5548 : :
5549 : : return index;
5550 : : }
5551 : :
5552 : : /* Subroutine of cxx_eval_constant_expression.
5553 : : Attempt to reduce a reference to an array slot. */
5554 : :
5555 : : static tree
5556 : 2989813 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5557 : : value_cat lval,
5558 : : bool *non_constant_p, bool *overflow_p,
5559 : : tree *jump_target)
5560 : : {
5561 : 2989813 : tree oldary = TREE_OPERAND (t, 0);
5562 : 2989813 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5563 : : lval,
5564 : : non_constant_p, overflow_p,
5565 : : jump_target);
5566 : 2989813 : if (*non_constant_p)
5567 : : return t;
5568 : 2806690 : if (*jump_target)
5569 : : return NULL_TREE;
5570 : 2806690 : if (!lval
5571 : 1575318 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5572 : 13228 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5573 : 2819918 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5574 : 13228 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5575 : 13228 : ary = TREE_OPERAND (ary, 0);
5576 : :
5577 : 2806690 : tree oldidx = TREE_OPERAND (t, 1);
5578 : 2806690 : tree index = eval_and_check_array_index (ctx, t, lval,
5579 : : non_constant_p, overflow_p,
5580 : : jump_target);
5581 : 2806690 : if (*non_constant_p)
5582 : : return t;
5583 : 2804560 : if (*jump_target)
5584 : : return NULL_TREE;
5585 : :
5586 : 2804560 : if (lval && ary == oldary && index == oldidx)
5587 : : return t;
5588 : 1700673 : else if (lval == vc_discard)
5589 : : return t;
5590 : 1700673 : else if (lval)
5591 : 126025 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5592 : :
5593 : 1574648 : unsigned len = 0, elem_nchars = 1;
5594 : 1574648 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5595 : 1574648 : if (TREE_CODE (ary) == CONSTRUCTOR)
5596 : 504197 : len = CONSTRUCTOR_NELTS (ary);
5597 : 1070451 : else if (TREE_CODE (ary) == STRING_CST)
5598 : : {
5599 : 1057231 : elem_nchars = (TYPE_PRECISION (elem_type)
5600 : 1057231 : / TYPE_PRECISION (char_type_node));
5601 : 1057231 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5602 : : }
5603 : 13220 : else if (TREE_CODE (ary) == VECTOR_CST)
5604 : : /* We don't create variable-length VECTOR_CSTs. */
5605 : 13220 : len = VECTOR_CST_NELTS (ary).to_constant ();
5606 : : else
5607 : : {
5608 : : /* We can't do anything with other tree codes, so use
5609 : : VERIFY_CONSTANT to complain and fail. */
5610 : 0 : VERIFY_CONSTANT (ary);
5611 : 0 : gcc_unreachable ();
5612 : : }
5613 : :
5614 : 1574648 : bool found;
5615 : 1574648 : HOST_WIDE_INT i = 0;
5616 : 1574648 : if (TREE_CODE (ary) == CONSTRUCTOR)
5617 : : {
5618 : 504197 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5619 : 504197 : found = (ix >= 0);
5620 : 504197 : if (found)
5621 : 501081 : i = ix;
5622 : : }
5623 : : else
5624 : : {
5625 : 1070451 : i = tree_to_shwi (index);
5626 : 1070451 : found = (i < len);
5627 : : }
5628 : :
5629 : 1574648 : if (found)
5630 : : {
5631 : 1571093 : tree r;
5632 : 1571093 : if (TREE_CODE (ary) == CONSTRUCTOR)
5633 : : {
5634 : 501081 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5635 : 501081 : if (TREE_CODE (r) == RAW_DATA_CST)
5636 : : {
5637 : 28962 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5638 : 28962 : gcc_checking_assert (ridx);
5639 : 28962 : unsigned int off
5640 : 28962 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5641 : 28962 : r = raw_data_cst_elt (r, off);
5642 : : }
5643 : : }
5644 : 1070012 : else if (TREE_CODE (ary) == VECTOR_CST)
5645 : 13220 : r = VECTOR_CST_ELT (ary, i);
5646 : : else
5647 : 1056792 : r = extract_string_elt (ary, elem_nchars, i);
5648 : :
5649 : 1098974 : if (r)
5650 : : /* Don't VERIFY_CONSTANT here. */
5651 : 1571093 : return r;
5652 : :
5653 : : /* Otherwise the element doesn't have a value yet. */
5654 : : }
5655 : :
5656 : : /* Not found. */
5657 : :
5658 : 3555 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
5659 : 49 : return build_constructor (elem_type, NULL);
5660 : :
5661 : 3506 : if (TREE_CODE (ary) == CONSTRUCTOR
5662 : 3506 : && CONSTRUCTOR_NO_CLEARING (ary))
5663 : : {
5664 : : /* 'ary' is part of the aggregate initializer we're currently
5665 : : building; if there's no initializer for this element yet,
5666 : : that's an error. */
5667 : 51 : if (!ctx->quiet)
5668 : 16 : error ("accessing uninitialized array element");
5669 : 51 : *non_constant_p = true;
5670 : 51 : return t;
5671 : : }
5672 : :
5673 : : /* If it's within the array bounds but doesn't have an explicit
5674 : : initializer, it's initialized from {}. But use build_value_init
5675 : : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
5676 : 3455 : tree val;
5677 : 3455 : constexpr_ctx new_ctx;
5678 : 3455 : if (CP_AGGREGATE_TYPE_P (elem_type))
5679 : : {
5680 : 480 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
5681 : 480 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
5682 : : }
5683 : : else
5684 : 2975 : val = build_value_init (elem_type, tf_warning_or_error);
5685 : :
5686 : : /* Create a new constructor only if we don't already have a suitable one. */
5687 : 3455 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
5688 : 3967 : && (!ctx->ctor
5689 : 45 : || !same_type_ignoring_top_level_qualifiers_p
5690 : 45 : (elem_type, TREE_TYPE (ctx->ctor))));
5691 : 503 : if (new_ctor)
5692 : : {
5693 : 503 : new_ctx = *ctx;
5694 : : /* We clear the object here. We used to replace it with T, but that
5695 : : caused problems (101371, 108158); and anyway, T is the initializer,
5696 : : not the target object. */
5697 : 503 : new_ctx.object = NULL_TREE;
5698 : 503 : new_ctx.ctor = build_constructor (elem_type, NULL);
5699 : 503 : ctx = &new_ctx;
5700 : : }
5701 : 3455 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5702 : : overflow_p, jump_target);
5703 : 3455 : if (new_ctor && t != ctx->ctor)
5704 : 482 : free_constructor (ctx->ctor);
5705 : : return t;
5706 : : }
5707 : :
5708 : : /* Subroutine of cxx_eval_constant_expression.
5709 : : Attempt to reduce a field access of a value of class type. */
5710 : :
5711 : : static tree
5712 : 34878680 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
5713 : : value_cat lval,
5714 : : bool *non_constant_p, bool *overflow_p,
5715 : : tree *jump_target)
5716 : : {
5717 : 34878680 : unsigned HOST_WIDE_INT i;
5718 : 34878680 : tree field;
5719 : 34878680 : tree value;
5720 : 34878680 : tree part = TREE_OPERAND (t, 1);
5721 : 34878680 : tree orig_whole = TREE_OPERAND (t, 0);
5722 : 34878680 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5723 : : lval,
5724 : : non_constant_p, overflow_p,
5725 : : jump_target);
5726 : 34878680 : if (*non_constant_p)
5727 : : return t;
5728 : 19612107 : if (*jump_target)
5729 : : return NULL_TREE;
5730 : 19612107 : if (INDIRECT_REF_P (whole)
5731 : 19612107 : && integer_zerop (TREE_OPERAND (whole, 0)))
5732 : : {
5733 : 177 : if (!ctx->quiet)
5734 : 39 : error ("dereferencing a null pointer in %qE", orig_whole);
5735 : 177 : *non_constant_p = true;
5736 : 177 : return t;
5737 : : }
5738 : :
5739 : 19611930 : if (TREE_CODE (whole) == PTRMEM_CST)
5740 : 1310 : whole = cplus_expand_constant (whole);
5741 : 19611930 : if (whole == orig_whole)
5742 : : return t;
5743 : 13146517 : if (lval == vc_discard)
5744 : : return t;
5745 : 13146493 : if (lval)
5746 : 5122937 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
5747 : : whole, part, NULL_TREE);
5748 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
5749 : : CONSTRUCTOR. */
5750 : 8023556 : if (TREE_CODE (whole) != CONSTRUCTOR)
5751 : : {
5752 : 0 : if (!ctx->quiet)
5753 : 0 : error ("%qE is not a constant expression", orig_whole);
5754 : 0 : *non_constant_p = true;
5755 : 0 : return t;
5756 : : }
5757 : 8022087 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
5758 : 8025195 : && DECL_MUTABLE_P (part))
5759 : : {
5760 : 132 : if (!ctx->quiet)
5761 : 16 : error ("mutable %qD is not usable in a constant expression", part);
5762 : 132 : *non_constant_p = true;
5763 : 132 : return t;
5764 : : }
5765 : :
5766 : 8023424 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
5767 : 10913791 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
5768 : : {
5769 : : /* Use name match for PMF fields, as a variant will have a
5770 : : different FIELD_DECL with a different type. */
5771 : 10909090 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
5772 : : : field == part)
5773 : : {
5774 : 8018723 : if (value == void_node)
5775 : 3 : goto uninit;
5776 : 8018720 : if (value)
5777 : : {
5778 : 8018702 : STRIP_ANY_LOCATION_WRAPPER (value);
5779 : 8018702 : return value;
5780 : : }
5781 : : else
5782 : : /* We're in the middle of initializing it. */
5783 : : break;
5784 : : }
5785 : : }
5786 : 4719 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
5787 : : {
5788 : 89 : if (CONSTRUCTOR_NELTS (whole) > 0)
5789 : : {
5790 : : /* DR 1188 says we don't have to deal with this. */
5791 : 74 : if (!ctx->quiet)
5792 : : {
5793 : 19 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
5794 : 19 : if (cep->value == NULL_TREE)
5795 : 9 : error ("accessing uninitialized member %qD", part);
5796 : : else
5797 : 10 : error ("accessing %qD member instead of active %qD member "
5798 : : "in constant expression", part, cep->index);
5799 : : }
5800 : 74 : *non_constant_p = true;
5801 : 74 : return t;
5802 : : }
5803 : 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
5804 : : {
5805 : : /* Value-initialized union, check if looking at the first member. */
5806 : 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
5807 : 12 : if (first != part)
5808 : : {
5809 : 9 : if (!ctx->quiet)
5810 : 3 : error ("accessing %qD member instead of initialized %qD "
5811 : : "member in constant expression", part, first);
5812 : 9 : *non_constant_p = true;
5813 : 9 : return t;
5814 : : }
5815 : : }
5816 : : }
5817 : :
5818 : : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
5819 : : classes never get represented; throw together a value now. */
5820 : 4636 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
5821 : 3551 : return build_constructor (TREE_TYPE (t), NULL);
5822 : :
5823 : 1085 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
5824 : :
5825 : 1085 : if (CONSTRUCTOR_NO_CLEARING (whole))
5826 : : {
5827 : 96 : uninit:
5828 : : /* 'whole' is part of the aggregate initializer we're currently
5829 : : building; if there's no initializer for this member yet, that's an
5830 : : error. */
5831 : 99 : if (!ctx->quiet)
5832 : 22 : error ("accessing uninitialized member %qD", part);
5833 : 99 : *non_constant_p = true;
5834 : 99 : return t;
5835 : : }
5836 : :
5837 : : /* If there's no explicit init for this field, it's value-initialized. */
5838 : :
5839 : 989 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
5840 : : {
5841 : : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
5842 : : and copy the flags. */
5843 : 128 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
5844 : 128 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
5845 : 128 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
5846 : 128 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
5847 : 128 : return value;
5848 : : }
5849 : :
5850 : 861 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
5851 : 861 : return cxx_eval_constant_expression (ctx, value,
5852 : : lval,
5853 : : non_constant_p, overflow_p,
5854 : 861 : jump_target);
5855 : : }
5856 : :
5857 : : /* Subroutine of cxx_eval_constant_expression.
5858 : : Attempt to reduce a field access of a value of class type that is
5859 : : expressed as a BIT_FIELD_REF. */
5860 : :
5861 : : static tree
5862 : 2885 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
5863 : : value_cat lval,
5864 : : bool *non_constant_p, bool *overflow_p,
5865 : : tree *jump_target)
5866 : : {
5867 : 2885 : tree orig_whole = TREE_OPERAND (t, 0);
5868 : 2885 : tree retval, fldval, utype, mask;
5869 : 2885 : bool fld_seen = false;
5870 : 2885 : HOST_WIDE_INT istart, isize;
5871 : 2885 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5872 : : lval,
5873 : : non_constant_p, overflow_p,
5874 : : jump_target);
5875 : 2885 : tree start, field, value;
5876 : 2885 : unsigned HOST_WIDE_INT i;
5877 : :
5878 : 2885 : if (*jump_target)
5879 : : return NULL_TREE;
5880 : 2885 : if (whole == orig_whole)
5881 : : return t;
5882 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
5883 : : CONSTRUCTOR. */
5884 : 2602 : if (!*non_constant_p
5885 : 2602 : && TREE_CODE (whole) != VECTOR_CST
5886 : 2600 : && TREE_CODE (whole) != CONSTRUCTOR)
5887 : : {
5888 : 0 : if (!ctx->quiet)
5889 : 0 : error ("%qE is not a constant expression", orig_whole);
5890 : 0 : *non_constant_p = true;
5891 : : }
5892 : 2602 : if (*non_constant_p)
5893 : : return t;
5894 : :
5895 : 2602 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5896 : : {
5897 : 2 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
5898 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
5899 : : return r;
5900 : 0 : if (!ctx->quiet)
5901 : 0 : error ("%qE is not a constant expression", orig_whole);
5902 : 0 : *non_constant_p = true;
5903 : 0 : return t;
5904 : : }
5905 : :
5906 : 2600 : start = TREE_OPERAND (t, 2);
5907 : 2600 : istart = tree_to_shwi (start);
5908 : 2600 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
5909 : 2600 : utype = TREE_TYPE (t);
5910 : 2600 : if (!TYPE_UNSIGNED (utype))
5911 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
5912 : 2600 : retval = build_int_cst (utype, 0);
5913 : 36400 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
5914 : : {
5915 : 33800 : tree bitpos = bit_position (field);
5916 : 33800 : STRIP_ANY_LOCATION_WRAPPER (value);
5917 : 33800 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
5918 : : return value;
5919 : 33800 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
5920 : 33800 : && TREE_CODE (value) == INTEGER_CST
5921 : 33800 : && tree_fits_shwi_p (bitpos)
5922 : 67600 : && tree_fits_shwi_p (DECL_SIZE (field)))
5923 : : {
5924 : 33800 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
5925 : 33800 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
5926 : 33800 : HOST_WIDE_INT shift;
5927 : 33800 : if (bit >= istart && bit + sz <= istart + isize)
5928 : : {
5929 : 7800 : fldval = fold_convert (utype, value);
5930 : 7800 : mask = build_int_cst_type (utype, -1);
5931 : 7800 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
5932 : : size_int (TYPE_PRECISION (utype) - sz));
5933 : 7800 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
5934 : : size_int (TYPE_PRECISION (utype) - sz));
5935 : 7800 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
5936 : 7800 : shift = bit - istart;
5937 : 7800 : if (BYTES_BIG_ENDIAN)
5938 : : shift = TYPE_PRECISION (utype) - shift - sz;
5939 : 7800 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
5940 : : size_int (shift));
5941 : 7800 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
5942 : 7800 : fld_seen = true;
5943 : : }
5944 : : }
5945 : : }
5946 : 2600 : if (fld_seen)
5947 : 2600 : return fold_convert (TREE_TYPE (t), retval);
5948 : 0 : gcc_unreachable ();
5949 : : return error_mark_node;
5950 : : }
5951 : :
5952 : : /* Helper for cxx_eval_bit_cast.
5953 : : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
5954 : : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
5955 : : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
5956 : : data members of reference type. */
5957 : :
5958 : : static bool
5959 : 4850 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
5960 : : tree orig_type)
5961 : : {
5962 : 5343 : if (TREE_CODE (type) == UNION_TYPE)
5963 : : {
5964 : 63 : if (!ctx->quiet)
5965 : : {
5966 : 21 : if (type == orig_type)
5967 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5968 : : "a union type", "__builtin_bit_cast", type);
5969 : : else
5970 : 15 : error_at (loc, "%qs is not a constant expression because %qT "
5971 : : "contains a union type", "__builtin_bit_cast",
5972 : : orig_type);
5973 : : }
5974 : 63 : return true;
5975 : : }
5976 : : if (TREE_CODE (type) == POINTER_TYPE)
5977 : : {
5978 : 60 : if (!ctx->quiet)
5979 : : {
5980 : 18 : if (type == orig_type)
5981 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5982 : : "a pointer type", "__builtin_bit_cast", type);
5983 : : else
5984 : 12 : error_at (loc, "%qs is not a constant expression because %qT "
5985 : : "contains a pointer type", "__builtin_bit_cast",
5986 : : orig_type);
5987 : : }
5988 : 60 : return true;
5989 : : }
5990 : : if (TREE_CODE (type) == REFERENCE_TYPE)
5991 : : {
5992 : 0 : if (!ctx->quiet)
5993 : : {
5994 : 0 : if (type == orig_type)
5995 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5996 : : "a reference type", "__builtin_bit_cast", type);
5997 : : else
5998 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5999 : : "contains a reference type", "__builtin_bit_cast",
6000 : : orig_type);
6001 : : }
6002 : 0 : return true;
6003 : : }
6004 : 1373 : if (TYPE_PTRMEM_P (type))
6005 : : {
6006 : 36 : if (!ctx->quiet)
6007 : : {
6008 : 12 : if (type == orig_type)
6009 : 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6010 : : "a pointer to member type", "__builtin_bit_cast",
6011 : : type);
6012 : : else
6013 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
6014 : : "contains a pointer to member type",
6015 : : "__builtin_bit_cast", orig_type);
6016 : : }
6017 : 36 : return true;
6018 : : }
6019 : 5184 : if (TYPE_VOLATILE (type))
6020 : : {
6021 : 0 : if (!ctx->quiet)
6022 : : {
6023 : 0 : if (type == orig_type)
6024 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6025 : : "volatile", "__builtin_bit_cast", type);
6026 : : else
6027 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
6028 : : "contains a volatile subobject",
6029 : : "__builtin_bit_cast", orig_type);
6030 : : }
6031 : 0 : return true;
6032 : : }
6033 : 5184 : if (TREE_CODE (type) == RECORD_TYPE)
6034 : 27210 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6035 : 25945 : if (TREE_CODE (field) == FIELD_DECL
6036 : 25945 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6037 : : return true;
6038 : 5094 : if (TREE_CODE (type) == ARRAY_TYPE)
6039 : 493 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6040 : : return false;
6041 : : }
6042 : :
6043 : : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6044 : : std::byte members of CONSTRUCTOR (recursively) if they contain
6045 : : some indeterminate bits (as set in MASK), remove the ctor elts,
6046 : : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6047 : : bits in MASK. */
6048 : :
6049 : : static void
6050 : 609 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6051 : : {
6052 : 609 : if (TREE_CODE (t) != CONSTRUCTOR)
6053 : : return;
6054 : :
6055 : : unsigned i, j = 0;
6056 : : tree index, value;
6057 : 2102 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6058 : : {
6059 : 1493 : tree type = TREE_TYPE (value);
6060 : 1493 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6061 : 2547 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6062 : : {
6063 : 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6064 : : {
6065 : 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6066 : 135 : gcc_assert (fldsz != 0);
6067 : 135 : HOST_WIDE_INT pos = int_byte_position (index);
6068 : 135 : HOST_WIDE_INT bpos
6069 : 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6070 : 135 : bpos %= BITS_PER_UNIT;
6071 : 135 : HOST_WIDE_INT end
6072 : 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6073 : 135 : gcc_assert (end == 1 || end == 2);
6074 : 135 : unsigned char *p = mask + pos;
6075 : 135 : unsigned char mask_save[2];
6076 : 135 : mask_save[0] = mask[pos];
6077 : 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6078 : 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6079 : : sorry_at (loc, "PDP11 bit-field handling unsupported"
6080 : : " in %qs", "__builtin_bit_cast");
6081 : 135 : else if (BYTES_BIG_ENDIAN)
6082 : : {
6083 : : /* Big endian. */
6084 : : if (bpos + fldsz <= BITS_PER_UNIT)
6085 : : *p &= ~(((1 << fldsz) - 1)
6086 : : << (BITS_PER_UNIT - bpos - fldsz));
6087 : : else
6088 : : {
6089 : : gcc_assert (bpos);
6090 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6091 : : p++;
6092 : : fldsz -= BITS_PER_UNIT - bpos;
6093 : : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6094 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6095 : : }
6096 : : }
6097 : : else
6098 : : {
6099 : : /* Little endian. */
6100 : 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6101 : 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6102 : : else
6103 : : {
6104 : 0 : gcc_assert (bpos);
6105 : 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6106 : 0 : p++;
6107 : 0 : fldsz -= BITS_PER_UNIT - bpos;
6108 : 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6109 : 0 : *p &= ~((1 << fldsz) - 1);
6110 : : }
6111 : : }
6112 : 135 : if (mask_save[0] != mask[pos]
6113 : 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6114 : : {
6115 : 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6116 : 36 : continue;
6117 : : }
6118 : : }
6119 : : }
6120 : 1223 : else if (is_byte_access_type_not_plain_char (type))
6121 : : {
6122 : 228 : HOST_WIDE_INT pos;
6123 : 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6124 : 132 : pos = tree_to_shwi (index);
6125 : : else
6126 : 96 : pos = int_byte_position (index);
6127 : 228 : if (mask[pos])
6128 : : {
6129 : 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6130 : 48 : mask[pos] = 0;
6131 : 48 : continue;
6132 : : }
6133 : : }
6134 : 1409 : if (TREE_CODE (value) == CONSTRUCTOR)
6135 : : {
6136 : 264 : HOST_WIDE_INT pos;
6137 : 264 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6138 : 144 : pos = tree_to_shwi (index)
6139 : 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6140 : : else
6141 : 192 : pos = int_byte_position (index);
6142 : 264 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6143 : : }
6144 : 1409 : if (i != j)
6145 : : {
6146 : 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6147 : 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6148 : : }
6149 : 1409 : ++j;
6150 : : }
6151 : 1218 : if (CONSTRUCTOR_NELTS (t) != j)
6152 : 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6153 : : }
6154 : :
6155 : : /* Subroutine of cxx_eval_constant_expression.
6156 : : Attempt to evaluate a BIT_CAST_EXPR. */
6157 : :
6158 : : static tree
6159 : 1031 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6160 : : bool *overflow_p, tree *jump_target)
6161 : : {
6162 : 1031 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6163 : 1031 : TREE_TYPE (t))
6164 : 3349 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6165 : 950 : EXPR_LOCATION (t)),
6166 : 950 : TREE_TYPE (TREE_OPERAND (t, 0)),
6167 : 950 : TREE_TYPE (TREE_OPERAND (t, 0))))
6168 : : {
6169 : 159 : *non_constant_p = true;
6170 : 159 : return t;
6171 : : }
6172 : :
6173 : 872 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6174 : : non_constant_p, overflow_p,
6175 : : jump_target);
6176 : 872 : if (*non_constant_p)
6177 : : return t;
6178 : 776 : if (*jump_target)
6179 : : return NULL_TREE;
6180 : :
6181 : 776 : location_t loc = EXPR_LOCATION (t);
6182 : 776 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6183 : : {
6184 : : if (!ctx->quiet)
6185 : : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6186 : : "__builtin_bit_cast");
6187 : : *non_constant_p = true;
6188 : : return t;
6189 : : }
6190 : :
6191 : 776 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6192 : : {
6193 : 0 : if (!ctx->quiet)
6194 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6195 : : "type is too large", "__builtin_bit_cast");
6196 : 0 : *non_constant_p = true;
6197 : 0 : return t;
6198 : : }
6199 : :
6200 : 776 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6201 : 776 : if (len < 0 || (int) len != len)
6202 : : {
6203 : 0 : if (!ctx->quiet)
6204 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6205 : : "type is too large", "__builtin_bit_cast");
6206 : 0 : *non_constant_p = true;
6207 : 0 : return t;
6208 : : }
6209 : :
6210 : 776 : unsigned char buf[64];
6211 : 776 : unsigned char *ptr, *mask;
6212 : 776 : size_t alen = (size_t) len * 2;
6213 : 776 : if (alen <= sizeof (buf))
6214 : : ptr = buf;
6215 : : else
6216 : 3 : ptr = XNEWVEC (unsigned char, alen);
6217 : 776 : mask = ptr + (size_t) len;
6218 : : /* At the beginning consider everything indeterminate. */
6219 : 776 : memset (mask, ~0, (size_t) len);
6220 : :
6221 : 776 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6222 : : {
6223 : 0 : if (!ctx->quiet)
6224 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6225 : : "argument cannot be encoded", "__builtin_bit_cast");
6226 : 0 : *non_constant_p = true;
6227 : 0 : if (ptr != buf)
6228 : 0 : XDELETE (ptr);
6229 : 0 : return t;
6230 : : }
6231 : :
6232 : 776 : tree r = NULL_TREE;
6233 : 776 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6234 : : {
6235 : 431 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6236 : 431 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6237 : : {
6238 : 46 : gcc_assert (len == 1);
6239 : 46 : if (mask[0])
6240 : : {
6241 : 24 : memset (mask, 0, len);
6242 : 24 : r = build_constructor (TREE_TYPE (r), NULL);
6243 : 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6244 : : }
6245 : : }
6246 : : }
6247 : 345 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6248 : : {
6249 : 345 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6250 : 345 : if (r != NULL_TREE)
6251 : : {
6252 : 345 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6253 : 345 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6254 : 345 : if (CHECKING_P)
6255 : : {
6256 : 345 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6257 : : non_constant_p, overflow_p,
6258 : : jump_target);
6259 : 345 : gcc_checking_assert (e == r && !*jump_target);
6260 : : r = e;
6261 : : }
6262 : : }
6263 : : }
6264 : :
6265 : 776 : if (r != NULL_TREE)
6266 : : {
6267 : 6223 : for (int i = 0; i < len; i++)
6268 : 5537 : if (mask[i])
6269 : : {
6270 : 90 : if (!ctx->quiet)
6271 : 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6272 : : "__builtin_bit_cast", i);
6273 : 90 : *non_constant_p = true;
6274 : 90 : r = t;
6275 : 90 : break;
6276 : : }
6277 : 776 : if (ptr != buf)
6278 : 3 : XDELETE (ptr);
6279 : 776 : return r;
6280 : : }
6281 : :
6282 : 0 : if (!ctx->quiet)
6283 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6284 : : "argument cannot be interpreted", "__builtin_bit_cast");
6285 : 0 : *non_constant_p = true;
6286 : 0 : if (ptr != buf)
6287 : 0 : XDELETE (ptr);
6288 : : return t;
6289 : : }
6290 : :
6291 : : /* Subroutine of cxx_eval_constant_expression.
6292 : : Evaluate a short-circuited logical expression T in the context
6293 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
6294 : : early return. CONTINUE_VALUE is used here purely for
6295 : : sanity check purposes. */
6296 : :
6297 : : static tree
6298 : 7320034 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6299 : : tree bailout_value, tree continue_value,
6300 : : bool *non_constant_p, bool *overflow_p,
6301 : : tree *jump_target)
6302 : : {
6303 : 7320034 : tree r;
6304 : 7320034 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6305 : : vc_prvalue, non_constant_p,
6306 : : overflow_p, jump_target);
6307 : 7320033 : if (*jump_target)
6308 : : return NULL_TREE;
6309 : 7320024 : VERIFY_CONSTANT (lhs);
6310 : 4464431 : if (tree_int_cst_equal (lhs, bailout_value))
6311 : : return lhs;
6312 : 3766964 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6313 : 3766964 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6314 : : vc_prvalue, non_constant_p,
6315 : : overflow_p, jump_target);
6316 : 3766964 : if (*jump_target)
6317 : : return NULL_TREE;
6318 : 3766964 : VERIFY_CONSTANT (r);
6319 : : return r;
6320 : : }
6321 : :
6322 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6323 : : CONSTRUCTOR elements to initialize (part of) an object containing that
6324 : : field. Return a pointer to the constructor_elt corresponding to the
6325 : : initialization of the field. */
6326 : :
6327 : : static constructor_elt *
6328 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6329 : : {
6330 : 0 : tree aggr = TREE_OPERAND (ref, 0);
6331 : 0 : tree field = TREE_OPERAND (ref, 1);
6332 : 0 : HOST_WIDE_INT i;
6333 : 0 : constructor_elt *ce;
6334 : :
6335 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6336 : :
6337 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6338 : : {
6339 : 0 : constructor_elt *base_ce
6340 : 0 : = base_field_constructor_elt (v, aggr);
6341 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6342 : : }
6343 : :
6344 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6345 : 0 : if (ce->index == field)
6346 : 0 : return ce;
6347 : :
6348 : 0 : gcc_unreachable ();
6349 : : return NULL;
6350 : : }
6351 : :
6352 : : /* Some of the expressions fed to the constexpr mechanism are calls to
6353 : : constructors, which have type void. In that case, return the type being
6354 : : initialized by the constructor. */
6355 : :
6356 : : static tree
6357 : 389860483 : initialized_type (tree t)
6358 : : {
6359 : 390707124 : if (TYPE_P (t))
6360 : : return t;
6361 : 390706789 : tree type = TREE_TYPE (t);
6362 : 390706789 : if (TREE_CODE (t) == CALL_EXPR)
6363 : : {
6364 : : /* A constructor call has void type, so we need to look deeper. */
6365 : 46883499 : tree fn = get_function_named_in_call (t);
6366 : 46883486 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6367 : 93719436 : && DECL_CXX_CONSTRUCTOR_P (fn))
6368 : 2386168 : type = DECL_CONTEXT (fn);
6369 : : }
6370 : 343823290 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6371 : 846641 : return initialized_type (TREE_OPERAND (t, 1));
6372 : 342976649 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6373 : 508319 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6374 : 389860148 : return cv_unqualified (type);
6375 : : }
6376 : :
6377 : : /* We're about to initialize element INDEX of an array or class from VALUE.
6378 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
6379 : : subobject and creating a new CONSTRUCTOR if the element is itself
6380 : : a class or array. */
6381 : :
6382 : : static void
6383 : 1617759 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6384 : : tree index, tree &value)
6385 : : {
6386 : 1617759 : new_ctx = *ctx;
6387 : :
6388 : 1617759 : if (index && TREE_CODE (index) != INTEGER_CST
6389 : 1479289 : && TREE_CODE (index) != FIELD_DECL
6390 : 3 : && TREE_CODE (index) != RANGE_EXPR)
6391 : : /* This won't have an element in the new CONSTRUCTOR. */
6392 : : return;
6393 : :
6394 : 1617759 : tree type = initialized_type (value);
6395 : 1617759 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6396 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6397 : : return;
6398 : 693369 : if (VECTOR_TYPE_P (type)
6399 : 1223 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
6400 : 693405 : && index == NULL_TREE)
6401 : : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6402 : : vector is constructed from smaller vectors, doesn't get its own
6403 : : CONSTRUCTOR either. */
6404 : : return;
6405 : :
6406 : : /* The sub-aggregate initializer might contain a placeholder;
6407 : : update object to refer to the subobject and ctor to refer to
6408 : : the (newly created) sub-initializer. */
6409 : 693333 : if (ctx->object)
6410 : : {
6411 : 530044 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6412 : : /* There's no well-defined subobject for this index. */
6413 : 3 : new_ctx.object = NULL_TREE;
6414 : : else
6415 : 530041 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6416 : : }
6417 : :
6418 : 693333 : if (is_empty_class (type))
6419 : : /* Leave ctor null for an empty subobject, they aren't represented in the
6420 : : result of evaluation. */
6421 : 646547 : new_ctx.ctor = NULL_TREE;
6422 : : else
6423 : : {
6424 : 46786 : tree elt = build_constructor (type, NULL);
6425 : 46786 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6426 : 46786 : new_ctx.ctor = elt;
6427 : : }
6428 : :
6429 : 693333 : if (TREE_CODE (value) == TARGET_EXPR)
6430 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6431 : 35052 : value = TARGET_EXPR_INITIAL (value);
6432 : : }
6433 : :
6434 : : /* We're about to process an initializer for a class or array TYPE. Make
6435 : : sure that CTX is set up appropriately. */
6436 : :
6437 : : static void
6438 : 1238801 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6439 : : {
6440 : : /* We don't bother building a ctor for an empty base subobject. */
6441 : 1238801 : if (is_empty_class (type))
6442 : : return;
6443 : :
6444 : : /* We're in the middle of an initializer that might involve placeholders;
6445 : : our caller should have created a CONSTRUCTOR for us to put the
6446 : : initializer into. We will either return that constructor or T. */
6447 : 594779 : gcc_assert (ctx->ctor);
6448 : 594779 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6449 : : (type, TREE_TYPE (ctx->ctor)));
6450 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
6451 : : the object is zero-initialized before calling the constructor. */
6452 : 594779 : if (ctx->object)
6453 : : {
6454 : 454147 : tree otype = TREE_TYPE (ctx->object);
6455 : 454147 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6456 : : /* Handle flexible array members. */
6457 : : || (TREE_CODE (otype) == ARRAY_TYPE
6458 : : && TYPE_DOMAIN (otype) == NULL_TREE
6459 : : && TREE_CODE (type) == ARRAY_TYPE
6460 : : && (same_type_ignoring_top_level_qualifiers_p
6461 : : (TREE_TYPE (type), TREE_TYPE (otype)))));
6462 : : }
6463 : 594779 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6464 : : || ctx->global->get_value (ctx->object) == ctx->ctor);
6465 : : }
6466 : :
6467 : : /* Subroutine of cxx_eval_constant_expression.
6468 : : The expression tree T denotes a C-style array or a C-style
6469 : : aggregate. Reduce it to a constant expression. */
6470 : :
6471 : : static tree
6472 : 1238176 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6473 : : value_cat lval,
6474 : : bool *non_constant_p, bool *overflow_p,
6475 : : tree *jump_target)
6476 : : {
6477 : 1238176 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6478 : 1238176 : bool changed = false;
6479 : 1238176 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6480 : 1238176 : tree type = TREE_TYPE (t);
6481 : :
6482 : 1238176 : constexpr_ctx new_ctx;
6483 : 1238176 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6484 : : {
6485 : : /* We don't really need the ctx->ctor business for a PMF or
6486 : : vector, but it's simpler to use the same code. */
6487 : 26196 : new_ctx = *ctx;
6488 : 26196 : new_ctx.ctor = build_constructor (type, NULL);
6489 : 26196 : new_ctx.object = NULL_TREE;
6490 : 26196 : ctx = &new_ctx;
6491 : 1238176 : };
6492 : 1238176 : verify_ctor_sanity (ctx, type);
6493 : 1238176 : vec<constructor_elt, va_gc> **p = nullptr;
6494 : 1238176 : if (ctx->ctor)
6495 : : {
6496 : 1238168 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6497 : 2473269 : vec_alloc (*p, vec_safe_length (v));
6498 : 1238168 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6499 : 16057 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6500 : : }
6501 : :
6502 : 1238176 : unsigned i;
6503 : 1238176 : tree index, value;
6504 : 1238176 : bool constant_p = true;
6505 : 1238176 : bool side_effects_p = false;
6506 : 2529843 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6507 : : {
6508 : 1611693 : tree orig_value = value;
6509 : 1611693 : init_subob_ctx (ctx, new_ctx, index, value);
6510 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6511 : 1611693 : bool no_slot = new_ctx.ctor == NULL_TREE;
6512 : 1611693 : int pos_hint = -1;
6513 : 1611693 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6514 : : {
6515 : : /* If we built a new CONSTRUCTOR, attach it now so that other
6516 : : initializers can refer to it. */
6517 : 40900 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6518 : 40900 : cep->value = new_ctx.ctor;
6519 : 40900 : pos_hint = cep - (*p)->begin();
6520 : 40900 : }
6521 : 1570793 : else if (TREE_CODE (type) == UNION_TYPE)
6522 : : /* Otherwise if we're constructing a non-aggregate union member, set
6523 : : the active union member now so that we can later detect and diagnose
6524 : : if its initializer attempts to activate another member. */
6525 : 1089 : get_or_insert_ctor_field (ctx->ctor, index);
6526 : 1611693 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6527 : : lval,
6528 : : non_constant_p, overflow_p,
6529 : : jump_target);
6530 : 1611693 : if (*jump_target)
6531 : : return NULL_TREE;
6532 : : /* Don't VERIFY_CONSTANT here. */
6533 : 1611693 : if (ctx->quiet && *non_constant_p)
6534 : : break;
6535 : 1291667 : if (elt != orig_value)
6536 : 173548 : changed = true;
6537 : :
6538 : 1291667 : if (!TREE_CONSTANT (elt))
6539 : 438333 : constant_p = false;
6540 : 1291667 : if (TREE_SIDE_EFFECTS (elt))
6541 : 129 : side_effects_p = true;
6542 : 1291667 : if (index && TREE_CODE (index) == COMPONENT_REF)
6543 : : {
6544 : : /* This is an initialization of a vfield inside a base
6545 : : subaggregate that we already initialized; push this
6546 : : initialization into the previous initialization. */
6547 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6548 : 0 : inner->value = elt;
6549 : 0 : changed = true;
6550 : 0 : }
6551 : 1291667 : else if (no_slot)
6552 : : /* This is an initializer for an empty field; now that we've
6553 : : checked that it's constant, we can ignore it. */
6554 : : changed = true;
6555 : 645423 : else if (index
6556 : 645295 : && (TREE_CODE (index) == NOP_EXPR
6557 : 645295 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
6558 : : {
6559 : : /* Old representation of empty bases. FIXME remove. */
6560 : 0 : gcc_checking_assert (false);
6561 : : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
6562 : : changed = true;
6563 : : }
6564 : : else
6565 : : {
6566 : 645423 : if (TREE_CODE (type) == UNION_TYPE
6567 : 645423 : && (*p)->last().index != index)
6568 : : /* The initializer erroneously changed the active union member that
6569 : : we're initializing. */
6570 : 8 : gcc_assert (*non_constant_p);
6571 : : else
6572 : : {
6573 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
6574 : : so recompute the location of the target constructer_elt. */
6575 : 645415 : constructor_elt *cep
6576 : 645415 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6577 : 645415 : cep->value = elt;
6578 : : }
6579 : :
6580 : : /* Adding or replacing an element might change the ctor's flags. */
6581 : 645423 : TREE_CONSTANT (ctx->ctor) = constant_p;
6582 : 645423 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6583 : : }
6584 : : }
6585 : 1238176 : if (*non_constant_p)
6586 : : return t;
6587 : 918114 : if (!changed)
6588 : : {
6589 : 158778 : if (VECTOR_TYPE_P (type))
6590 : 14268 : t = fold (t);
6591 : 158778 : return t;
6592 : : }
6593 : 759336 : t = ctx->ctor;
6594 : 759336 : if (!t)
6595 : 8 : t = build_constructor (type, NULL);
6596 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
6597 : : element without an explicit initializer as value-initialized. */
6598 : 759336 : CONSTRUCTOR_NO_CLEARING (t) = false;
6599 : 759336 : TREE_CONSTANT (t) = constant_p;
6600 : 759336 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6601 : 759336 : if (VECTOR_TYPE_P (type))
6602 : 1070 : t = fold (t);
6603 : : return t;
6604 : : }
6605 : :
6606 : : /* Subroutine of cxx_eval_constant_expression.
6607 : : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6608 : : initialization of a non-static data member of array type. Reduce it to a
6609 : : CONSTRUCTOR.
6610 : :
6611 : : Note that apart from value-initialization (when VALUE_INIT is true),
6612 : : this is only intended to support value-initialization and the
6613 : : initializations done by defaulted constructors for classes with
6614 : : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6615 : : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6616 : : for the copy/move constructor. */
6617 : :
6618 : : static tree
6619 : 625 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6620 : : bool value_init, value_cat lval,
6621 : : bool *non_constant_p, bool *overflow_p,
6622 : : tree *jump_target)
6623 : : {
6624 : 625 : tree elttype = TREE_TYPE (atype);
6625 : 625 : verify_ctor_sanity (ctx, atype);
6626 : 625 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6627 : 625 : bool pre_init = false;
6628 : 625 : unsigned HOST_WIDE_INT i;
6629 : 625 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6630 : :
6631 : 625 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6632 : 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6633 : 0 : non_constant_p, overflow_p, jump_target);
6634 : :
6635 : : /* We already checked access when building the VEC_INIT_EXPR. */
6636 : 625 : deferring_access_check_sentinel acs (dk_deferred);
6637 : :
6638 : : /* For the default constructor, build up a call to the default
6639 : : constructor of the element type. We only need to handle class types
6640 : : here, as for a constructor to be constexpr, all members must be
6641 : : initialized, which for a defaulted default constructor means they must
6642 : : be of a class type with a constexpr default constructor. */
6643 : 625 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6644 : : /* We only do this at the lowest level. */;
6645 : 581 : else if (value_init)
6646 : : {
6647 : 174 : init = build_value_init (elttype, complain);
6648 : 174 : pre_init = true;
6649 : : }
6650 : 407 : else if (!init)
6651 : : {
6652 : 322 : releasing_vec argvec;
6653 : 322 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6654 : : &argvec, elttype, LOOKUP_NORMAL,
6655 : : complain);
6656 : 322 : init = build_aggr_init_expr (elttype, init);
6657 : 322 : pre_init = true;
6658 : 322 : }
6659 : :
6660 : 625 : bool zeroed_out = false;
6661 : 625 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
6662 : : {
6663 : : /* We're initializing an array object that had been zero-initialized
6664 : : earlier. Truncate ctx->ctor, and propagate its zeroed state by
6665 : : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
6666 : : initializers we append to it. */
6667 : 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
6668 : 156 : zeroed_out = true;
6669 : 156 : vec_safe_truncate (*p, 0);
6670 : : }
6671 : :
6672 : 625 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
6673 : : overflow_p, jump_target);
6674 : 625 : if (*jump_target)
6675 : : return NULL_TREE;
6676 : 625 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
6677 : 6273 : for (i = 0; i < max; ++i)
6678 : : {
6679 : 6066 : tree idx = build_int_cst (size_type_node, i);
6680 : 6066 : tree eltinit;
6681 : 6066 : bool reuse = false;
6682 : 6066 : constexpr_ctx new_ctx;
6683 : 6401 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
6684 : 6066 : bool no_slot = new_ctx.ctor == NULL_TREE;
6685 : 6066 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6686 : : {
6687 : 5886 : if (zeroed_out)
6688 : 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
6689 : 5886 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
6690 : : }
6691 : 6066 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6692 : : {
6693 : : /* A multidimensional array; recurse. */
6694 : 176 : if (value_init || init == NULL_TREE)
6695 : : {
6696 : 168 : eltinit = NULL_TREE;
6697 : 168 : reuse = i == 0;
6698 : : }
6699 : : else
6700 : 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6701 : 176 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
6702 : : value_init, lval, non_constant_p,
6703 : : overflow_p, jump_target);
6704 : : }
6705 : 5890 : else if (pre_init)
6706 : : {
6707 : : /* Initializing an element using value or default initialization
6708 : : we just pre-built above. */
6709 : 5731 : if (init == void_node)
6710 : : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
6711 : 3 : return ctx->ctor;
6712 : 5728 : eltinit = init;
6713 : 5728 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6714 : : /* Clarify what object is being initialized (118285). */
6715 : 5591 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6716 : 5728 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6717 : : non_constant_p, overflow_p,
6718 : : jump_target);
6719 : 5728 : reuse = i == 0;
6720 : : }
6721 : : else
6722 : : {
6723 : : /* Copying an element. */
6724 : 159 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6725 : 159 : if (!lvalue_p (init))
6726 : 132 : eltinit = move (eltinit);
6727 : 159 : eltinit = (perform_implicit_conversion_flags
6728 : 159 : (elttype, eltinit, complain,
6729 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
6730 : 159 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6731 : : /* Clarify what object is being initialized (118285). */
6732 : 142 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6733 : 159 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6734 : : non_constant_p, overflow_p,
6735 : : jump_target);
6736 : : }
6737 : 6063 : if (*jump_target)
6738 : : return NULL_TREE;
6739 : 6063 : if (*non_constant_p)
6740 : : break;
6741 : 5965 : if (no_slot)
6742 : : {
6743 : : /* This is an initializer for an empty subobject; now that we've
6744 : : checked that it's constant, we can ignore it. */
6745 : 18 : gcc_checking_assert (i == 0);
6746 : : break;
6747 : : }
6748 : 5947 : else if (new_ctx.ctor != ctx->ctor)
6749 : : {
6750 : : /* We appended this element above; update the value. */
6751 : 5816 : gcc_assert ((*p)->last().index == idx);
6752 : 5816 : (*p)->last().value = eltinit;
6753 : : }
6754 : : else
6755 : 131 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
6756 : : /* Reuse the result of cxx_eval_constant_expression call
6757 : : from the first iteration to all others if it is a constant
6758 : : initializer that doesn't require relocations. */
6759 : 5947 : if (reuse
6760 : 5947 : && max > 1
6761 : 5947 : && (eltinit == NULL_TREE
6762 : 452 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
6763 : 452 : == null_pointer_node)))
6764 : : {
6765 : 299 : if (new_ctx.ctor != ctx->ctor)
6766 : 174 : eltinit = new_ctx.ctor;
6767 : 299 : tree range = build2 (RANGE_EXPR, size_type_node,
6768 : : build_int_cst (size_type_node, 1),
6769 : 299 : build_int_cst (size_type_node, max - 1));
6770 : 299 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
6771 : 299 : break;
6772 : : }
6773 : 5648 : else if (i == 0)
6774 : 204 : vec_safe_reserve (*p, max);
6775 : : }
6776 : :
6777 : 622 : if (!*non_constant_p)
6778 : : {
6779 : 524 : init = ctx->ctor;
6780 : 524 : CONSTRUCTOR_NO_CLEARING (init) = false;
6781 : : }
6782 : 622 : return init;
6783 : : }
6784 : :
6785 : : static tree
6786 : 512 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
6787 : : value_cat lval,
6788 : : bool *non_constant_p, bool *overflow_p, tree *jump_target)
6789 : : {
6790 : 512 : tree atype = TREE_TYPE (t);
6791 : 512 : tree init = VEC_INIT_EXPR_INIT (t);
6792 : 512 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
6793 : 512 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
6794 : : ;
6795 : 73 : else if (CONSTRUCTOR_NELTS (init) == 0
6796 : 73 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
6797 : : {
6798 : : /* Handle {} as value-init. */
6799 : : init = NULL_TREE;
6800 : : value_init = true;
6801 : : }
6802 : : else
6803 : : {
6804 : : /* This is a more complicated case, like needing to loop over trailing
6805 : : elements; call build_vec_init and evaluate the result. */
6806 : 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6807 : 63 : constexpr_ctx new_ctx = *ctx;
6808 : 63 : if (!ctx->object)
6809 : : {
6810 : : /* We want to have an initialization target for an VEC_INIT_EXPR.
6811 : : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
6812 : 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
6813 : 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
6814 : 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
6815 : 51 : ctx->global->put_value (new_ctx.object, ctor);
6816 : 51 : ctx = &new_ctx;
6817 : : }
6818 : 63 : init = expand_vec_init_expr (ctx->object, t, complain);
6819 : 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
6820 : : overflow_p, jump_target);
6821 : : }
6822 : 449 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
6823 : : lval, non_constant_p, overflow_p, jump_target);
6824 : 449 : if (*non_constant_p)
6825 : : return t;
6826 : : else
6827 : 370 : return r;
6828 : : }
6829 : :
6830 : : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
6831 : : where the desired type is an array of unknown bounds because the variable
6832 : : has had its bounds deduced since the wrapping expression was created. */
6833 : :
6834 : : static bool
6835 : 154113811 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
6836 : : {
6837 : 154113811 : while (TREE_CODE (type1) == ARRAY_TYPE
6838 : 13295 : && TREE_CODE (type2) == ARRAY_TYPE
6839 : 154113815 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
6840 : : {
6841 : 2 : type1 = TREE_TYPE (type1);
6842 : 2 : type2 = TREE_TYPE (type2);
6843 : : }
6844 : 154113811 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
6845 : : }
6846 : :
6847 : : /* Try to determine the currently active union member for an expression
6848 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
6849 : : otherwise return NULL_TREE. */
6850 : :
6851 : : static tree
6852 : 75 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
6853 : : {
6854 : 75 : constexpr_ctx new_ctx = *ctx;
6855 : 75 : new_ctx.quiet = true;
6856 : 75 : bool non_constant_p = false, overflow_p = false;
6857 : 75 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
6858 : : &non_constant_p,
6859 : : &overflow_p, jump_target);
6860 : 75 : if (*jump_target)
6861 : : return NULL_TREE;
6862 : 75 : if (TREE_CODE (ctor) == CONSTRUCTOR
6863 : 24 : && CONSTRUCTOR_NELTS (ctor) == 1
6864 : 12 : && CONSTRUCTOR_ELT (ctor, 0)->index
6865 : 87 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
6866 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
6867 : : return NULL_TREE;
6868 : : }
6869 : :
6870 : : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
6871 : :
6872 : : static tree
6873 : 3746692 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
6874 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
6875 : : tree *jump_target)
6876 : : {
6877 : 5994596 : tree optype = TREE_TYPE (op);
6878 : 5994596 : unsigned HOST_WIDE_INT const_nunits;
6879 : 5994596 : if (off == 0 && similar_type_p (optype, type))
6880 : : return op;
6881 : 3743032 : else if (cxx_dialect >= cxx26
6882 : 2123560 : && VAR_P (op)
6883 : 778895 : && DECL_VTABLE_OR_VTT_P (op)
6884 : 12329 : && same_type_ignoring_top_level_qualifiers_p (type,
6885 : : ptrdiff_type_node)
6886 : 3744014 : && POINTER_TYPE_P (strip_array_types (optype)))
6887 : : {
6888 : : /* We often read some virtual table elements using ptrdiff_t rather
6889 : : than pointer type. */
6890 : 982 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
6891 : : strip_array_types (optype),
6892 : : op, off, empty_base,
6893 : : jump_target))
6894 : 982 : return fold_convert (type, ret);
6895 : : }
6896 : 3742050 : else if (TREE_CODE (optype) == COMPLEX_TYPE
6897 : 3742050 : && similar_type_p (type, TREE_TYPE (optype)))
6898 : : {
6899 : : /* *(foo *)&complexfoo => __real__ complexfoo */
6900 : 0 : if (off == 0)
6901 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
6902 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
6903 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
6904 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
6905 : : }
6906 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
6907 : 3742050 : else if (VECTOR_TYPE_P (optype)
6908 : 0 : && similar_type_p (type, TREE_TYPE (optype))
6909 : 3742050 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
6910 : : {
6911 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6912 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
6913 : 0 : if (off < max_offset && off % part_width == 0)
6914 : : {
6915 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
6916 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
6917 : 0 : TYPE_SIZE (type), index);
6918 : : }
6919 : : }
6920 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
6921 : 3742050 : else if (TREE_CODE (optype) == ARRAY_TYPE
6922 : 2247904 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
6923 : 5989954 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
6924 : : {
6925 : 2247904 : tree type_domain = TYPE_DOMAIN (optype);
6926 : 2247904 : tree min_val = size_zero_node;
6927 : 2247904 : if (type_domain && TYPE_MIN_VALUE (type_domain))
6928 : 2247899 : min_val = TYPE_MIN_VALUE (type_domain);
6929 : 2247904 : unsigned HOST_WIDE_INT el_sz
6930 : 2247904 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
6931 : 2247904 : unsigned HOST_WIDE_INT idx = off / el_sz;
6932 : 2247904 : unsigned HOST_WIDE_INT rem = off % el_sz;
6933 : 2247904 : if (tree_fits_uhwi_p (min_val))
6934 : : {
6935 : 2247904 : tree index = size_int (idx + tree_to_uhwi (min_val));
6936 : 2247904 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
6937 : : NULL_TREE, NULL_TREE);
6938 : 2247904 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
6939 : 2247904 : empty_base, jump_target);
6940 : : }
6941 : : }
6942 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
6943 : 1494146 : else if (TREE_CODE (optype) == RECORD_TYPE
6944 : 1494146 : || TREE_CODE (optype) == UNION_TYPE)
6945 : : {
6946 : 1490635 : if (TREE_CODE (optype) == UNION_TYPE)
6947 : : /* For unions prefer the currently active member. */
6948 : 75 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
6949 : : {
6950 : 12 : unsigned HOST_WIDE_INT el_sz
6951 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6952 : 12 : if (off < el_sz)
6953 : : {
6954 : 12 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
6955 : : op, field, NULL_TREE);
6956 : 12 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
6957 : : off, empty_base,
6958 : : jump_target))
6959 : : return ret;
6960 : : }
6961 : : }
6962 : :
6963 : : /* Handle conversion to "as base" type. */
6964 : 1490629 : if (CLASS_TYPE_P (optype)
6965 : 2981094 : && CLASSTYPE_AS_BASE (optype) == type)
6966 : : return op;
6967 : :
6968 : : /* Handle conversion to an empty base class, which is represented with a
6969 : : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
6970 : : which is likely to be a waste of time (109678). */
6971 : 1490052 : if (is_empty_class (type)
6972 : 1449414 : && CLASS_TYPE_P (optype)
6973 : 2939460 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
6974 : : {
6975 : 850384 : if (empty_base)
6976 : 850384 : *empty_base = true;
6977 : 850384 : return op;
6978 : : }
6979 : :
6980 : 639668 : for (tree field = TYPE_FIELDS (optype);
6981 : 6128405 : field; field = DECL_CHAIN (field))
6982 : 6119237 : if (TREE_CODE (field) == FIELD_DECL
6983 : 645608 : && TREE_TYPE (field) != error_mark_node
6984 : 6764845 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
6985 : : {
6986 : 645608 : tree pos = byte_position (field);
6987 : 645608 : if (!tree_fits_uhwi_p (pos))
6988 : 0 : continue;
6989 : 645608 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
6990 : 645608 : unsigned HOST_WIDE_INT el_sz;
6991 : 645608 : if (DECL_FIELD_IS_BASE (field)
6992 : 41496 : && CLASS_TYPE_P (optype)
6993 : 687104 : && CLASSTYPE_VBASECLASSES (optype))
6994 : 6189 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
6995 : : else
6996 : 639419 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6997 : 645608 : if (upos <= off && off < upos + el_sz)
6998 : : {
6999 : 639578 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7000 : : op, field, NULL_TREE);
7001 : 639578 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7002 : : off - upos,
7003 : : empty_base,
7004 : : jump_target))
7005 : : return ret;
7006 : : }
7007 : : }
7008 : : }
7009 : :
7010 : : return NULL_TREE;
7011 : : }
7012 : :
7013 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7014 : : match. We want to be less strict for simple *& folding; if we have a
7015 : : non-const temporary that we access through a const pointer, that should
7016 : : work. We handle this here rather than change fold_indirect_ref_1
7017 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7018 : : don't really make sense outside of constant expression evaluation. Also
7019 : : we want to allow folding to COMPONENT_REF, which could cause trouble
7020 : : with TBAA in fold_indirect_ref_1. */
7021 : :
7022 : : static tree
7023 : 63201230 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7024 : : tree op0, bool *empty_base, tree *jump_target)
7025 : : {
7026 : 63201230 : tree sub = op0;
7027 : 63201230 : tree subtype;
7028 : :
7029 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7030 : 131705747 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7031 : 176917937 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7032 : : {
7033 : 48879415 : if (TREE_CODE (sub) == NOP_EXPR
7034 : 48879415 : && REINTERPRET_CAST_P (sub))
7035 : : return NULL_TREE;
7036 : 48879415 : sub = TREE_OPERAND (sub, 0);
7037 : : }
7038 : :
7039 : 63201230 : subtype = TREE_TYPE (sub);
7040 : 63201230 : if (!INDIRECT_TYPE_P (subtype))
7041 : : return NULL_TREE;
7042 : :
7043 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7044 : : the innermost component into the offset until it would make the
7045 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
7046 : : more folding opportunities. */
7047 : 66307295 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7048 : 3106120 : if (cxx_dialect >= cxx26)
7049 : : {
7050 : : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7051 : : to access virtual base members. */
7052 : 1728747 : tree nobj = obj;
7053 : 1728747 : while (TREE_CODE (nobj) == COMPONENT_REF
7054 : 1743570 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7055 : 14823 : nobj = TREE_OPERAND (nobj, 0);
7056 : 1728747 : if (nobj != obj
7057 : 14365 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7058 : 1743112 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7059 : 2039 : while (obj != nobj)
7060 : : {
7061 : 1174 : tree field = TREE_OPERAND (obj, 1);
7062 : 1174 : tree pos = byte_position (field);
7063 : 1174 : off = int_const_binop (PLUS_EXPR, off, pos);
7064 : 1174 : obj = TREE_OPERAND (obj, 0);
7065 : : }
7066 : : }
7067 : 3748084 : while (TREE_CODE (obj) == COMPONENT_REF
7068 : : /* We need to preserve union member accesses so that we can
7069 : : later properly diagnose accessing the wrong member. */
7070 : 1465622 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7071 : 5105216 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7072 : : {
7073 : 645606 : tree field = TREE_OPERAND (obj, 1);
7074 : 645606 : tree pos = byte_position (field);
7075 : 645606 : if (integer_zerop (off) && integer_nonzerop (pos))
7076 : : /* If the offset is already 0, keep going as long as the
7077 : : component is at position 0. */
7078 : : break;
7079 : 641964 : off = int_const_binop (PLUS_EXPR, off, pos);
7080 : 641964 : obj = TREE_OPERAND (obj, 0);
7081 : : }
7082 : 3106120 : };
7083 : :
7084 : 63201175 : if (TREE_CODE (sub) == ADDR_EXPR)
7085 : : {
7086 : 26970317 : tree op = TREE_OPERAND (sub, 0);
7087 : 26970317 : tree optype = TREE_TYPE (op);
7088 : :
7089 : : /* *&CONST_DECL -> to the value of the const decl. */
7090 : 26970317 : if (TREE_CODE (op) == CONST_DECL)
7091 : 0 : return DECL_INITIAL (op);
7092 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
7093 : 26970317 : if (similar_type_p (optype, type))
7094 : : {
7095 : 25749908 : tree fop = fold_read_from_constant_string (op);
7096 : 25749908 : if (fop)
7097 : : return fop;
7098 : : else
7099 : 25739816 : return op;
7100 : : }
7101 : : else
7102 : : {
7103 : 1220409 : tree off = integer_zero_node;
7104 : 1220409 : canonicalize_obj_off (op, off);
7105 : 1220409 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7106 : : tree_to_uhwi (off), empty_base,
7107 : : jump_target);
7108 : : }
7109 : : }
7110 : 36230858 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7111 : 36230858 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7112 : : {
7113 : 2035208 : tree op00 = TREE_OPERAND (sub, 0);
7114 : 2035208 : tree off = TREE_OPERAND (sub, 1);
7115 : :
7116 : 2035208 : STRIP_NOPS (op00);
7117 : 2035208 : if (TREE_CODE (op00) == ADDR_EXPR)
7118 : : {
7119 : 1885711 : tree obj = TREE_OPERAND (op00, 0);
7120 : 1885711 : canonicalize_obj_off (obj, off);
7121 : 1885711 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7122 : : tree_to_uhwi (off), empty_base,
7123 : : jump_target);
7124 : : }
7125 : : }
7126 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7127 : 34195650 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7128 : 34195650 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7129 : : {
7130 : 0 : tree type_domain;
7131 : 0 : tree min_val = size_zero_node;
7132 : 0 : tree newsub
7133 : 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7134 : : jump_target);
7135 : 0 : if (*jump_target)
7136 : : return NULL_TREE;
7137 : 0 : if (newsub)
7138 : : sub = newsub;
7139 : : else
7140 : 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7141 : 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7142 : 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7143 : 0 : min_val = TYPE_MIN_VALUE (type_domain);
7144 : 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7145 : 0 : NULL_TREE);
7146 : : }
7147 : :
7148 : : return NULL_TREE;
7149 : : }
7150 : :
7151 : : static tree
7152 : 35565612 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7153 : : value_cat lval,
7154 : : bool *non_constant_p, bool *overflow_p,
7155 : : tree *jump_target)
7156 : : {
7157 : 35565612 : tree orig_op0 = TREE_OPERAND (t, 0);
7158 : 35565612 : bool empty_base = false;
7159 : :
7160 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7161 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7162 : :
7163 : 35565612 : if (TREE_CODE (t) == MEM_REF
7164 : 35565612 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7165 : : {
7166 : 9 : gcc_assert (ctx->quiet);
7167 : 9 : *non_constant_p = true;
7168 : 9 : return t;
7169 : : }
7170 : :
7171 : : /* First try to simplify it directly. */
7172 : 35565603 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7173 : : orig_op0, &empty_base, jump_target);
7174 : 35565603 : if (*jump_target)
7175 : : return NULL_TREE;
7176 : 35565603 : if (!r)
7177 : : {
7178 : : /* If that didn't work, evaluate the operand first. */
7179 : 34120995 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7180 : : vc_prvalue, non_constant_p,
7181 : : overflow_p, jump_target);
7182 : 34120995 : if (*jump_target)
7183 : : return NULL_TREE;
7184 : : /* Don't VERIFY_CONSTANT here. */
7185 : 34120987 : if (*non_constant_p)
7186 : : return t;
7187 : :
7188 : 22507946 : if (!lval && integer_zerop (op0))
7189 : : {
7190 : 68 : if (!ctx->quiet)
7191 : 12 : error ("dereferencing a null pointer");
7192 : 68 : *non_constant_p = true;
7193 : 68 : return t;
7194 : : }
7195 : :
7196 : 22507878 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7197 : : &empty_base, jump_target);
7198 : 22507878 : if (*jump_target)
7199 : : return NULL_TREE;
7200 : 22507878 : if (r == NULL_TREE)
7201 : : {
7202 : : /* We couldn't fold to a constant value. Make sure it's not
7203 : : something we should have been able to fold. */
7204 : 227764 : tree sub = op0;
7205 : 227764 : STRIP_NOPS (sub);
7206 : 227764 : if (TREE_CODE (sub) == ADDR_EXPR)
7207 : : {
7208 : 1721 : gcc_assert (!similar_type_p
7209 : : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7210 : : /* DR 1188 says we don't have to deal with this. */
7211 : 1721 : if (!ctx->quiet)
7212 : : {
7213 : 8 : auto_diagnostic_group d;
7214 : 13 : error_at (cp_expr_loc_or_input_loc (t),
7215 : : "accessing value of %qT object through a %qT "
7216 : : "glvalue in a constant expression",
7217 : 8 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7218 : 8 : tree ob = build_fold_indirect_ref (sub);
7219 : 8 : if (DECL_P (ob))
7220 : : {
7221 : 8 : if (DECL_ARTIFICIAL (ob))
7222 : 1 : inform (DECL_SOURCE_LOCATION (ob),
7223 : 1 : "%qT object created here", TREE_TYPE (ob));
7224 : : else
7225 : 7 : inform (DECL_SOURCE_LOCATION (ob),
7226 : : "%q#D declared here", ob);
7227 : : }
7228 : 8 : }
7229 : 1721 : *non_constant_p = true;
7230 : 1721 : return t;
7231 : : }
7232 : :
7233 : 226043 : if (lval == vc_glvalue && op0 != orig_op0)
7234 : 19665 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7235 : 206378 : if (!lval)
7236 : 145548 : VERIFY_CONSTANT (t);
7237 : 206378 : return t;
7238 : : }
7239 : : }
7240 : :
7241 : 23724722 : r = cxx_eval_constant_expression (ctx, r,
7242 : : lval, non_constant_p, overflow_p,
7243 : : jump_target);
7244 : 23724721 : if (*jump_target)
7245 : : return NULL_TREE;
7246 : 23724721 : if (*non_constant_p)
7247 : : return t;
7248 : :
7249 : : /* If we're pulling out the value of an empty base, just return an empty
7250 : : CONSTRUCTOR. */
7251 : 17709445 : if (empty_base && !lval)
7252 : : {
7253 : 19372 : r = build_constructor (TREE_TYPE (t), NULL);
7254 : 19372 : TREE_CONSTANT (r) = true;
7255 : : }
7256 : :
7257 : : return r;
7258 : : }
7259 : :
7260 : : /* Complain about R, a DECL that is accessed outside its lifetime. */
7261 : :
7262 : : static void
7263 : 34 : outside_lifetime_error (location_t loc, tree r)
7264 : : {
7265 : 34 : auto_diagnostic_group d;
7266 : 34 : if (DECL_NAME (r) == heap_deleted_identifier)
7267 : : {
7268 : : /* Provide a more accurate message for deleted variables. */
7269 : 6 : error_at (loc, "use of allocated storage after deallocation "
7270 : : "in a constant expression");
7271 : 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7272 : : }
7273 : : else
7274 : : {
7275 : 28 : error_at (loc, "accessing %qE outside its lifetime", r);
7276 : 28 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7277 : : }
7278 : 34 : }
7279 : :
7280 : : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7281 : : FUNDEF_P is true if we're checking a constexpr function body.
7282 : : Shared between potential_constant_expression and
7283 : : cxx_eval_constant_expression. */
7284 : :
7285 : : static void
7286 : 328 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7287 : : {
7288 : 328 : auto_diagnostic_group d;
7289 : 328 : tree type = TREE_TYPE (r);
7290 : 328 : if (DECL_NAME (r) == heap_uninit_identifier
7291 : 328 : || DECL_NAME (r) == heap_identifier
7292 : 325 : || DECL_NAME (r) == heap_vec_uninit_identifier
7293 : 653 : || DECL_NAME (r) == heap_vec_identifier)
7294 : : {
7295 : 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7296 : : "storage is not usable in a constant expression"))
7297 : 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7298 : 3 : return;
7299 : : }
7300 : 325 : if (DECL_NAME (r) == heap_deleted_identifier)
7301 : : {
7302 : 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7303 : : "deallocation in a constant expression"))
7304 : 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7305 : 0 : return;
7306 : : }
7307 : 325 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7308 : : "a constant expression", r))
7309 : : return;
7310 : : /* Avoid error cascade. */
7311 : 324 : if (DECL_INITIAL (r) == error_mark_node)
7312 : : return;
7313 : 310 : if (DECL_DECLARED_CONSTEXPR_P (r))
7314 : 3 : inform (DECL_SOURCE_LOCATION (r),
7315 : : "%qD used in its own initializer", r);
7316 : 307 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7317 : : {
7318 : 216 : if (!CP_TYPE_CONST_P (type))
7319 : 187 : inform (DECL_SOURCE_LOCATION (r),
7320 : : "%q#D is not const", r);
7321 : 29 : else if (CP_TYPE_VOLATILE_P (type))
7322 : 0 : inform (DECL_SOURCE_LOCATION (r),
7323 : : "%q#D is volatile", r);
7324 : 29 : else if (!DECL_INITIAL (r)
7325 : 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7326 : 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7327 : 29 : inform (DECL_SOURCE_LOCATION (r),
7328 : : "%qD was not initialized with a constant "
7329 : : "expression", r);
7330 : : else
7331 : 0 : gcc_unreachable ();
7332 : : }
7333 : 91 : else if (TYPE_REF_P (type))
7334 : 9 : inform (DECL_SOURCE_LOCATION (r),
7335 : : "%qD was not initialized with a constant "
7336 : : "expression", r);
7337 : : else
7338 : : {
7339 : 82 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7340 : 82 : inform (DECL_SOURCE_LOCATION (r),
7341 : : "%qD was not declared %<constexpr%>", r);
7342 : : else
7343 : 0 : inform (DECL_SOURCE_LOCATION (r),
7344 : : "%qD does not have integral or enumeration type",
7345 : : r);
7346 : : }
7347 : 328 : }
7348 : :
7349 : : /* Subroutine of cxx_eval_constant_expression.
7350 : : Like cxx_eval_unary_expression, except for trinary expressions. */
7351 : :
7352 : : static tree
7353 : 16 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7354 : : value_cat lval,
7355 : : bool *non_constant_p, bool *overflow_p,
7356 : : tree *jump_target)
7357 : : {
7358 : 16 : int i;
7359 : 16 : tree args[3];
7360 : 16 : tree val;
7361 : :
7362 : 37 : for (i = 0; i < 3; i++)
7363 : : {
7364 : 30 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7365 : : lval,
7366 : : non_constant_p, overflow_p,
7367 : : jump_target);
7368 : 30 : if (*jump_target)
7369 : : return NULL_TREE;
7370 : 30 : VERIFY_CONSTANT (args[i]);
7371 : : }
7372 : :
7373 : 7 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7374 : : args[0], args[1], args[2]);
7375 : 7 : if (val == NULL_TREE)
7376 : : return t;
7377 : 7 : VERIFY_CONSTANT (val);
7378 : : return val;
7379 : : }
7380 : :
7381 : : /* True if T was declared in a function declared to be constexpr, and
7382 : : therefore potentially constant in C++14. */
7383 : :
7384 : : bool
7385 : 80420905 : var_in_constexpr_fn (tree t)
7386 : : {
7387 : 80420905 : tree ctx = DECL_CONTEXT (t);
7388 : 80420905 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7389 : 153143795 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7390 : : }
7391 : :
7392 : : /* True if a function might be constexpr: either a function that was
7393 : : declared constexpr, or a C++17 lambda op(). */
7394 : :
7395 : : bool
7396 : 442634938 : maybe_constexpr_fn (tree t)
7397 : : {
7398 : 442634938 : return (DECL_DECLARED_CONSTEXPR_P (t)
7399 : 197351534 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7400 : 635094072 : || (flag_implicit_constexpr
7401 : 66 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7402 : : }
7403 : :
7404 : : /* True if T was declared in a function that might be constexpr: either a
7405 : : function that was declared constexpr, or a C++17 lambda op(). */
7406 : :
7407 : : bool
7408 : 53182438 : var_in_maybe_constexpr_fn (tree t)
7409 : : {
7410 : 106364292 : return (DECL_FUNCTION_SCOPE_P (t)
7411 : 99010479 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7412 : : }
7413 : :
7414 : : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7415 : : build_over_call we implement trivial copy of a class with tail padding using
7416 : : assignment of character arrays, which is valid in normal code, but not in
7417 : : constexpr evaluation. We don't need to worry about clobbering tail padding
7418 : : in constexpr evaluation, so strip the type punning. */
7419 : :
7420 : : static void
7421 : 32527630 : maybe_simplify_trivial_copy (tree &target, tree &init)
7422 : : {
7423 : 32527630 : if (TREE_CODE (target) == MEM_REF
7424 : 4429 : && TREE_CODE (init) == MEM_REF
7425 : 4343 : && TREE_TYPE (target) == TREE_TYPE (init)
7426 : 4343 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7427 : 32531973 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7428 : : {
7429 : 4343 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7430 : 4343 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7431 : : }
7432 : 32527630 : }
7433 : :
7434 : : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7435 : : of constant type. This does not check for 'mutable', so the
7436 : : caller is expected to be mindful of that. */
7437 : :
7438 : : static bool
7439 : 280 : cref_has_const_field (tree ref)
7440 : : {
7441 : 349 : while (TREE_CODE (ref) == COMPONENT_REF)
7442 : : {
7443 : 340 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7444 : : return true;
7445 : 69 : ref = TREE_OPERAND (ref, 0);
7446 : : }
7447 : : return false;
7448 : : }
7449 : :
7450 : : /* Return true if we are modifying something that is const during constant
7451 : : expression evaluation. CODE is the code of the statement, OBJ is the
7452 : : object in question, MUTABLE_P is true if one of the subobjects were
7453 : : declared mutable. */
7454 : :
7455 : : static bool
7456 : 29670163 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7457 : : {
7458 : : /* If this is initialization, there's no problem. */
7459 : 29670163 : if (code != MODIFY_EXPR)
7460 : : return false;
7461 : :
7462 : : /* [basic.type.qualifier] "A const object is an object of type
7463 : : const T or a non-mutable subobject of a const object." */
7464 : 7809215 : if (mutable_p)
7465 : : return false;
7466 : :
7467 : 7808479 : if (TREE_READONLY (obj))
7468 : : return true;
7469 : :
7470 : 7804732 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7471 : : {
7472 : : /* Although a COMPONENT_REF may have a const type, we should
7473 : : only consider it modifying a const object when any of the
7474 : : field components is const. This can happen when using
7475 : : constructs such as const_cast<const T &>(m), making something
7476 : : const even though it wasn't declared const. */
7477 : 4057 : if (TREE_CODE (obj) == COMPONENT_REF)
7478 : 280 : return cref_has_const_field (obj);
7479 : : else
7480 : : return true;
7481 : : }
7482 : :
7483 : : return false;
7484 : : }
7485 : :
7486 : : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7487 : :
7488 : : static tree
7489 : 32527630 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7490 : : value_cat lval,
7491 : : bool *non_constant_p, bool *overflow_p,
7492 : : tree *jump_target)
7493 : : {
7494 : 32527630 : constexpr_ctx new_ctx = *ctx;
7495 : :
7496 : 32527630 : tree init = TREE_OPERAND (t, 1);
7497 : :
7498 : : /* First we figure out where we're storing to. */
7499 : 32527630 : tree target = TREE_OPERAND (t, 0);
7500 : :
7501 : 32527630 : maybe_simplify_trivial_copy (target, init);
7502 : :
7503 : 32527630 : tree type = TREE_TYPE (target);
7504 : 32527630 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7505 : 25064551 : if (preeval && !TREE_CLOBBER_P (init))
7506 : : {
7507 : : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7508 : 25012604 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7509 : 10295472 : && TREE_CODE (init) == CALL_EXPR
7510 : 913720 : && CALL_EXPR_FN (init) == NULL_TREE
7511 : 25012608 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7512 : 4 : return void_node;
7513 : :
7514 : : /* Evaluate the value to be stored without knowing what object it will be
7515 : : stored in, so that any side-effects happen first. */
7516 : 25012600 : if (!SCALAR_TYPE_P (type))
7517 : 56428 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7518 : 25012600 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7519 : : non_constant_p, overflow_p,
7520 : : jump_target);
7521 : 25012598 : if (*jump_target)
7522 : : return NULL_TREE;
7523 : 25012519 : if (*non_constant_p)
7524 : : return t;
7525 : : }
7526 : :
7527 : 26589800 : bool evaluated = false;
7528 : 26589800 : if (lval == vc_glvalue)
7529 : : {
7530 : : /* If we want to return a reference to the target, we need to evaluate it
7531 : : as a whole; otherwise, only evaluate the innermost piece to avoid
7532 : : building up unnecessary *_REFs. */
7533 : 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7534 : : non_constant_p, overflow_p,
7535 : : jump_target);
7536 : 0 : evaluated = true;
7537 : 0 : if (*jump_target)
7538 : : return NULL_TREE;
7539 : 0 : if (*non_constant_p)
7540 : : return t;
7541 : : }
7542 : :
7543 : : /* Find the underlying variable. */
7544 : 26589800 : releasing_vec refs;
7545 : 26589800 : tree object = NULL_TREE;
7546 : : /* If we're modifying a const object, save it. */
7547 : 26589800 : tree const_object_being_modified = NULL_TREE;
7548 : 26589800 : bool mutable_p = false;
7549 : : /* If we see a union, we can't ignore clobbers. */
7550 : 26589800 : int seen_union = 0;
7551 : 89296566 : for (tree probe = target; object == NULL_TREE; )
7552 : : {
7553 : 62707058 : switch (TREE_CODE (probe))
7554 : : {
7555 : 9527696 : case BIT_FIELD_REF:
7556 : 9527696 : case COMPONENT_REF:
7557 : 9527696 : case ARRAY_REF:
7558 : 9527696 : {
7559 : 9527696 : tree ob = TREE_OPERAND (probe, 0);
7560 : 9527696 : tree elt = TREE_OPERAND (probe, 1);
7561 : 9527696 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7562 : : mutable_p = true;
7563 : 9527696 : if (TREE_CODE (probe) == ARRAY_REF)
7564 : : {
7565 : 1289716 : elt = eval_and_check_array_index (ctx, probe, false,
7566 : : non_constant_p, overflow_p,
7567 : : jump_target);
7568 : 1289716 : if (*jump_target)
7569 : 66 : return NULL_TREE;
7570 : 1289716 : if (*non_constant_p)
7571 : : return t;
7572 : : }
7573 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7574 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7575 : : the array isn't const. Instead, check "a" in the next iteration;
7576 : : that will detect modifying "const int a[10]". */
7577 : 8237980 : else if (evaluated
7578 : 3080655 : && modifying_const_object_p (TREE_CODE (t), probe,
7579 : : mutable_p)
7580 : 8238251 : && const_object_being_modified == NULL_TREE)
7581 : : const_object_being_modified = probe;
7582 : :
7583 : : /* Track named member accesses for unions to validate modifications
7584 : : that change active member. */
7585 : 9527630 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7586 : 5157325 : vec_safe_push (refs, probe);
7587 : : else
7588 : 4370305 : vec_safe_push (refs, NULL_TREE);
7589 : :
7590 : 9527630 : vec_safe_push (refs, elt);
7591 : 9527630 : vec_safe_push (refs, TREE_TYPE (probe));
7592 : 9527630 : probe = ob;
7593 : 9527630 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
7594 : 191122 : ++seen_union;
7595 : : }
7596 : 9527630 : break;
7597 : :
7598 : 15 : case REALPART_EXPR:
7599 : 15 : gcc_assert (refs->is_empty ());
7600 : 15 : vec_safe_push (refs, NULL_TREE);
7601 : 15 : vec_safe_push (refs, probe);
7602 : 15 : vec_safe_push (refs, TREE_TYPE (probe));
7603 : 15 : probe = TREE_OPERAND (probe, 0);
7604 : 15 : break;
7605 : :
7606 : 17 : case IMAGPART_EXPR:
7607 : 17 : gcc_assert (refs->is_empty ());
7608 : 17 : vec_safe_push (refs, NULL_TREE);
7609 : 17 : vec_safe_push (refs, probe);
7610 : 17 : vec_safe_push (refs, TREE_TYPE (probe));
7611 : 17 : probe = TREE_OPERAND (probe, 0);
7612 : 17 : break;
7613 : :
7614 : 53179330 : default:
7615 : 53179330 : if (evaluated)
7616 : : object = probe;
7617 : : else
7618 : : {
7619 : 26589822 : tree pvar = tree_strip_any_location_wrapper (probe);
7620 : 26589822 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7621 : : {
7622 : : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7623 : : active union member. */
7624 : 55 : probe = DECL_VALUE_EXPR (pvar);
7625 : 55 : break;
7626 : : }
7627 : 26589767 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7628 : : non_constant_p, overflow_p,
7629 : : jump_target);
7630 : 26589767 : evaluated = true;
7631 : 26589767 : if (*jump_target)
7632 : : return NULL_TREE;
7633 : 26589767 : if (*non_constant_p)
7634 : : return t;
7635 : : }
7636 : : break;
7637 : : }
7638 : : }
7639 : :
7640 : 26589508 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7641 : 26589508 : && const_object_being_modified == NULL_TREE)
7642 : 26589508 : const_object_being_modified = object;
7643 : :
7644 : 26589508 : if (DECL_P (object)
7645 : 26589471 : && TREE_CLOBBER_P (init)
7646 : 26648817 : && DECL_NAME (object) == heap_deleted_identifier)
7647 : : /* Ignore clobbers of deleted allocations for now; we'll get a better error
7648 : : message later when operator delete is called. */
7649 : 15 : return void_node;
7650 : :
7651 : : /* And then find/build up our initializer for the path to the subobject
7652 : : we're initializing. */
7653 : 26589493 : tree *valp;
7654 : 26589493 : if (DECL_P (object))
7655 : 26589456 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7656 : : else
7657 : 37 : valp = NULL;
7658 : 26589493 : if (!valp)
7659 : : {
7660 : : /* A constant-expression cannot modify objects from outside the
7661 : : constant-expression. */
7662 : 4314 : if (!ctx->quiet)
7663 : : {
7664 : 25 : auto_diagnostic_group d;
7665 : 25 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
7666 : : {
7667 : 0 : error ("modification of allocated storage after deallocation "
7668 : : "is not a constant expression");
7669 : 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
7670 : : }
7671 : 25 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
7672 : : {
7673 : 10 : if (TREE_CLOBBER_P (init))
7674 : 3 : error ("destroying %qE outside its lifetime", object);
7675 : : else
7676 : 7 : error ("modification of %qE outside its lifetime "
7677 : : "is not a constant expression", object);
7678 : 10 : inform (DECL_SOURCE_LOCATION (object), "declared here");
7679 : : }
7680 : : else
7681 : : {
7682 : 15 : if (TREE_CLOBBER_P (init))
7683 : 6 : error ("destroying %qE from outside current evaluation "
7684 : : "is not a constant expression", object);
7685 : : else
7686 : 9 : error ("modification of %qE from outside current evaluation "
7687 : : "is not a constant expression", object);
7688 : : }
7689 : 25 : }
7690 : 4314 : *non_constant_p = true;
7691 : 4314 : return t;
7692 : : }
7693 : :
7694 : : /* Handle explicit end-of-lifetime. */
7695 : 26585179 : if (TREE_CLOBBER_P (init))
7696 : : {
7697 : 59246 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
7698 : 59246 : && refs->is_empty ())
7699 : : {
7700 : 27933 : ctx->global->destroy_value (object);
7701 : 27933 : return void_node;
7702 : : }
7703 : :
7704 : 29098 : if (!seen_union && !*valp
7705 : 35358 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
7706 : 4015 : return void_node;
7707 : :
7708 : : /* Ending the lifetime of a const object is OK. */
7709 : : const_object_being_modified = NULL_TREE;
7710 : : }
7711 : :
7712 : 26553231 : type = TREE_TYPE (object);
7713 : 26553231 : bool no_zero_init = true;
7714 : 26553231 : bool zero_padding_bits = false;
7715 : :
7716 : 53106462 : auto_vec<tree *> ctors;
7717 : 53106462 : releasing_vec indexes;
7718 : 53106462 : auto_vec<int> index_pos_hints;
7719 : 26553231 : bool activated_union_member_p = false;
7720 : 26553231 : bool empty_base = false;
7721 : 36030959 : while (!refs->is_empty ())
7722 : : {
7723 : 9519329 : if (*valp == NULL_TREE)
7724 : : {
7725 : 1120018 : *valp = build_constructor (type, NULL);
7726 : 1120018 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7727 : 1120018 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7728 : : }
7729 : 8399311 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
7730 : 8399311 : TREE_CODE (*valp) == STRING_CST)
7731 : : {
7732 : : /* An array was initialized with a string constant, and now
7733 : : we're writing into one of its elements. Explode the
7734 : : single initialization into a set of element
7735 : : initializations. */
7736 : 3987 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7737 : :
7738 : 3987 : tree string = *valp;
7739 : 3987 : tree elt_type = TREE_TYPE (type);
7740 : 3987 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
7741 : 3987 : / TYPE_PRECISION (char_type_node));
7742 : 3987 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
7743 : 3987 : tree ary_ctor = build_constructor (type, NULL);
7744 : :
7745 : 3987 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
7746 : 8135 : for (unsigned ix = 0; ix != num_elts; ix++)
7747 : : {
7748 : 4148 : constructor_elt elt =
7749 : : {
7750 : 4148 : build_int_cst (size_type_node, ix),
7751 : 4148 : extract_string_elt (string, chars_per_elt, ix)
7752 : 4148 : };
7753 : 4148 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
7754 : : }
7755 : :
7756 : 3987 : *valp = ary_ctor;
7757 : : }
7758 : :
7759 : 9519329 : enum tree_code code = TREE_CODE (type);
7760 : 9519329 : tree reftype = refs->pop();
7761 : 9519329 : tree index = refs->pop();
7762 : 9519329 : bool is_access_expr = refs->pop() != NULL_TREE;
7763 : :
7764 : 9519329 : if (code == COMPLEX_TYPE)
7765 : : {
7766 : 32 : if (TREE_CODE (*valp) == COMPLEX_CST)
7767 : 30 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
7768 : 30 : TREE_IMAGPART (*valp));
7769 : 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
7770 : 1 : && CONSTRUCTOR_NELTS (*valp) == 0
7771 : 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
7772 : : {
7773 : 1 : tree r = build_constructor (reftype, NULL);
7774 : 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
7775 : 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
7776 : : }
7777 : 32 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
7778 : 32 : ctors.safe_push (valp);
7779 : 32 : vec_safe_push (indexes, index);
7780 : 32 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
7781 : 32 : gcc_checking_assert (refs->is_empty ());
7782 : : type = reftype;
7783 : 40978 : break;
7784 : : }
7785 : :
7786 : : /* If the value of object is already zero-initialized, any new ctors for
7787 : : subobjects will also be zero-initialized. Similarly with zeroing of
7788 : : padding bits. */
7789 : 9519297 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
7790 : 9519297 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
7791 : :
7792 : 9519297 : if (code == RECORD_TYPE && is_empty_field (index))
7793 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
7794 : : have no data and might have an offset lower than previously declared
7795 : : fields, which confuses the middle-end. The code below will notice
7796 : : that we don't have a CONSTRUCTOR for our inner target and just
7797 : : return init. */
7798 : : {
7799 : : empty_base = true;
7800 : : break;
7801 : : }
7802 : :
7803 : : /* If a union is zero-initialized, its first non-static named data member
7804 : : is zero-initialized (and therefore active). */
7805 : 9478351 : if (code == UNION_TYPE
7806 : 9478351 : && !no_zero_init
7807 : 9478351 : && CONSTRUCTOR_NELTS (*valp) == 0)
7808 : 75 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
7809 : 75 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
7810 : :
7811 : : /* Check for implicit change of active member for a union. */
7812 : :
7813 : : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
7814 : : now allow initializing an array element to activate the array. */
7815 : 9512417 : auto only_array_refs = [](const releasing_vec &refs)
7816 : : {
7817 : 34070 : for (unsigned i = 1; i < refs->length(); i += 3)
7818 : 34 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
7819 : : return false;
7820 : : return true;
7821 : : };
7822 : :
7823 : 9478351 : if (code == UNION_TYPE
7824 : 191088 : && (CONSTRUCTOR_NELTS (*valp) == 0
7825 : 77345 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
7826 : : /* An INIT_EXPR of the last member in an access chain is always OK,
7827 : : but still check implicit change of members earlier on; see
7828 : : cpp2a/constexpr-union6.C. */
7829 : 9594746 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
7830 : : {
7831 : 82359 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
7832 : 82359 : tree inner = strip_array_types (reftype);
7833 : :
7834 : 82359 : if (has_active_member && cxx_dialect < cxx20)
7835 : : {
7836 : 68 : if (!ctx->quiet)
7837 : 22 : error_at (cp_expr_loc_or_input_loc (t),
7838 : : "change of the active member of a union "
7839 : : "from %qD to %qD is not a constant expression "
7840 : : "before C++20",
7841 : 22 : CONSTRUCTOR_ELT (*valp, 0)->index,
7842 : : index);
7843 : 68 : *non_constant_p = true;
7844 : : }
7845 : 82291 : else if (!is_access_expr
7846 : 5371 : || (TREE_CLOBBER_P (init)
7847 : 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
7848 : 87662 : || (TREE_CODE (t) == MODIFY_EXPR
7849 : 5359 : && CLASS_TYPE_P (inner)
7850 : 15 : && !type_has_non_deleted_trivial_default_ctor (inner)))
7851 : : {
7852 : : /* Diagnose changing active union member after initialization
7853 : : without a valid member access expression, as described in
7854 : : [class.union.general] p5. */
7855 : 76929 : if (!ctx->quiet)
7856 : : {
7857 : 33 : auto_diagnostic_group d;
7858 : 33 : if (has_active_member)
7859 : 24 : error_at (cp_expr_loc_or_input_loc (t),
7860 : : "accessing %qD member instead of initialized "
7861 : : "%qD member in constant expression",
7862 : 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
7863 : : else
7864 : 9 : error_at (cp_expr_loc_or_input_loc (t),
7865 : : "accessing uninitialized member %qD",
7866 : : index);
7867 : 33 : if (is_access_expr)
7868 : 3 : inform (DECL_SOURCE_LOCATION (index),
7869 : : "%qD does not implicitly begin its lifetime "
7870 : : "because %qT does not have a non-deleted "
7871 : : "trivial default constructor, use "
7872 : : "%<std::construct_at%> instead",
7873 : : index, inner);
7874 : : else
7875 : 30 : inform (DECL_SOURCE_LOCATION (index),
7876 : : "initializing %qD requires a member access "
7877 : : "expression as the left operand of the assignment",
7878 : : index);
7879 : 33 : }
7880 : 76929 : *non_constant_p = true;
7881 : : }
7882 : 5362 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
7883 : : {
7884 : : /* Diagnose changing the active union member while the union
7885 : : is in the process of being initialized. */
7886 : 9 : if (!ctx->quiet)
7887 : 3 : error_at (cp_expr_loc_or_input_loc (t),
7888 : : "change of the active member of a union "
7889 : : "from %qD to %qD during initialization",
7890 : 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
7891 : : index);
7892 : 9 : *non_constant_p = true;
7893 : : }
7894 : : no_zero_init = true;
7895 : : }
7896 : :
7897 : 9478351 : ctors.safe_push (valp);
7898 : 9478351 : vec_safe_push (indexes, index);
7899 : :
7900 : : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
7901 : : uninitialized. */
7902 : 8965651 : int pos = (!seen_union && TREE_CLOBBER_P (init)
7903 : 30991 : && CONSTRUCTOR_NO_CLEARING (*valp)
7904 : 9507060 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
7905 : 9478351 : constructor_elt *cep
7906 : 9478351 : = get_or_insert_ctor_field (*valp, index, pos);
7907 : 9478351 : if (cep == nullptr)
7908 : 623 : return void_node;
7909 : 9477728 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
7910 : :
7911 : 9477728 : if (code == UNION_TYPE)
7912 : : {
7913 : 191088 : activated_union_member_p = true;
7914 : 191088 : --seen_union;
7915 : : }
7916 : :
7917 : 9477728 : valp = &cep->value;
7918 : 9477728 : type = reftype;
7919 : : }
7920 : :
7921 : : /* Change an "as-base" clobber to the real type;
7922 : : we don't need to worry about padding in constexpr. */
7923 : 26552608 : tree itype = initialized_type (init);
7924 : 26552608 : if (IS_FAKE_BASE_TYPE (itype))
7925 : 77 : itype = TYPE_CONTEXT (itype);
7926 : :
7927 : : /* For initialization of an empty base, the original target will be
7928 : : *(base*)this, evaluation of which resolves to the object
7929 : : argument, which has the derived type rather than the base type. */
7930 : 53064270 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
7931 : 26511662 : (itype, type)))
7932 : : {
7933 : 6378 : gcc_assert (is_empty_class (TREE_TYPE (target)));
7934 : : empty_base = true;
7935 : : }
7936 : :
7937 : : /* Detect modifying a constant object in constexpr evaluation.
7938 : : We have found a const object that is being modified. Figure out
7939 : : if we need to issue an error. Consider
7940 : :
7941 : : struct A {
7942 : : int n;
7943 : : constexpr A() : n(1) { n = 2; } // #1
7944 : : };
7945 : : struct B {
7946 : : const A a;
7947 : : constexpr B() { a.n = 3; } // #2
7948 : : };
7949 : : constexpr B b{};
7950 : :
7951 : : #1 is OK, since we're modifying an object under construction, but
7952 : : #2 is wrong, since "a" is const and has been fully constructed.
7953 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
7954 : : which means that the object is read-only. For the example above, the
7955 : : *ctors stack at the point of #2 will look like:
7956 : :
7957 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
7958 : : ctors[1] = {.n=2} TREE_READONLY = 1
7959 : :
7960 : : and we're modifying "b.a", so we search the stack and see if the
7961 : : constructor for "b.a" has already run. */
7962 : 26552608 : if (const_object_being_modified)
7963 : : {
7964 : 6772 : bool fail = false;
7965 : 6772 : tree const_objtype
7966 : 6772 : = strip_array_types (TREE_TYPE (const_object_being_modified));
7967 : 6772 : if (!CLASS_TYPE_P (const_objtype))
7968 : : fail = true;
7969 : : else
7970 : : {
7971 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
7972 : : volatile, or const volatile object. const and volatile
7973 : : semantics are not applied on an object under construction.
7974 : : They come into effect when the constructor for the most
7975 : : derived object ends." */
7976 : 20170 : for (tree *elt : ctors)
7977 : 6836 : if (same_type_ignoring_top_level_qualifiers_p
7978 : 6836 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
7979 : : {
7980 : 6667 : fail = TREE_READONLY (*elt);
7981 : 6667 : break;
7982 : : }
7983 : : }
7984 : 6667 : if (fail)
7985 : : {
7986 : 198 : if (!ctx->quiet)
7987 : 63 : modifying_const_object_error (t, const_object_being_modified);
7988 : 198 : *non_constant_p = true;
7989 : 198 : return t;
7990 : : }
7991 : : }
7992 : :
7993 : 26552410 : if (!preeval)
7994 : : {
7995 : : /* We're handling an INIT_EXPR of class type, so the value of the
7996 : : initializer can depend on the object it's initializing. */
7997 : :
7998 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
7999 : : wants to modify it. */
8000 : 7455643 : if (*valp == NULL_TREE)
8001 : : {
8002 : 7286943 : *valp = build_constructor (type, NULL);
8003 : 7286943 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8004 : 7286943 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8005 : : }
8006 : 7455643 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8007 : 7455643 : new_ctx.object = target;
8008 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8009 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8010 : : expansion of those trees uses ctx instead. */
8011 : 7455643 : if (TREE_CODE (init) == TARGET_EXPR)
8012 : 1627468 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8013 : 1627468 : init = tinit;
8014 : 7455643 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8015 : : non_constant_p, overflow_p,
8016 : : jump_target);
8017 : 7455643 : if (*jump_target)
8018 : : return NULL_TREE;
8019 : : /* The hash table might have moved since the get earlier, and the
8020 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
8021 : : recompute VALP. */
8022 : 7455621 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8023 : 8330080 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8024 : : {
8025 : 874459 : ctors[i] = valp;
8026 : 874459 : constructor_elt *cep
8027 : 874459 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8028 : 874459 : valp = &cep->value;
8029 : : }
8030 : : }
8031 : :
8032 : 26552388 : if (*non_constant_p)
8033 : : return t;
8034 : :
8035 : : /* Don't share a CONSTRUCTOR that might be changed later. */
8036 : 25524240 : init = unshare_constructor (init);
8037 : :
8038 : 25524240 : gcc_checking_assert (!*valp
8039 : : || *valp == void_node
8040 : : || (same_type_ignoring_top_level_qualifiers_p
8041 : : (TREE_TYPE (*valp), type)));
8042 : 25524240 : if (empty_base)
8043 : : {
8044 : : /* Just evaluate the initializer and return, since there's no actual data
8045 : : to store, and we didn't build a CONSTRUCTOR. */
8046 : 44284 : if (!*valp)
8047 : : {
8048 : : /* But do make sure we have something in *valp. */
8049 : 0 : *valp = build_constructor (type, nullptr);
8050 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8051 : 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8052 : : }
8053 : : }
8054 : 25479956 : else if (TREE_CLOBBER_P (init))
8055 : : {
8056 : 26675 : if (AGGREGATE_TYPE_P (type))
8057 : : {
8058 : 13704 : if (*valp)
8059 : 13684 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8060 : : else
8061 : 20 : *valp = build_constructor (type, nullptr);
8062 : 13704 : TREE_CONSTANT (*valp) = true;
8063 : 13704 : TREE_SIDE_EFFECTS (*valp) = false;
8064 : 13704 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8065 : 13704 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8066 : : }
8067 : : else
8068 : 12971 : *valp = void_node;
8069 : : }
8070 : 25453281 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8071 : 6510499 : && TREE_CODE (init) == CONSTRUCTOR)
8072 : : {
8073 : : /* An outer ctx->ctor might be pointing to *valp, so replace
8074 : : its contents. */
8075 : 2106721 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8076 : 2106721 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8077 : 2106721 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8078 : 6320163 : CONSTRUCTOR_NO_CLEARING (*valp)
8079 : 2106721 : = CONSTRUCTOR_NO_CLEARING (init);
8080 : 6320163 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8081 : 2106721 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8082 : : }
8083 : : else
8084 : 23346560 : *valp = init;
8085 : :
8086 : : /* After initialization, 'const' semantics apply to the value of the
8087 : : object. Make a note of this fact by marking the CONSTRUCTOR
8088 : : TREE_READONLY. */
8089 : 25524240 : if (TREE_CODE (t) == INIT_EXPR
8090 : 19148016 : && !empty_base
8091 : 19103732 : && TREE_CODE (*valp) == CONSTRUCTOR
8092 : 27580665 : && TYPE_READONLY (type))
8093 : : {
8094 : 20949 : tree target_type = TREE_TYPE (target);
8095 : 20949 : if (IS_FAKE_BASE_TYPE (target_type))
8096 : 0 : target_type = TYPE_CONTEXT (target_type);
8097 : 20949 : if (INDIRECT_REF_P (target)
8098 : 36039 : && (is_this_parameter
8099 : 15090 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8100 : : /* We've just initialized '*this' (perhaps via the target
8101 : : constructor of a delegating constructor). Leave it up to the
8102 : : caller that set 'this' to set TREE_READONLY appropriately. */
8103 : 26 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8104 : : (target_type, type) || empty_base);
8105 : : else
8106 : 20923 : TREE_READONLY (*valp) = true;
8107 : : }
8108 : :
8109 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8110 : : CONSTRUCTORs, if any. */
8111 : 25524240 : bool c = TREE_CONSTANT (init);
8112 : 25524240 : bool s = TREE_SIDE_EFFECTS (init);
8113 : 25524240 : if (!indexes->is_empty ())
8114 : : {
8115 : 5725924 : tree last = indexes->last ();
8116 : 5725924 : if (TREE_CODE (last) == REALPART_EXPR
8117 : 5725924 : || TREE_CODE (last) == IMAGPART_EXPR)
8118 : : {
8119 : : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8120 : : possible. */
8121 : 32 : tree *cexpr = ctors.last ();
8122 : 32 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8123 : 32 : TREE_OPERAND (*cexpr, 0),
8124 : 32 : TREE_OPERAND (*cexpr, 1)))
8125 : 31 : *cexpr = c;
8126 : : else
8127 : : {
8128 : 2 : TREE_CONSTANT (*cexpr)
8129 : 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8130 : 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8131 : 2 : TREE_SIDE_EFFECTS (*cexpr)
8132 : 2 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8133 : 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8134 : : }
8135 : 32 : c = TREE_CONSTANT (*cexpr);
8136 : 32 : s = TREE_SIDE_EFFECTS (*cexpr);
8137 : : }
8138 : : }
8139 : 25524240 : if (!c || s || activated_union_member_p)
8140 : 12020282 : for (tree *elt : ctors)
8141 : : {
8142 : 1994817 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8143 : 0 : continue;
8144 : 1994817 : if (!c)
8145 : 1580699 : TREE_CONSTANT (*elt) = false;
8146 : 1994817 : if (s)
8147 : 0 : TREE_SIDE_EFFECTS (*elt) = true;
8148 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8149 : : this union. */
8150 : 1994817 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8151 : 114008 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8152 : : }
8153 : :
8154 : 25524240 : if (lval)
8155 : : return target;
8156 : : else
8157 : 169132 : return init;
8158 : 26589800 : }
8159 : :
8160 : : /* Evaluate a ++ or -- expression. */
8161 : :
8162 : : static tree
8163 : 1954546 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8164 : : value_cat lval,
8165 : : bool *non_constant_p, bool *overflow_p,
8166 : : tree *jump_target)
8167 : : {
8168 : 1954546 : enum tree_code code = TREE_CODE (t);
8169 : 1954546 : tree type = TREE_TYPE (t);
8170 : 1954546 : tree op = TREE_OPERAND (t, 0);
8171 : 1954546 : tree offset = TREE_OPERAND (t, 1);
8172 : 1954546 : gcc_assert (TREE_CONSTANT (offset));
8173 : :
8174 : : /* OFFSET is constant, but perhaps not constant enough. We need to
8175 : : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8176 : 1954546 : offset = fold_simple (offset);
8177 : :
8178 : : /* The operand as an lvalue. */
8179 : 1954546 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8180 : : non_constant_p, overflow_p,
8181 : : jump_target);
8182 : 1954546 : if (*jump_target)
8183 : : return NULL_TREE;
8184 : :
8185 : : /* The operand as an rvalue. */
8186 : 1954546 : tree val
8187 : 1954546 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8188 : : non_constant_p, overflow_p,
8189 : : jump_target);
8190 : 1954546 : if (*jump_target)
8191 : : return NULL_TREE;
8192 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8193 : : a local array in a constexpr function. */
8194 : 1954546 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8195 : 929086 : if (!ptr)
8196 : 929086 : VERIFY_CONSTANT (val);
8197 : :
8198 : : /* The modified value. */
8199 : 1917746 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8200 : 1917746 : tree mod;
8201 : 1917746 : if (INDIRECT_TYPE_P (type))
8202 : : {
8203 : : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8204 : 1025460 : offset = convert_to_ptrofftype (offset);
8205 : 1025460 : if (!inc)
8206 : 96303 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8207 : 1025460 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8208 : : }
8209 : 892286 : else if (c_promoting_integer_type_p (type)
8210 : 10074 : && !TYPE_UNSIGNED (type)
8211 : 892439 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8212 : : {
8213 : 153 : offset = fold_convert (integer_type_node, offset);
8214 : 153 : mod = fold_convert (integer_type_node, val);
8215 : 209 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8216 : : mod, offset);
8217 : 153 : mod = fold_convert (type, t);
8218 : 153 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8219 : 9 : TREE_OVERFLOW (mod) = false;
8220 : : }
8221 : : else
8222 : 999777 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8223 : 1917746 : if (!ptr)
8224 : 892286 : VERIFY_CONSTANT (mod);
8225 : :
8226 : : /* Storing the modified value. */
8227 : 2920009 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8228 : : MODIFY_EXPR, type, op, mod);
8229 : 1917746 : mod = cxx_eval_constant_expression (ctx, store, lval,
8230 : : non_constant_p, overflow_p,
8231 : : jump_target);
8232 : 1917746 : ggc_free (store);
8233 : 1917746 : if (*jump_target)
8234 : : return NULL_TREE;
8235 : 1917746 : if (*non_constant_p)
8236 : : return t;
8237 : :
8238 : : /* And the value of the expression. */
8239 : 1875820 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8240 : : /* Prefix ops are lvalues, but the caller might want an rvalue;
8241 : : lval has already been taken into account in the store above. */
8242 : : return mod;
8243 : : else
8244 : : /* Postfix ops are rvalues. */
8245 : 264439 : return val;
8246 : : }
8247 : :
8248 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8249 : : STMT matches *jump_target. If we're looking for a case label and we see
8250 : : the default label, note it in ctx->css_state. */
8251 : :
8252 : : static bool
8253 : 397091 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8254 : : {
8255 : 397091 : switch (TREE_CODE (*jump_target))
8256 : : {
8257 : 45 : case LABEL_DECL:
8258 : 45 : if (TREE_CODE (stmt) == LABEL_EXPR
8259 : 45 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8260 : : return true;
8261 : : break;
8262 : :
8263 : 393641 : case INTEGER_CST:
8264 : 393641 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8265 : : {
8266 : 393625 : gcc_assert (ctx->css_state != NULL);
8267 : 393625 : if (!CASE_LOW (stmt))
8268 : : {
8269 : : /* default: should appear just once in a SWITCH_EXPR
8270 : : body (excluding nested SWITCH_EXPR). */
8271 : 56642 : gcc_assert (*ctx->css_state != css_default_seen);
8272 : : /* When evaluating SWITCH_EXPR body for the second time,
8273 : : return true for the default: label. */
8274 : 56642 : if (*ctx->css_state == css_default_processing)
8275 : : return true;
8276 : 28333 : *ctx->css_state = css_default_seen;
8277 : : }
8278 : 336983 : else if (CASE_HIGH (stmt))
8279 : : {
8280 : 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8281 : 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8282 : : return true;
8283 : : }
8284 : 336963 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8285 : : return true;
8286 : : }
8287 : : break;
8288 : :
8289 : : case BREAK_STMT:
8290 : : case CONTINUE_STMT:
8291 : : /* These two are handled directly in cxx_eval_loop_expr by testing
8292 : : breaks (jump_target) or continues (jump_target). */
8293 : : break;
8294 : :
8295 : : case VAR_DECL:
8296 : : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8297 : : and other places by testing throws (jump_target). */
8298 : : break;
8299 : :
8300 : 0 : default:
8301 : 0 : gcc_unreachable ();
8302 : : }
8303 : : return false;
8304 : : }
8305 : :
8306 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8307 : : semantics, for switch, break, continue, and return. */
8308 : :
8309 : : static tree
8310 : 17327131 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8311 : : bool *non_constant_p, bool *overflow_p,
8312 : : tree *jump_target)
8313 : : {
8314 : : /* In a statement-expression we want to return the last value.
8315 : : For empty statement expression return void_node. */
8316 : 17327131 : tree r = void_node;
8317 : 40110618 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8318 : : {
8319 : 32588464 : tree stmt = *i;
8320 : :
8321 : : /* We've found a continue, so skip everything until we reach
8322 : : the label its jumping to. */
8323 : 32588464 : if (continues (jump_target))
8324 : : {
8325 : 3450 : if (label_matches (ctx, jump_target, stmt))
8326 : : /* Found it. */
8327 : 9 : *jump_target = NULL_TREE;
8328 : : else
8329 : 3441 : continue;
8330 : : }
8331 : 32585023 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8332 : 6578309 : continue;
8333 : :
8334 : 26006714 : value_cat lval = vc_discard;
8335 : : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8336 : 37559768 : if (tsi_one_before_end_p (i)
8337 : 11554224 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8338 : : lval = vc_prvalue;
8339 : :
8340 : 26006714 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8341 : : non_constant_p, overflow_p,
8342 : : jump_target);
8343 : 26006712 : if (*non_constant_p)
8344 : : break;
8345 : 20020692 : if (returns (jump_target)
8346 : 16225168 : || breaks (jump_target)
8347 : 22783487 : || throws (jump_target))
8348 : : break;
8349 : : }
8350 : 17327129 : return r;
8351 : : }
8352 : :
8353 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8354 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
8355 : :
8356 : : static tree
8357 : 984753 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8358 : : bool *non_constant_p, bool *overflow_p,
8359 : : tree *jump_target)
8360 : : {
8361 : 984753 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8362 : 984753 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8363 : 984753 : unsigned cond_cleanup_depth = 0;
8364 : 984753 : int count = 0;
8365 : 984753 : switch (TREE_CODE (t))
8366 : : {
8367 : 85 : case LOOP_EXPR:
8368 : 85 : body = LOOP_EXPR_BODY (t);
8369 : 85 : break;
8370 : 776238 : case DO_STMT:
8371 : 776238 : body = DO_BODY (t);
8372 : 776238 : cond = DO_COND (t);
8373 : 776238 : break;
8374 : 78408 : case WHILE_STMT:
8375 : 78408 : body = WHILE_BODY (t);
8376 : 78408 : cond = WHILE_COND (t);
8377 : 78408 : cond_prep = WHILE_COND_PREP (t);
8378 : 78408 : cond_cleanup = WHILE_COND_CLEANUP (t);
8379 : 78408 : count = -1;
8380 : 78408 : break;
8381 : 130022 : case FOR_STMT:
8382 : 130022 : if (FOR_INIT_STMT (t))
8383 : 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8384 : : non_constant_p, overflow_p, jump_target);
8385 : 130022 : if (*non_constant_p)
8386 : : return NULL_TREE;
8387 : 130022 : body = FOR_BODY (t);
8388 : 130022 : cond = FOR_COND (t);
8389 : 130022 : expr = FOR_EXPR (t);
8390 : 130022 : cond_prep = FOR_COND_PREP (t);
8391 : 130022 : cond_cleanup = FOR_COND_CLEANUP (t);
8392 : 130022 : count = -1;
8393 : 130022 : break;
8394 : 0 : default:
8395 : 0 : gcc_unreachable ();
8396 : : }
8397 : 984753 : if (cond_prep)
8398 : 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8399 : 9162959 : auto cleanup_cond = [&] {
8400 : : /* Clean up the condition variable after each iteration. */
8401 : 8178206 : if (cond_cleanup_depth && !*non_constant_p)
8402 : : {
8403 : 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8404 : 105 : tree s = BIND_EXPR_BODY (cond_prep);
8405 : 105 : unsigned i;
8406 : 210 : for (i = cond_cleanup_depth; i; --i)
8407 : : {
8408 : 105 : tree_stmt_iterator iter = tsi_last (s);
8409 : 105 : s = tsi_stmt (iter);
8410 : 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8411 : 105 : s = CLEANUP_BODY (s);
8412 : : }
8413 : 105 : tree c;
8414 : 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8415 : 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8416 : : overflow_p, jump_target);
8417 : 105 : }
8418 : 8178206 : if (cond_prep)
8419 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8420 : 222 : decl; decl = DECL_CHAIN (decl))
8421 : 111 : destroy_value_checked (ctx, decl, non_constant_p);
8422 : 984753 : };
8423 : 7402503 : do
8424 : : {
8425 : 7402503 : if (count != -1)
8426 : : {
8427 : 7194073 : if (body)
8428 : 7194073 : cxx_eval_constant_expression (ctx, body, vc_discard,
8429 : : non_constant_p, overflow_p,
8430 : : jump_target);
8431 : 7194073 : if (breaks (jump_target))
8432 : : {
8433 : 620 : *jump_target = NULL_TREE;
8434 : 620 : break;
8435 : : }
8436 : :
8437 : 7193453 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8438 : 694 : *jump_target = NULL_TREE;
8439 : :
8440 : 7193453 : if (expr)
8441 : 1292523 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8442 : : non_constant_p, overflow_p,
8443 : : jump_target);
8444 : 7193453 : cleanup_cond ();
8445 : : }
8446 : :
8447 : 7401883 : if (cond_prep)
8448 : : {
8449 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8450 : 222 : decl; decl = DECL_CHAIN (decl))
8451 : 111 : ctx->global->clear_value (decl);
8452 : 111 : if (cond_cleanup)
8453 : : {
8454 : : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8455 : : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8456 : : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8457 : : at the end of those STATEMENT_LISTs. */
8458 : 111 : cond_cleanup_depth = 0;
8459 : 111 : tree s = BIND_EXPR_BODY (cond_prep);
8460 : 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8461 : 222 : depth; --depth)
8462 : : {
8463 : 111 : for (tree_stmt_iterator i = tsi_start (s);
8464 : 321 : !tsi_end_p (i); ++i)
8465 : : {
8466 : 321 : tree stmt = *i;
8467 : 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8468 : 0 : continue;
8469 : 321 : if (tsi_one_before_end_p (i))
8470 : : {
8471 : : /* The last statement in the STATEMENT_LIST
8472 : : has to be a CLEANUP_STMT (verified in
8473 : : finish_loop_cond_prep). We want to
8474 : : evaluate just its CLEANUP_BODY part but not
8475 : : CLEANUP_EXPR part just yet. */
8476 : 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8477 : : /* If the CLEANUP_STMT is not actually to be
8478 : : evaluated, don't increment cond_cleanup_depth
8479 : : so that we don't evaluate the CLEANUP_EXPR
8480 : : for it later either. */
8481 : 105 : if (*jump_target)
8482 : : {
8483 : : depth = 1;
8484 : : break;
8485 : : }
8486 : 105 : ++cond_cleanup_depth;
8487 : : /* If not in the innermost one, next iteration
8488 : : will handle CLEANUP_BODY similarly. */
8489 : 105 : if (depth > 1)
8490 : : {
8491 : 0 : s = CLEANUP_BODY (stmt);
8492 : 0 : break;
8493 : : }
8494 : : /* The innermost one can be evaluated normally. */
8495 : 105 : cxx_eval_constant_expression (ctx,
8496 : 105 : CLEANUP_BODY (stmt),
8497 : : vc_discard,
8498 : : non_constant_p,
8499 : : overflow_p,
8500 : : jump_target);
8501 : 105 : break;
8502 : : }
8503 : : /* And so should be evaluated statements which aren't
8504 : : last in the STATEMENT_LIST. */
8505 : 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8506 : : non_constant_p, overflow_p,
8507 : : jump_target);
8508 : 216 : if (*non_constant_p
8509 : 210 : || returns (jump_target)
8510 : 210 : || breaks (jump_target)
8511 : 210 : || continues (jump_target)
8512 : 531 : || throws (jump_target))
8513 : : {
8514 : : depth = 1;
8515 : : break;
8516 : : }
8517 : : }
8518 : : }
8519 : : }
8520 : : else
8521 : 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8522 : : vc_discard, non_constant_p,
8523 : : overflow_p, jump_target);
8524 : : }
8525 : :
8526 : 7401883 : if (cond)
8527 : : {
8528 : 7400830 : tree res
8529 : 7400830 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8530 : : non_constant_p, overflow_p,
8531 : : jump_target);
8532 : 7400830 : if (res)
8533 : : {
8534 : 7364555 : if (verify_constant (res, ctx->quiet, non_constant_p,
8535 : : overflow_p))
8536 : : break;
8537 : 7141547 : if (integer_zerop (res))
8538 : : break;
8539 : : }
8540 : : else
8541 : 36275 : gcc_assert (*jump_target);
8542 : : }
8543 : :
8544 : 6454317 : if (++count >= constexpr_loop_limit)
8545 : : {
8546 : 27 : if (!ctx->quiet)
8547 : 9 : error_at (cp_expr_loc_or_input_loc (t),
8548 : : "%<constexpr%> loop iteration count exceeds limit of %d "
8549 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8550 : : constexpr_loop_limit);
8551 : 27 : *non_constant_p = true;
8552 : 27 : break;
8553 : : }
8554 : : }
8555 : 19326494 : while (!returns (jump_target)
8556 : 6417914 : && !breaks (jump_target)
8557 : 6417914 : && !continues (jump_target)
8558 : 6418007 : && (!switches (jump_target) || count == 0)
8559 : 6417884 : && !throws (jump_target)
8560 : 12908714 : && !*non_constant_p);
8561 : :
8562 : 984753 : cleanup_cond ();
8563 : :
8564 : 984753 : return NULL_TREE;
8565 : : }
8566 : :
8567 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8568 : : semantics. */
8569 : :
8570 : : static tree
8571 : 37275 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8572 : : bool *non_constant_p, bool *overflow_p,
8573 : : tree *jump_target)
8574 : : {
8575 : 37275 : tree cond
8576 : 37275 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8577 : 37275 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8578 : : non_constant_p, overflow_p,
8579 : : jump_target);
8580 : 37275 : if (*jump_target)
8581 : : return NULL_TREE;
8582 : 37275 : VERIFY_CONSTANT (cond);
8583 : 36957 : if (TREE_CODE (cond) != INTEGER_CST)
8584 : : {
8585 : : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8586 : : switch condition even if it's constant enough for other things
8587 : : (c++/113545). */
8588 : 0 : gcc_checking_assert (ctx->quiet);
8589 : 0 : *non_constant_p = true;
8590 : 0 : return t;
8591 : : }
8592 : :
8593 : 36957 : *jump_target = cond;
8594 : :
8595 : 36957 : tree body
8596 : 36957 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8597 : 36957 : constexpr_ctx new_ctx = *ctx;
8598 : 36957 : constexpr_switch_state css = css_default_not_seen;
8599 : 36957 : new_ctx.css_state = &css;
8600 : 36957 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8601 : : non_constant_p, overflow_p, jump_target);
8602 : 65310 : if (switches (jump_target) && css == css_default_seen)
8603 : : {
8604 : : /* If the SWITCH_EXPR body has default: label, process it once again,
8605 : : this time instructing label_matches to return true for default:
8606 : : label on switches (jump_target). */
8607 : 28309 : css = css_default_processing;
8608 : 28309 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8609 : : non_constant_p, overflow_p, jump_target);
8610 : : }
8611 : 36957 : if (breaks (jump_target) || switches (jump_target))
8612 : 22496 : *jump_target = NULL_TREE;
8613 : : return NULL_TREE;
8614 : : }
8615 : :
8616 : : /* Find the object of TYPE under initialization in CTX. */
8617 : :
8618 : : static tree
8619 : 16631 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8620 : : {
8621 : 16631 : if (!ctx)
8622 : : return NULL_TREE;
8623 : :
8624 : : /* Prefer the outermost matching object, but don't cross
8625 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8626 : 16299 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8627 : 510 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8628 : : return outer_ob;
8629 : :
8630 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8631 : : can is a minor optimization. */
8632 : 16121 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8633 : 300 : return ctx->ctor;
8634 : :
8635 : 15821 : if (!ctx->object)
8636 : : return NULL_TREE;
8637 : :
8638 : : /* Since an object cannot have a field of its own type, we can search outward
8639 : : from ctx->object to find the unique containing object of TYPE. */
8640 : : tree ob = ctx->object;
8641 : 15812 : while (ob)
8642 : : {
8643 : 15812 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
8644 : : break;
8645 : 223 : if (handled_component_p (ob))
8646 : 221 : ob = TREE_OPERAND (ob, 0);
8647 : : else
8648 : : ob = NULL_TREE;
8649 : : }
8650 : :
8651 : : return ob;
8652 : : }
8653 : :
8654 : : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
8655 : : true, we're checking a constexpr function body. */
8656 : :
8657 : : static void
8658 : 31 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
8659 : : {
8660 : 31 : auto_diagnostic_group d;
8661 : 31 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
8662 : : "constant expression"))
8663 : 31 : inform (loc, "only unevaluated inline assembly is allowed in a "
8664 : : "%<constexpr%> function in C++20");
8665 : 31 : }
8666 : :
8667 : : /* We're getting the constant value of DECL in a manifestly constant-evaluated
8668 : : context; maybe complain about that. */
8669 : :
8670 : : static void
8671 : 61079259 : maybe_warn_about_constant_value (location_t loc, tree decl)
8672 : : {
8673 : 61079259 : static bool explained = false;
8674 : 61079259 : if (cxx_dialect >= cxx17
8675 : 60923498 : && warn_interference_size
8676 : 60923498 : && !OPTION_SET_P (param_destruct_interfere_size)
8677 : 60923498 : && DECL_CONTEXT (decl) == std_node
8678 : 8084248 : && DECL_NAME (decl)
8679 : 8084242 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
8680 : 12 : && (LOCATION_FILE (input_location) != main_input_filename
8681 : 6 : || module_exporting_p ())
8682 : 61079262 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
8683 : 61079268 : && !explained)
8684 : : {
8685 : 6 : explained = true;
8686 : 6 : inform (loc, "its value can vary between compiler versions or "
8687 : : "with different %<-mtune%> or %<-mcpu%> flags");
8688 : 6 : inform (loc, "if this use is part of a public ABI, change it to "
8689 : : "instead use a constant variable you define");
8690 : 6 : inform (loc, "the default value for the current CPU tuning "
8691 : : "is %d bytes", param_destruct_interfere_size);
8692 : 6 : inform (loc, "you can stabilize this value with %<--param "
8693 : : "hardware_destructive_interference_size=%d%>, or disable "
8694 : : "this warning with %<-Wno-interference-size%>",
8695 : : param_destruct_interfere_size);
8696 : : }
8697 : 61079259 : }
8698 : :
8699 : : /* For element type ELT_TYPE, return the appropriate type of the heap object
8700 : : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
8701 : : in bytes. If COOKIE_SIZE is NULL, return array type
8702 : : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
8703 : : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
8704 : : where N is computed such that the size of the struct fits into FULL_SIZE.
8705 : : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
8706 : : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
8707 : : will be also 0 and so it is not possible to determine the actual array
8708 : : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
8709 : : expression evaluation of subexpressions of ARG_SIZE. */
8710 : :
8711 : : static tree
8712 : 9986 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
8713 : : tree cookie_size, tree full_size, tree arg_size,
8714 : : bool *non_constant_p, bool *overflow_p,
8715 : : tree *jump_target)
8716 : : {
8717 : 9986 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
8718 : 9986 : gcc_assert (tree_fits_uhwi_p (full_size));
8719 : 9986 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
8720 : 9986 : if (arg_size)
8721 : : {
8722 : 9 : STRIP_NOPS (arg_size);
8723 : 9 : if (cookie_size)
8724 : : {
8725 : 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
8726 : : arg_size = NULL_TREE;
8727 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
8728 : 0 : && tree_int_cst_equal (cookie_size,
8729 : 0 : TREE_OPERAND (arg_size, 0)))
8730 : : {
8731 : 0 : arg_size = TREE_OPERAND (arg_size, 1);
8732 : 0 : STRIP_NOPS (arg_size);
8733 : : }
8734 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
8735 : 0 : && tree_int_cst_equal (cookie_size,
8736 : 0 : TREE_OPERAND (arg_size, 1)))
8737 : : {
8738 : 0 : arg_size = TREE_OPERAND (arg_size, 0);
8739 : 0 : STRIP_NOPS (arg_size);
8740 : : }
8741 : : else
8742 : : arg_size = NULL_TREE;
8743 : : }
8744 : 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
8745 : : {
8746 : 9 : tree op0 = TREE_OPERAND (arg_size, 0);
8747 : 9 : tree op1 = TREE_OPERAND (arg_size, 1);
8748 : 9 : if (integer_zerop (op0))
8749 : 9 : arg_size
8750 : 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
8751 : : non_constant_p, overflow_p,
8752 : : jump_target);
8753 : 0 : else if (integer_zerop (op1))
8754 : 0 : arg_size
8755 : 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
8756 : : non_constant_p, overflow_p,
8757 : : jump_target);
8758 : : else
8759 : : arg_size = NULL_TREE;
8760 : 9 : if (*jump_target)
8761 : : return NULL_TREE;
8762 : : }
8763 : : else
8764 : : arg_size = NULL_TREE;
8765 : : }
8766 : :
8767 : 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
8768 : 9986 : if (!arg_size)
8769 : : {
8770 : 9977 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
8771 : 9977 : gcc_assert (fsz >= csz);
8772 : 9977 : fsz -= csz;
8773 : 9977 : if (esz)
8774 : 9977 : fsz /= esz;
8775 : : }
8776 : 9986 : tree itype2 = build_index_type (size_int (fsz - 1));
8777 : 9986 : if (!cookie_size)
8778 : 9977 : return build_cplus_array_type (elt_type, itype2);
8779 : 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
8780 : : }
8781 : :
8782 : : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
8783 : : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
8784 : : the expression which needed the cleanup threw. If both threw, diagnose
8785 : : it and return NULL, otherwise return R. If only the cleanup threw, set
8786 : : *JUMP_TARGET to the exception object from the cleanup. */
8787 : :
8788 : : static tree
8789 : 77985 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
8790 : : bool *non_constant_p, tree *jump_target, tree jmp_target)
8791 : : {
8792 : 77986 : if (!throws (&jmp_target))
8793 : : return r;
8794 : 7 : if (throws (jump_target))
8795 : : {
8796 : : /* [except.throw]/9 - If the exception handling mechanism
8797 : : handling an uncaught exception directly invokes a function
8798 : : that exits via an exception, the function std::terminate is
8799 : : invoked. */
8800 : 6 : if (!ctx->quiet)
8801 : : {
8802 : 2 : auto_diagnostic_group d;
8803 : 2 : diagnose_std_terminate (loc, ctx, *jump_target);
8804 : 2 : inform (loc, "destructor exited with an exception");
8805 : 2 : }
8806 : 6 : *non_constant_p = true;
8807 : 6 : *jump_target = NULL_TREE;
8808 : 6 : return NULL_TREE;
8809 : : }
8810 : 1 : *jump_target = jmp_target;
8811 : 1 : return r;
8812 : : }
8813 : :
8814 : : /* Attempt to reduce the expression T to a constant value.
8815 : : On failure, issue diagnostic and return error_mark_node. */
8816 : : /* FIXME unify with c_fully_fold */
8817 : : /* FIXME overflow_p is too global */
8818 : :
8819 : : static tree
8820 : 1234826692 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
8821 : : value_cat lval,
8822 : : bool *non_constant_p, bool *overflow_p,
8823 : : tree *jump_target)
8824 : : {
8825 : 1234826692 : if (*jump_target)
8826 : : {
8827 : : /* If we are jumping, ignore all statements/expressions except those
8828 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
8829 : 1440070 : switch (TREE_CODE (t))
8830 : : {
8831 : : case BIND_EXPR:
8832 : : case STATEMENT_LIST:
8833 : : case LOOP_EXPR:
8834 : : case COND_EXPR:
8835 : : case IF_STMT:
8836 : : case DO_STMT:
8837 : : case WHILE_STMT:
8838 : : case FOR_STMT:
8839 : : break;
8840 : 393641 : case LABEL_EXPR:
8841 : 393641 : case CASE_LABEL_EXPR:
8842 : 393641 : if (label_matches (ctx, jump_target, t))
8843 : : /* Found it. */
8844 : 36913 : *jump_target = NULL_TREE;
8845 : 393641 : return NULL_TREE;
8846 : : default:
8847 : : return NULL_TREE;
8848 : : }
8849 : : }
8850 : 1233611104 : if (error_operand_p (t))
8851 : : {
8852 : 133 : *non_constant_p = true;
8853 : 133 : return t;
8854 : : }
8855 : :
8856 : : /* Change the input location to the currently processed expression for
8857 : : better error messages when a subexpression has no location. */
8858 : 1233610971 : location_t loc = cp_expr_loc_or_input_loc (t);
8859 : 2467216529 : iloc_sentinel sentinel (loc);
8860 : :
8861 : 1233610971 : STRIP_ANY_LOCATION_WRAPPER (t);
8862 : :
8863 : 1233610971 : if (CONSTANT_CLASS_P (t))
8864 : : {
8865 : 261367754 : if (TREE_OVERFLOW (t))
8866 : : {
8867 : 62 : if (!ctx->quiet)
8868 : 15 : permerror (input_location, "overflow in constant expression");
8869 : 62 : if (!flag_permissive || ctx->quiet)
8870 : 59 : *overflow_p = true;
8871 : : }
8872 : :
8873 : 261367754 : if (TREE_CODE (t) == INTEGER_CST
8874 : 252085095 : && TYPE_PTR_P (TREE_TYPE (t))
8875 : : /* INTEGER_CST with pointer-to-method type is only used
8876 : : for a virtual method in a pointer to member function.
8877 : : Don't reject those. */
8878 : 867860 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
8879 : 262235300 : && !integer_zerop (t))
8880 : : {
8881 : 6 : if (!ctx->quiet)
8882 : 0 : error ("value %qE of type %qT is not a constant expression",
8883 : 0 : t, TREE_TYPE (t));
8884 : 6 : *non_constant_p = true;
8885 : : }
8886 : :
8887 : 261367754 : return t;
8888 : : }
8889 : :
8890 : : /* Avoid excessively long constexpr evaluations. */
8891 : 972243217 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
8892 : : {
8893 : 9 : if (!ctx->quiet)
8894 : 3 : error_at (loc,
8895 : : "%<constexpr%> evaluation operation count exceeds limit of "
8896 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
8897 : : constexpr_ops_limit);
8898 : 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
8899 : 9 : *non_constant_p = true;
8900 : 9 : return t;
8901 : : }
8902 : :
8903 : 972243208 : constexpr_ctx new_ctx;
8904 : 972243208 : tree r = t;
8905 : :
8906 : 972243208 : tree_code tcode = TREE_CODE (t);
8907 : 972243208 : switch (tcode)
8908 : : {
8909 : 15687298 : case RESULT_DECL:
8910 : 15687298 : if (lval)
8911 : : return t;
8912 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
8913 : : through an invisible reference, or in named return value
8914 : : optimization. */
8915 : 38960 : if (tree v = ctx->global->get_value (t))
8916 : : return v;
8917 : : else
8918 : : {
8919 : 3 : if (!ctx->quiet)
8920 : 0 : error ("%qE is not a constant expression", t);
8921 : 3 : *non_constant_p = true;
8922 : : }
8923 : 3 : break;
8924 : :
8925 : 138691136 : case VAR_DECL:
8926 : 138691136 : if (DECL_HAS_VALUE_EXPR_P (t))
8927 : : {
8928 : 1315178 : if (is_normal_capture_proxy (t)
8929 : 1315178 : && current_function_decl == DECL_CONTEXT (t))
8930 : : {
8931 : : /* Function parms aren't constexpr within the function
8932 : : definition, so don't try to look at the closure. But if the
8933 : : captured variable is constant, try to evaluate it directly. */
8934 : 395532 : r = DECL_CAPTURED_VARIABLE (t);
8935 : : }
8936 : : else
8937 : 919646 : r = DECL_VALUE_EXPR (t);
8938 : :
8939 : 1315178 : tree type = TREE_TYPE (t);
8940 : 1315178 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
8941 : : {
8942 : : /* Adjust r to match the reference-ness of t. */
8943 : 247071 : if (TYPE_REF_P (type))
8944 : 247020 : r = build_address (r);
8945 : : else
8946 : 51 : r = convert_from_reference (r);
8947 : : }
8948 : 1315178 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
8949 : 1315178 : overflow_p, jump_target);
8950 : : }
8951 : : /* fall through */
8952 : 145196447 : case CONST_DECL:
8953 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
8954 : : CONST_DECL for aggregate constants. */
8955 : 145196447 : if (lval)
8956 : : return t;
8957 : 114675786 : else if (t == ctx->object)
8958 : 538347 : return ctx->ctor;
8959 : 114137439 : if (VAR_P (t))
8960 : : {
8961 : 106316950 : if (tree v = ctx->global->get_value (t))
8962 : : {
8963 : : r = v;
8964 : : break;
8965 : : }
8966 : 91779906 : if (ctx->global->is_outside_lifetime (t))
8967 : : {
8968 : 96 : if (!ctx->quiet)
8969 : 28 : outside_lifetime_error (loc, t);
8970 : 96 : *non_constant_p = true;
8971 : 96 : break;
8972 : : }
8973 : : }
8974 : 99600299 : if (ctx->manifestly_const_eval == mce_true)
8975 : 61079259 : maybe_warn_about_constant_value (loc, t);
8976 : 99600299 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
8977 : 99600299 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
8978 : : {
8979 : : /* If the class is empty, we aren't actually loading anything. */
8980 : 87557 : r = build_constructor (TREE_TYPE (t), NULL);
8981 : 87557 : TREE_CONSTANT (r) = true;
8982 : : }
8983 : 99512742 : else if (ctx->strict)
8984 : 99234445 : r = decl_really_constant_value (t, /*unshare_p=*/false);
8985 : : else
8986 : 278297 : r = decl_constant_value (t, /*unshare_p=*/false);
8987 : 99597602 : if (TREE_CODE (r) == TARGET_EXPR
8988 : 99597602 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
8989 : 0 : r = TARGET_EXPR_INITIAL (r);
8990 : 99597602 : if (DECL_P (r)
8991 : : /* P2280 allows references to unknown. */
8992 : 99752166 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
8993 : : {
8994 : 28047390 : if (!ctx->quiet)
8995 : 168 : non_const_var_error (loc, r, /*fundef_p*/false);
8996 : 28047390 : *non_constant_p = true;
8997 : : }
8998 : : break;
8999 : :
9000 : : case DEBUG_BEGIN_STMT:
9001 : : /* ??? It might be nice to retain this information somehow, so
9002 : : as to be able to step into a constexpr function call. */
9003 : : /* Fall through. */
9004 : :
9005 : : case FUNCTION_DECL:
9006 : : case TEMPLATE_DECL:
9007 : : case LABEL_DECL:
9008 : : case LABEL_EXPR:
9009 : : case CASE_LABEL_EXPR:
9010 : : case PREDICT_EXPR:
9011 : : case OMP_DECLARE_MAPPER:
9012 : : return t;
9013 : :
9014 : 110637002 : case PARM_DECL:
9015 : 110637002 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9016 : : {
9017 : : /* glvalue use. */
9018 : 4856407 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9019 : 115987 : if (tree v = ctx->global->get_value (t))
9020 : 777137542 : r = v;
9021 : : }
9022 : 105780595 : else if (tree v = ctx->global->get_value (t))
9023 : : {
9024 : 39266130 : r = v;
9025 : 39266130 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9026 : 4719 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9027 : : non_constant_p, overflow_p,
9028 : : jump_target);
9029 : 39266130 : if (*jump_target)
9030 : : return NULL_TREE;
9031 : : }
9032 : 66514465 : else if (lval)
9033 : : /* Defer in case this is only used for its type. */;
9034 : 66514465 : else if (ctx->global->is_outside_lifetime (t))
9035 : : {
9036 : 18 : if (!ctx->quiet)
9037 : 6 : outside_lifetime_error (loc, t);
9038 : 18 : *non_constant_p = true;
9039 : 18 : break;
9040 : : }
9041 : 66514447 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9042 : 66514447 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9043 : : {
9044 : : /* If the class is empty, we aren't actually loading anything. */
9045 : 4197 : r = build_constructor (TREE_TYPE (t), NULL);
9046 : 4197 : TREE_CONSTANT (r) = true;
9047 : : }
9048 : 66510250 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9049 : : /* P2280 allows references to unknown... */;
9050 : 66438097 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9051 : : /* ...as well as the this pointer. */;
9052 : : else
9053 : : {
9054 : 66277455 : if (!ctx->quiet)
9055 : 194 : error ("%qE is not a constant expression", t);
9056 : 66277455 : *non_constant_p = true;
9057 : : }
9058 : : break;
9059 : :
9060 : 89161349 : case CALL_EXPR:
9061 : 89161349 : case AGGR_INIT_EXPR:
9062 : 89161349 : r = cxx_eval_call_expression (ctx, t, lval,
9063 : : non_constant_p, overflow_p, jump_target);
9064 : 89161349 : break;
9065 : :
9066 : 4671486 : case DECL_EXPR:
9067 : 4671486 : {
9068 : 4671486 : r = DECL_EXPR_DECL (t);
9069 : 4671486 : if (TREE_CODE (r) == USING_DECL)
9070 : : {
9071 : 2422 : r = void_node;
9072 : 2422 : break;
9073 : : }
9074 : :
9075 : 4669064 : if (VAR_P (r)
9076 : 4669064 : && (TREE_STATIC (r)
9077 : 4563417 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9078 : : /* Allow __FUNCTION__ etc. */
9079 : 105647 : && !DECL_ARTIFICIAL (r)
9080 : 4669083 : && !decl_constant_var_p (r))
9081 : : {
9082 : 7 : if (!ctx->quiet)
9083 : : {
9084 : 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9085 : 1 : error_at (loc, "control passes through definition of %qD "
9086 : : "with thread storage duration", r);
9087 : : else
9088 : 1 : error_at (loc, "control passes through definition of %qD "
9089 : : "with static storage duration", r);
9090 : : }
9091 : 7 : *non_constant_p = true;
9092 : 7 : break;
9093 : : }
9094 : :
9095 : : /* make_rtl_for_nonlocal_decl could have deferred emission of
9096 : : a local static var, but if it appears in a statement expression
9097 : : which is constant expression evaluated to e.g. just the address
9098 : : of the variable, its DECL_EXPR will never be seen during
9099 : : gimple lowering's record_vars_into as the statement expression
9100 : : will not be in the IL at all. */
9101 : 4669057 : if (VAR_P (r)
9102 : 4669057 : && TREE_STATIC (r)
9103 : 105640 : && !DECL_REALLY_EXTERN (r)
9104 : 105640 : && DECL_FUNCTION_SCOPE_P (r)
9105 : 105640 : && !var_in_maybe_constexpr_fn (r)
9106 : 4669060 : && decl_constant_var_p (r))
9107 : : {
9108 : 3 : varpool_node *node = varpool_node::get (r);
9109 : 3 : if (node == NULL || !node->definition)
9110 : 3 : rest_of_decl_compilation (r, 0, at_eof);
9111 : : }
9112 : :
9113 : 9223028 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9114 : 8916172 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9115 : : {
9116 : 422148 : new_ctx = *ctx;
9117 : 422148 : new_ctx.object = r;
9118 : 422148 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9119 : 422148 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9120 : 422148 : ctx->global->put_value (r, new_ctx.ctor);
9121 : 422148 : ctx = &new_ctx;
9122 : : }
9123 : :
9124 : 4669057 : if (tree init = DECL_INITIAL (r))
9125 : : {
9126 : 2400159 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9127 : : non_constant_p, overflow_p,
9128 : : jump_target);
9129 : 2400159 : if (*jump_target)
9130 : : return NULL_TREE;
9131 : : /* Don't share a CONSTRUCTOR that might be changed. */
9132 : 2400159 : init = unshare_constructor (init);
9133 : : /* Remember that a constant object's constructor has already
9134 : : run. */
9135 : 4800318 : if (CLASS_TYPE_P (TREE_TYPE (r))
9136 : 2505354 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9137 : 3439 : TREE_READONLY (init) = true;
9138 : 2400159 : ctx->global->put_value (r, init);
9139 : : }
9140 : 2268898 : else if (ctx == &new_ctx)
9141 : : /* We gave it a CONSTRUCTOR above. */;
9142 : : else
9143 : 1961334 : ctx->global->put_value (r, NULL_TREE);
9144 : : }
9145 : : break;
9146 : :
9147 : 9708277 : case TARGET_EXPR:
9148 : 9708277 : {
9149 : 9708277 : tree type = TREE_TYPE (t);
9150 : :
9151 : 9708277 : if (!literal_type_p (type))
9152 : : {
9153 : 2429 : if (!ctx->quiet)
9154 : : {
9155 : 0 : auto_diagnostic_group d;
9156 : 0 : error ("temporary of non-literal type %qT in a "
9157 : : "constant expression", type);
9158 : 0 : explain_non_literal_class (type);
9159 : 0 : }
9160 : 2429 : *non_constant_p = true;
9161 : 3490530 : break;
9162 : : }
9163 : 9705848 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9164 : : /* Avoid evaluating a TARGET_EXPR more than once. */
9165 : 9705848 : tree slot = TARGET_EXPR_SLOT (t);
9166 : 9705848 : if (tree v = ctx->global->get_value (slot))
9167 : : {
9168 : 750 : if (lval)
9169 : 5149126 : return slot;
9170 : : r = v;
9171 : : break;
9172 : : }
9173 : 9705098 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9174 : : {
9175 : : /* We're being expanded without an explicit target, so start
9176 : : initializing a new object; expansion with an explicit target
9177 : : strips the TARGET_EXPR before we get here. */
9178 : 7707451 : new_ctx = *ctx;
9179 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9180 : : any PLACEHOLDER_EXPR within the initializer that refers to the
9181 : : former object under construction. */
9182 : 7707451 : new_ctx.parent = ctx;
9183 : 7707451 : new_ctx.ctor = build_constructor (type, NULL);
9184 : 7707451 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9185 : 7707451 : new_ctx.object = slot;
9186 : 7707451 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9187 : 7707451 : ctx = &new_ctx;
9188 : : }
9189 : :
9190 : : /* If the initializer is complex, evaluate it to initialize slot. */
9191 : 9705098 : bool is_complex = target_expr_needs_replace (t);
9192 : 9705098 : if (is_complex)
9193 : : /* In case no initialization actually happens, clear out any
9194 : : void_node from a previous evaluation. */
9195 : 45 : ctx->global->put_value (slot, NULL_TREE);
9196 : :
9197 : : /* Pass vc_prvalue because this indicates
9198 : : initialization of a temporary. */
9199 : 9705098 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
9200 : : non_constant_p, overflow_p,
9201 : : jump_target);
9202 : 9705098 : if (*non_constant_p)
9203 : : break;
9204 : 6217534 : if (*jump_target)
9205 : : return NULL_TREE;
9206 : 6217530 : if (!is_complex)
9207 : : {
9208 : 6217491 : r = unshare_constructor (r);
9209 : : /* Adjust the type of the result to the type of the temporary. */
9210 : 6217491 : r = adjust_temp_type (type, r);
9211 : 6217491 : ctx->global->put_value (slot, r);
9212 : : }
9213 : 6217530 : if (TARGET_EXPR_CLEANUP (t)
9214 : 6217530 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9215 : : {
9216 : 259808 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9217 : : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9218 : : them. */
9219 : 259808 : if (CLEANUP_EH_ONLY (t))
9220 : 352 : ctx->global->cleanups->safe_push (NULL_TREE);
9221 : : }
9222 : 6217530 : if (ctx->save_exprs)
9223 : 2382009 : ctx->save_exprs->safe_push (slot);
9224 : 6217530 : if (lval)
9225 : : return slot;
9226 : 1068621 : if (is_complex)
9227 : 0 : r = ctx->global->get_value (slot);
9228 : : }
9229 : 1068621 : break;
9230 : :
9231 : 32527630 : case INIT_EXPR:
9232 : 32527630 : case MODIFY_EXPR:
9233 : 32527630 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9234 : 32527630 : r = cxx_eval_store_expression (ctx, t, lval,
9235 : : non_constant_p, overflow_p, jump_target);
9236 : 32527630 : break;
9237 : :
9238 : 0 : case SCOPE_REF:
9239 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9240 : : lval,
9241 : : non_constant_p, overflow_p,
9242 : : jump_target);
9243 : 0 : break;
9244 : :
9245 : 20709338 : case RETURN_EXPR:
9246 : 20709338 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9247 : 20679999 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9248 : : lval,
9249 : : non_constant_p, overflow_p,
9250 : : jump_target);
9251 : 20709336 : if (!throws (jump_target))
9252 : 20709278 : *jump_target = t;
9253 : : break;
9254 : 23723 : case BREAK_STMT:
9255 : 23723 : case CONTINUE_STMT:
9256 : 23723 : *jump_target = t;
9257 : 23723 : break;
9258 : :
9259 : 640421 : case SAVE_EXPR:
9260 : : /* Avoid evaluating a SAVE_EXPR more than once. */
9261 : 640421 : if (tree v = ctx->global->get_value (t))
9262 : : r = v;
9263 : : else
9264 : : {
9265 : 633908 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9266 : : vc_prvalue, non_constant_p,
9267 : : overflow_p, jump_target);
9268 : 633908 : if (*non_constant_p || *jump_target)
9269 : : break;
9270 : 8344 : ctx->global->put_value (t, r);
9271 : 8344 : if (ctx->save_exprs)
9272 : 6231 : ctx->save_exprs->safe_push (t);
9273 : : }
9274 : : break;
9275 : :
9276 : 19744311 : case NON_LVALUE_EXPR:
9277 : 19744311 : case EXPR_STMT:
9278 : 19744311 : case EH_SPEC_BLOCK:
9279 : 19744311 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9280 : : lval,
9281 : : non_constant_p, overflow_p,
9282 : : jump_target);
9283 : 19744311 : break;
9284 : :
9285 : 447 : case TRY_BLOCK:
9286 : 447 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9287 : : non_constant_p, overflow_p,
9288 : : jump_target);
9289 : 447 : if (!*non_constant_p && throws (jump_target))
9290 : 196 : if (tree h = TRY_HANDLERS (t))
9291 : : {
9292 : 196 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9293 : 196 : if (TREE_CODE (h) == STATEMENT_LIST)
9294 : : {
9295 : 334 : for (tree stmt : tsi_range (h))
9296 : 317 : if (TREE_CODE (stmt) == HANDLER
9297 : 317 : && handler_match_for_exception_type (stmt, type))
9298 : : {
9299 : : h = stmt;
9300 : : break;
9301 : : }
9302 : 95 : if (TREE_CODE (h) == STATEMENT_LIST)
9303 : : h = NULL_TREE;
9304 : : }
9305 : 101 : else if (TREE_CODE (h) != HANDLER
9306 : 101 : || !handler_match_for_exception_type (h, type))
9307 : : h = NULL_TREE;
9308 : : if (h)
9309 : : {
9310 : 179 : gcc_assert (VAR_P (*jump_target));
9311 : 179 : ctx->global->caught_exceptions.safe_push (*jump_target);
9312 : 179 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9313 : 179 : *jump_target = NULL_TREE;
9314 : 179 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9315 : : vc_discard, non_constant_p,
9316 : : overflow_p, jump_target);
9317 : : }
9318 : : }
9319 : : break;
9320 : :
9321 : 26802155 : case CLEANUP_POINT_EXPR:
9322 : 26802155 : {
9323 : 26802155 : auto_vec<tree, 2> cleanups;
9324 : 26802155 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9325 : 26802155 : ctx->global->cleanups = &cleanups;
9326 : :
9327 : 26802155 : auto_vec<tree, 10> save_exprs;
9328 : 26802155 : constexpr_ctx new_ctx = *ctx;
9329 : 26802155 : new_ctx.save_exprs = &save_exprs;
9330 : :
9331 : 26802155 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9332 : : lval,
9333 : : non_constant_p, overflow_p,
9334 : : jump_target);
9335 : :
9336 : 26802154 : ctx->global->cleanups = prev_cleanups;
9337 : 26802154 : unsigned int i;
9338 : 26802154 : tree cleanup, jmp_target = NULL_TREE;
9339 : 26802154 : bool eh = throws (jump_target);
9340 : : /* Evaluate the cleanups. */
9341 : 53843133 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9342 : 238825 : if (cleanup == NULL_TREE)
9343 : : {
9344 : : /* NULL_TREE cleanup is a marker that before it is
9345 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9346 : : if the body didn't throw. */
9347 : 329 : if (!eh)
9348 : 328 : --i;
9349 : : }
9350 : : else
9351 : 238496 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9352 : : non_constant_p, overflow_p,
9353 : : &jmp_target);
9354 : :
9355 : : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9356 : : full-expression. */
9357 : 82794702 : for (tree save_expr : save_exprs)
9358 : 2388240 : destroy_value_checked (ctx, save_expr, non_constant_p);
9359 : 26802154 : if (throws (&jmp_target))
9360 : 0 : *jump_target = jmp_target;
9361 : 26802154 : }
9362 : 26802154 : break;
9363 : :
9364 : 19415006 : case MUST_NOT_THROW_EXPR:
9365 : 19415006 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9366 : : lval,
9367 : : non_constant_p, overflow_p,
9368 : : jump_target);
9369 : 19415005 : if (throws (jump_target))
9370 : : {
9371 : : /* [except.handle]/7 - If the search for a handler exits the
9372 : : function body of a function with a non-throwing exception
9373 : : specification, the function std::terminate is invoked. */
9374 : 27 : if (!ctx->quiet)
9375 : : {
9376 : 9 : auto_diagnostic_group d;
9377 : 9 : diagnose_std_terminate (loc, ctx, *jump_target);
9378 : 9 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9379 : 7 : && ctx->call
9380 : 16 : && ctx->call->fundef)
9381 : 7 : inform (loc, "uncaught exception exited from %<noexcept%> "
9382 : : "function %qD",
9383 : : ctx->call->fundef->decl);
9384 : 2 : else if (MUST_NOT_THROW_THROW_P (t))
9385 : 1 : inform (loc, "destructor exited with an exception after "
9386 : : "initializing the exception object");
9387 : 1 : else if (MUST_NOT_THROW_CATCH_P (t))
9388 : 1 : inform (loc, "constructor exited with another exception while "
9389 : : "entering handler");
9390 : 9 : }
9391 : 27 : *non_constant_p = true;
9392 : 27 : *jump_target = NULL_TREE;
9393 : 27 : r = NULL_TREE;
9394 : : }
9395 : : break;
9396 : :
9397 : 0 : case TRY_CATCH_EXPR:
9398 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9399 : : {
9400 : 0 : r = void_node;
9401 : 0 : break;
9402 : : }
9403 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9404 : : non_constant_p, overflow_p,
9405 : : jump_target);
9406 : 0 : if (!*non_constant_p && throws (jump_target))
9407 : : {
9408 : 0 : tree jmp_target = NULL_TREE;
9409 : 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9410 : : non_constant_p, overflow_p,
9411 : : &jmp_target);
9412 : 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9413 : : jmp_target);
9414 : : }
9415 : : break;
9416 : :
9417 : 471 : case TRY_FINALLY_EXPR:
9418 : 471 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9419 : : non_constant_p, overflow_p,
9420 : : jump_target);
9421 : 471 : if (!*non_constant_p)
9422 : : {
9423 : 456 : tree jmp_target = NULL_TREE;
9424 : : /* Also evaluate the cleanup. */
9425 : 456 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9426 : 456 : && throws (jump_target))
9427 : 0 : cxx_eval_constant_expression (ctx,
9428 : 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9429 : : 1), vc_discard,
9430 : : non_constant_p, overflow_p,
9431 : : &jmp_target);
9432 : : else
9433 : 456 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9434 : : non_constant_p, overflow_p,
9435 : : &jmp_target);
9436 : 456 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9437 : : jmp_target);
9438 : : }
9439 : : break;
9440 : :
9441 : 18 : case EH_ELSE_EXPR:
9442 : : /* Evaluate any cleanup that applies to non-EH exits. */
9443 : 18 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9444 : : non_constant_p, overflow_p,
9445 : : jump_target);
9446 : :
9447 : : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9448 : 18 : break;
9449 : :
9450 : 708043 : case CLEANUP_STMT:
9451 : 708043 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9452 : : non_constant_p, overflow_p,
9453 : : jump_target);
9454 : 708042 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9455 : : {
9456 : 77529 : iloc_sentinel ils (loc);
9457 : 77529 : tree jmp_target = NULL_TREE;
9458 : : /* Also evaluate the cleanup. */
9459 : 77529 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9460 : : non_constant_p, overflow_p,
9461 : : &jmp_target);
9462 : 77529 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9463 : : jmp_target);
9464 : 77529 : }
9465 : : break;
9466 : :
9467 : : /* These differ from cxx_eval_unary_expression in that this doesn't
9468 : : check for a constant operand or result; an address can be
9469 : : constant without its operand being, and vice versa. */
9470 : 35565612 : case MEM_REF:
9471 : 35565612 : case INDIRECT_REF:
9472 : 35565612 : r = cxx_eval_indirect_ref (ctx, t, lval,
9473 : : non_constant_p, overflow_p,
9474 : : jump_target);
9475 : 35565612 : break;
9476 : :
9477 : 40994067 : case ADDR_EXPR:
9478 : 40994067 : {
9479 : 40994067 : tree oldop = TREE_OPERAND (t, 0);
9480 : 40994067 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9481 : : non_constant_p, overflow_p,
9482 : : jump_target);
9483 : 40994067 : if (*jump_target)
9484 : : return NULL_TREE;
9485 : : /* Don't VERIFY_CONSTANT here. */
9486 : 40994064 : if (*non_constant_p)
9487 : : return t;
9488 : 33131980 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9489 : : /* This function does more aggressive folding than fold itself. */
9490 : 33131980 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9491 : 33131980 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9492 : : {
9493 : 24139511 : ggc_free (r);
9494 : 24139511 : return t;
9495 : : }
9496 : : break;
9497 : : }
9498 : :
9499 : 501729 : case REALPART_EXPR:
9500 : 501729 : case IMAGPART_EXPR:
9501 : 501729 : if (lval)
9502 : : {
9503 : 614 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9504 : : non_constant_p, overflow_p,
9505 : : jump_target);
9506 : 614 : if (*jump_target)
9507 : : return NULL_TREE;
9508 : 614 : if (r == error_mark_node)
9509 : : ;
9510 : 614 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9511 : : r = t;
9512 : : else
9513 : 568 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9514 : : break;
9515 : : }
9516 : : /* FALLTHRU */
9517 : 20939000 : case CONJ_EXPR:
9518 : 20939000 : case FIX_TRUNC_EXPR:
9519 : 20939000 : case FLOAT_EXPR:
9520 : 20939000 : case NEGATE_EXPR:
9521 : 20939000 : case ABS_EXPR:
9522 : 20939000 : case ABSU_EXPR:
9523 : 20939000 : case BIT_NOT_EXPR:
9524 : 20939000 : case TRUTH_NOT_EXPR:
9525 : 20939000 : case FIXED_CONVERT_EXPR:
9526 : 20939000 : case VEC_DUPLICATE_EXPR:
9527 : 20939000 : r = cxx_eval_unary_expression (ctx, t, lval,
9528 : : non_constant_p, overflow_p,
9529 : : jump_target);
9530 : 20939000 : break;
9531 : :
9532 : 7319331 : case SIZEOF_EXPR:
9533 : 7319331 : r = fold_sizeof_expr (t);
9534 : : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9535 : : which could lead to an infinite recursion. */
9536 : 7319331 : if (TREE_CODE (r) != SIZEOF_EXPR)
9537 : 7319331 : r = cxx_eval_constant_expression (ctx, r, lval,
9538 : : non_constant_p, overflow_p,
9539 : : jump_target);
9540 : : else
9541 : : {
9542 : 0 : *non_constant_p = true;
9543 : 0 : gcc_assert (ctx->quiet);
9544 : : }
9545 : :
9546 : : break;
9547 : :
9548 : 3816098 : case COMPOUND_EXPR:
9549 : 3816098 : {
9550 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9551 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9552 : : introduced by build_call_a. */
9553 : 3816098 : tree op0 = TREE_OPERAND (t, 0);
9554 : 3816098 : tree op1 = TREE_OPERAND (t, 1);
9555 : 3816098 : STRIP_NOPS (op1);
9556 : 954484 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9557 : 3989778 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9558 : 3176229 : r = cxx_eval_constant_expression (ctx, op0,
9559 : : lval, non_constant_p, overflow_p,
9560 : : jump_target);
9561 : : else
9562 : : {
9563 : : /* Check that the LHS is constant and then discard it. */
9564 : 639869 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9565 : : non_constant_p, overflow_p,
9566 : : jump_target);
9567 : 639869 : if (*jump_target)
9568 : : return NULL_TREE;
9569 : 639610 : if (*non_constant_p)
9570 : : return t;
9571 : 553290 : op1 = TREE_OPERAND (t, 1);
9572 : 553290 : r = cxx_eval_constant_expression (ctx, op1,
9573 : : lval, non_constant_p, overflow_p,
9574 : : jump_target);
9575 : : }
9576 : : }
9577 : : break;
9578 : :
9579 : 50311023 : case POINTER_PLUS_EXPR:
9580 : 50311023 : case POINTER_DIFF_EXPR:
9581 : 50311023 : case PLUS_EXPR:
9582 : 50311023 : case MINUS_EXPR:
9583 : 50311023 : case MULT_EXPR:
9584 : 50311023 : case TRUNC_DIV_EXPR:
9585 : 50311023 : case CEIL_DIV_EXPR:
9586 : 50311023 : case FLOOR_DIV_EXPR:
9587 : 50311023 : case ROUND_DIV_EXPR:
9588 : 50311023 : case TRUNC_MOD_EXPR:
9589 : 50311023 : case CEIL_MOD_EXPR:
9590 : 50311023 : case ROUND_MOD_EXPR:
9591 : 50311023 : case RDIV_EXPR:
9592 : 50311023 : case EXACT_DIV_EXPR:
9593 : 50311023 : case MIN_EXPR:
9594 : 50311023 : case MAX_EXPR:
9595 : 50311023 : case LSHIFT_EXPR:
9596 : 50311023 : case RSHIFT_EXPR:
9597 : 50311023 : case LROTATE_EXPR:
9598 : 50311023 : case RROTATE_EXPR:
9599 : 50311023 : case BIT_IOR_EXPR:
9600 : 50311023 : case BIT_XOR_EXPR:
9601 : 50311023 : case BIT_AND_EXPR:
9602 : 50311023 : case TRUTH_XOR_EXPR:
9603 : 50311023 : case LT_EXPR:
9604 : 50311023 : case LE_EXPR:
9605 : 50311023 : case GT_EXPR:
9606 : 50311023 : case GE_EXPR:
9607 : 50311023 : case EQ_EXPR:
9608 : 50311023 : case NE_EXPR:
9609 : 50311023 : case SPACESHIP_EXPR:
9610 : 50311023 : case UNORDERED_EXPR:
9611 : 50311023 : case ORDERED_EXPR:
9612 : 50311023 : case UNLT_EXPR:
9613 : 50311023 : case UNLE_EXPR:
9614 : 50311023 : case UNGT_EXPR:
9615 : 50311023 : case UNGE_EXPR:
9616 : 50311023 : case UNEQ_EXPR:
9617 : 50311023 : case LTGT_EXPR:
9618 : 50311023 : case RANGE_EXPR:
9619 : 50311023 : case COMPLEX_EXPR:
9620 : 50311023 : r = cxx_eval_binary_expression (ctx, t, lval,
9621 : : non_constant_p, overflow_p,
9622 : : jump_target);
9623 : 50311023 : break;
9624 : :
9625 : : /* fold can introduce non-IF versions of these; still treat them as
9626 : : short-circuiting. */
9627 : 5997593 : case TRUTH_AND_EXPR:
9628 : 5997593 : case TRUTH_ANDIF_EXPR:
9629 : 5997593 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
9630 : : boolean_true_node,
9631 : : non_constant_p, overflow_p,
9632 : : jump_target);
9633 : 5997593 : break;
9634 : :
9635 : 1322441 : case TRUTH_OR_EXPR:
9636 : 1322441 : case TRUTH_ORIF_EXPR:
9637 : 1322441 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
9638 : : boolean_false_node,
9639 : : non_constant_p, overflow_p,
9640 : : jump_target);
9641 : 1322441 : break;
9642 : :
9643 : 2989813 : case ARRAY_REF:
9644 : 2989813 : r = cxx_eval_array_reference (ctx, t, lval,
9645 : : non_constant_p, overflow_p,
9646 : : jump_target);
9647 : 2989813 : break;
9648 : :
9649 : 34878686 : case COMPONENT_REF:
9650 : 34878686 : if (is_overloaded_fn (t))
9651 : : {
9652 : : /* We can only get here in checking mode via
9653 : : build_non_dependent_expr, because any expression that
9654 : : calls or takes the address of the function will have
9655 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9656 : 6 : gcc_checking_assert (ctx->quiet || errorcount);
9657 : 6 : *non_constant_p = true;
9658 : 6 : return t;
9659 : : }
9660 : 34878680 : r = cxx_eval_component_reference (ctx, t, lval,
9661 : : non_constant_p, overflow_p,
9662 : : jump_target);
9663 : 34878680 : break;
9664 : :
9665 : 2885 : case BIT_FIELD_REF:
9666 : 2885 : r = cxx_eval_bit_field_ref (ctx, t, lval,
9667 : : non_constant_p, overflow_p,
9668 : : jump_target);
9669 : 2885 : break;
9670 : :
9671 : 9517628 : case COND_EXPR:
9672 : 9517628 : case IF_STMT:
9673 : 9517628 : if (*jump_target)
9674 : : {
9675 : 152803 : tree orig_jump = *jump_target;
9676 : 152803 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
9677 : 305606 : ? TREE_OPERAND (t, 1) : void_node);
9678 : : /* When jumping to a label, the label might be either in the
9679 : : then or else blocks, so process then block first in skipping
9680 : : mode first, and if we are still in the skipping mode at its end,
9681 : : process the else block too. */
9682 : 152803 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9683 : : overflow_p, jump_target);
9684 : : /* It's possible that we found the label in the then block. But
9685 : : it could have been followed by another jumping statement, e.g.
9686 : : say we're looking for case 1:
9687 : : if (cond)
9688 : : {
9689 : : // skipped statements
9690 : : case 1:; // clears up *jump_target
9691 : : return 1; // and sets it to a RETURN_EXPR
9692 : : }
9693 : : else { ... }
9694 : : in which case we need not go looking to the else block.
9695 : : (goto is not allowed in a constexpr function.) */
9696 : 152803 : if (*jump_target == orig_jump)
9697 : : {
9698 : 152851 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
9699 : 152851 : ? TREE_OPERAND (t, 2) : void_node);
9700 : 152769 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9701 : : overflow_p, jump_target);
9702 : : }
9703 : : break;
9704 : : }
9705 : 9364825 : r = cxx_eval_conditional_expression (ctx, t, lval,
9706 : : non_constant_p, overflow_p,
9707 : : jump_target);
9708 : 9364825 : break;
9709 : 866 : case VEC_COND_EXPR:
9710 : 866 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
9711 : : overflow_p, jump_target);
9712 : 866 : break;
9713 : :
9714 : 13030006 : case CONSTRUCTOR:
9715 : 13030006 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
9716 : : {
9717 : : /* Don't re-process a constant CONSTRUCTOR. */
9718 : 11792175 : verify_constructor_flags (t);
9719 : 11792175 : if (TREE_CONSTANT (t))
9720 : : return t;
9721 : : }
9722 : 1237831 : if (TREE_CLOBBER_P (t))
9723 : : {
9724 : : /* Assignment from a clobber is handled in cxx_eval_store_expression;
9725 : : a clobber by itself isn't a constant-expression. */
9726 : 0 : gcc_assert (ctx->quiet);
9727 : 0 : *non_constant_p = true;
9728 : 0 : break;
9729 : : }
9730 : 1237831 : r = cxx_eval_bare_aggregate (ctx, t, lval,
9731 : : non_constant_p, overflow_p, jump_target);
9732 : 1237831 : break;
9733 : :
9734 : 512 : case VEC_INIT_EXPR:
9735 : : /* We can get this in a defaulted constructor for a class with a
9736 : : non-static data member of array type. Either the initializer will
9737 : : be NULL, meaning default-initialization, or it will be an lvalue
9738 : : or xvalue of the same type, meaning direct-initialization from the
9739 : : corresponding member. */
9740 : 512 : r = cxx_eval_vec_init (ctx, t, lval,
9741 : : non_constant_p, overflow_p, jump_target);
9742 : 512 : break;
9743 : :
9744 : 16 : case VEC_PERM_EXPR:
9745 : 16 : r = cxx_eval_trinary_expression (ctx, t, lval,
9746 : : non_constant_p, overflow_p,
9747 : : jump_target);
9748 : 16 : break;
9749 : :
9750 : 4 : case PAREN_EXPR:
9751 : 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
9752 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
9753 : : constant expressions since it's unaffected by -fassociative-math. */
9754 : 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9755 : : non_constant_p, overflow_p,
9756 : : jump_target);
9757 : 4 : break;
9758 : :
9759 : 181730270 : case NOP_EXPR:
9760 : 181730270 : if (REINTERPRET_CAST_P (t))
9761 : : {
9762 : 73 : if (!ctx->quiet)
9763 : 6 : error_at (loc,
9764 : : "%<reinterpret_cast%> is not a constant expression");
9765 : 73 : *non_constant_p = true;
9766 : 73 : return t;
9767 : : }
9768 : : /* FALLTHROUGH. */
9769 : 214732650 : case CONVERT_EXPR:
9770 : 214732650 : case VIEW_CONVERT_EXPR:
9771 : 214732650 : case UNARY_PLUS_EXPR:
9772 : 214732650 : {
9773 : 214732650 : tree oldop = TREE_OPERAND (t, 0);
9774 : :
9775 : 214732650 : tree op = cxx_eval_constant_expression (ctx, oldop,
9776 : 214732650 : VOID_TYPE_P (TREE_TYPE (t))
9777 : : ? vc_discard
9778 : : : tcode == VIEW_CONVERT_EXPR
9779 : 203198045 : ? lval : vc_prvalue,
9780 : : non_constant_p, overflow_p,
9781 : : jump_target);
9782 : 214729952 : if (*jump_target)
9783 : : return NULL_TREE;
9784 : 214729614 : if (*non_constant_p)
9785 : : return t;
9786 : 162187981 : tree type = TREE_TYPE (t);
9787 : :
9788 : 162187981 : if (VOID_TYPE_P (type))
9789 : 10940619 : return void_node;
9790 : :
9791 : 151247362 : if (TREE_CODE (t) == CONVERT_EXPR
9792 : 13086393 : && ARITHMETIC_TYPE_P (type)
9793 : 2405102 : && INDIRECT_TYPE_P (TREE_TYPE (op))
9794 : 151270472 : && ctx->strict)
9795 : : {
9796 : 23020 : if (!ctx->quiet)
9797 : 54 : error_at (loc,
9798 : : "conversion from pointer type %qT to arithmetic type "
9799 : 54 : "%qT in a constant expression", TREE_TYPE (op), type);
9800 : 23020 : *non_constant_p = true;
9801 : 23020 : return t;
9802 : : }
9803 : :
9804 : : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
9805 : : type cannot be part of a core constant expression as a resolution to
9806 : : DR 1312. */
9807 : 43186788 : if (TYPE_PTROB_P (type)
9808 : 41868236 : && TYPE_PTR_P (TREE_TYPE (op))
9809 : 31137006 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
9810 : : /* Inside a call to std::construct_at,
9811 : : std::allocator<T>::{,de}allocate, or
9812 : : std::source_location::current, we permit casting from void*
9813 : : because that is compiler-generated code. */
9814 : 156994 : && !is_std_construct_at (ctx->call)
9815 : 24323 : && !is_std_allocator_allocate (ctx->call)
9816 : 151244552 : && !is_std_source_location_current (ctx->call))
9817 : : {
9818 : : /* Likewise, don't error when casting from void* when OP is
9819 : : &heap uninit and similar. */
9820 : 20051 : tree sop = tree_strip_nop_conversions (op);
9821 : 20051 : tree decl = NULL_TREE;
9822 : 20051 : if (TREE_CODE (sop) == ADDR_EXPR)
9823 : 15000 : decl = TREE_OPERAND (sop, 0);
9824 : 15000 : if (decl
9825 : 15000 : && VAR_P (decl)
9826 : 14662 : && DECL_ARTIFICIAL (decl)
9827 : 29623 : && (DECL_NAME (decl) == heap_identifier
9828 : 6903 : || DECL_NAME (decl) == heap_uninit_identifier
9829 : 2188 : || DECL_NAME (decl) == heap_vec_identifier
9830 : 1174 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
9831 : : /* OK */;
9832 : : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
9833 : : cv void" to a pointer-to-object type T unless P is a null
9834 : : pointer value or points to an object whose type is similar to
9835 : : T. */
9836 : 5444 : else if (cxx_dialect > cxx23)
9837 : : {
9838 : 5328 : if (integer_zerop (sop))
9839 : 31 : return build_int_cst (type, 0);
9840 : 5297 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
9841 : : NULL, jump_target);
9842 : 5297 : if (*jump_target)
9843 : : return NULL_TREE;
9844 : 5297 : if (r)
9845 : : {
9846 : 5262 : r = build1 (ADDR_EXPR, type, r);
9847 : 5262 : break;
9848 : : }
9849 : 35 : if (!ctx->quiet)
9850 : : {
9851 : 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
9852 : 3 : auto_diagnostic_group d;
9853 : 3 : error_at (loc, "cast from %qT is not allowed in a "
9854 : : "constant expression because "
9855 : : "pointed-to type %qT is not similar to %qT",
9856 : 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
9857 : 3 : TREE_TYPE (type));
9858 : 3 : tree obj = build_fold_indirect_ref (sop);
9859 : 3 : if (TREE_CODE (obj) == COMPONENT_REF)
9860 : 2 : obj = TREE_OPERAND (obj, 1);
9861 : 3 : if (DECL_P (obj))
9862 : 3 : inform (DECL_SOURCE_LOCATION (obj),
9863 : : "pointed-to object declared here");
9864 : 3 : }
9865 : 35 : *non_constant_p = true;
9866 : 35 : return t;
9867 : : }
9868 : : else
9869 : : {
9870 : 116 : if (!ctx->quiet)
9871 : 26 : error_at (loc, "cast from %qT is not allowed in a "
9872 : : "constant expression before C++26",
9873 : 26 : TREE_TYPE (op));
9874 : 116 : *non_constant_p = true;
9875 : 116 : return t;
9876 : : }
9877 : : }
9878 : :
9879 : 151218898 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
9880 : : {
9881 : 1712 : op = cplus_expand_constant (op);
9882 : 1712 : if (TREE_CODE (op) == PTRMEM_CST)
9883 : : {
9884 : 21 : if (!ctx->quiet)
9885 : 3 : error_at (loc, "%qE is not a constant expression when the "
9886 : : "class %qT is still incomplete", op,
9887 : 3 : PTRMEM_CST_CLASS (op));
9888 : 21 : *non_constant_p = true;
9889 : 21 : return t;
9890 : : }
9891 : : }
9892 : :
9893 : 151218877 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
9894 : : {
9895 : 803 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
9896 : 803 : && !can_convert_qual (type, op))
9897 : 6 : op = cplus_expand_constant (op);
9898 : 803 : return cp_fold_convert (type, op);
9899 : : }
9900 : :
9901 : 151218074 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
9902 : : {
9903 : 347305 : if (integer_zerop (op))
9904 : : {
9905 : 288217 : if (TYPE_REF_P (type))
9906 : : {
9907 : 32 : if (!ctx->quiet)
9908 : 4 : error_at (loc, "dereferencing a null pointer");
9909 : 32 : *non_constant_p = true;
9910 : 32 : return t;
9911 : : }
9912 : : }
9913 : 59088 : else if (TYPE_PTR_P (type)
9914 : 59088 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
9915 : : /* INTEGER_CST with pointer-to-method type is only used
9916 : : for a virtual method in a pointer to member function.
9917 : : Don't reject those. */
9918 : : ;
9919 : : else
9920 : : {
9921 : : /* This detects for example:
9922 : : reinterpret_cast<void*>(sizeof 0)
9923 : : */
9924 : 59085 : if (!ctx->quiet)
9925 : 21 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
9926 : : "a constant expression",
9927 : : type, op);
9928 : 59085 : *non_constant_p = true;
9929 : 59085 : return t;
9930 : : }
9931 : : }
9932 : :
9933 : 151158957 : if (INDIRECT_TYPE_P (type)
9934 : 67046413 : && TREE_CODE (op) == NOP_EXPR
9935 : 33501094 : && TREE_TYPE (op) == ptr_type_node
9936 : 78740 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
9937 : 77186 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
9938 : 151203551 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9939 : 44594 : 0)) == heap_uninit_identifier
9940 : 35766 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9941 : 35766 : 0)) == heap_vec_uninit_identifier))
9942 : : {
9943 : 9986 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
9944 : 9986 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
9945 : 9986 : tree elt_type = TREE_TYPE (type);
9946 : 9986 : tree cookie_size = NULL_TREE;
9947 : 9986 : tree arg_size = NULL_TREE;
9948 : 9986 : if (TREE_CODE (elt_type) == RECORD_TYPE
9949 : 9986 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
9950 : : {
9951 : 9 : tree fld1 = TYPE_FIELDS (elt_type);
9952 : 9 : tree fld2 = DECL_CHAIN (fld1);
9953 : 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
9954 : 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
9955 : : }
9956 : 9986 : DECL_NAME (var)
9957 : 9986 : = (DECL_NAME (var) == heap_uninit_identifier
9958 : 9986 : ? heap_identifier : heap_vec_identifier);
9959 : : /* For zero sized elt_type, try to recover how many outer_nelts
9960 : : it should have. */
9961 : 9986 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
9962 : 9977 : : integer_zerop (var_size))
9963 : 40 : && !int_size_in_bytes (elt_type)
9964 : 9 : && TREE_CODE (oldop) == CALL_EXPR
9965 : 10004 : && call_expr_nargs (oldop) >= 1)
9966 : 9 : if (tree fun = get_function_named_in_call (oldop))
9967 : 9 : if (cxx_replaceable_global_alloc_fn (fun)
9968 : 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
9969 : 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
9970 : 9986 : tree new_type
9971 : 9986 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
9972 : : var_size, arg_size,
9973 : : non_constant_p, overflow_p,
9974 : : jump_target);
9975 : 9986 : if (*jump_target)
9976 : : return NULL_TREE;
9977 : 9986 : TREE_TYPE (var) = new_type;
9978 : 9986 : TREE_TYPE (TREE_OPERAND (op, 0))
9979 : 19972 : = build_pointer_type (TREE_TYPE (var));
9980 : : }
9981 : :
9982 : 151158957 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
9983 : : /* We didn't fold at the top so we could check for ptr-int
9984 : : conversion. */
9985 : 12182075 : return fold (t);
9986 : :
9987 : 138976882 : tree sop;
9988 : :
9989 : : /* Handle an array's bounds having been deduced after we built
9990 : : the wrapping expression. */
9991 : 138976882 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
9992 : : r = op;
9993 : 39833656 : else if (sop = tree_strip_nop_conversions (op),
9994 : 54970585 : sop != op && (same_type_ignoring_tlq_and_bounds_p
9995 : 15136929 : (type, TREE_TYPE (sop))))
9996 : : r = sop;
9997 : 32659606 : else if (tcode == UNARY_PLUS_EXPR)
9998 : 0 : r = fold_convert (TREE_TYPE (t), op);
9999 : : else
10000 : 32659606 : r = fold_build1 (tcode, type, op);
10001 : :
10002 : : /* Conversion of an out-of-range value has implementation-defined
10003 : : behavior; the language considers it different from arithmetic
10004 : : overflow, which is undefined. */
10005 : 138976882 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10006 : 12078 : TREE_OVERFLOW (r) = false;
10007 : : }
10008 : : break;
10009 : :
10010 : 8310 : case EXCESS_PRECISION_EXPR:
10011 : 8310 : {
10012 : 8310 : tree oldop = TREE_OPERAND (t, 0);
10013 : :
10014 : 8310 : tree op = cxx_eval_constant_expression (ctx, oldop,
10015 : : lval,
10016 : : non_constant_p, overflow_p,
10017 : : jump_target);
10018 : 8310 : if (*jump_target)
10019 : : return NULL_TREE;
10020 : 8310 : if (*non_constant_p)
10021 : : return t;
10022 : 8306 : r = fold_convert (TREE_TYPE (t), op);
10023 : 8306 : break;
10024 : : }
10025 : :
10026 : 39417 : case EMPTY_CLASS_EXPR:
10027 : : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10028 : : it to an appropriate CONSTRUCTOR. */
10029 : 39417 : return build_constructor (TREE_TYPE (t), NULL);
10030 : :
10031 : 17327131 : case STATEMENT_LIST:
10032 : 17327131 : new_ctx = *ctx;
10033 : 17327131 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10034 : 17327131 : return cxx_eval_statement_list (&new_ctx, t,
10035 : 17327129 : non_constant_p, overflow_p, jump_target);
10036 : :
10037 : 9164074 : case BIND_EXPR:
10038 : : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10039 : : map, so that when checking whether they're already destroyed later we
10040 : : don't get confused by remnants of previous calls. */
10041 : 13104250 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10042 : 3940176 : ctx->global->clear_value (decl);
10043 : 9164074 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10044 : : lval,
10045 : : non_constant_p, overflow_p,
10046 : : jump_target);
10047 : 13104248 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10048 : 3940175 : destroy_value_checked (ctx, decl, non_constant_p);
10049 : : break;
10050 : :
10051 : 1954546 : case PREINCREMENT_EXPR:
10052 : 1954546 : case POSTINCREMENT_EXPR:
10053 : 1954546 : case PREDECREMENT_EXPR:
10054 : 1954546 : case POSTDECREMENT_EXPR:
10055 : 1954546 : return cxx_eval_increment_expression (ctx, t,
10056 : : lval, non_constant_p, overflow_p,
10057 : 1954546 : jump_target);
10058 : :
10059 : 697 : case THROW_EXPR:
10060 : 697 : if (cxx_dialect >= cxx26)
10061 : 369 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10062 : : non_constant_p, overflow_p,
10063 : 369 : jump_target);
10064 : : /* FALLTHROUGH */
10065 : 336 : case LAMBDA_EXPR:
10066 : 336 : case NEW_EXPR:
10067 : 336 : case VEC_NEW_EXPR:
10068 : 336 : case DELETE_EXPR:
10069 : 336 : case VEC_DELETE_EXPR:
10070 : 336 : case MODOP_EXPR:
10071 : : /* GCC internal stuff. */
10072 : 336 : case VA_ARG_EXPR:
10073 : 336 : case BASELINK:
10074 : 336 : case OFFSET_REF:
10075 : 336 : if (!ctx->quiet)
10076 : 86 : error_at (loc, "expression %qE is not a constant expression", t);
10077 : 336 : *non_constant_p = true;
10078 : 336 : break;
10079 : :
10080 : 117892 : case OBJ_TYPE_REF:
10081 : : /* Virtual function lookup. We don't need to do anything fancy. */
10082 : 117892 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10083 : : lval, non_constant_p, overflow_p,
10084 : 117892 : jump_target);
10085 : :
10086 : 16121 : case PLACEHOLDER_EXPR:
10087 : : /* Use of the value or address of the current object. */
10088 : 16121 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10089 : : {
10090 : 15889 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10091 : : return ctor;
10092 : : else
10093 : 15589 : return cxx_eval_constant_expression (ctx, ctor, lval,
10094 : : non_constant_p, overflow_p,
10095 : 15589 : jump_target);
10096 : : }
10097 : : /* A placeholder without a referent. We can get here when
10098 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
10099 : : just say it's non-constant for now. */
10100 : 232 : gcc_assert (ctx->quiet);
10101 : 232 : *non_constant_p = true;
10102 : 232 : break;
10103 : :
10104 : 239 : case EXIT_EXPR:
10105 : 239 : {
10106 : 239 : tree cond = TREE_OPERAND (t, 0);
10107 : 239 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10108 : : non_constant_p, overflow_p,
10109 : : jump_target);
10110 : 239 : if (*jump_target)
10111 : : return NULL_TREE;
10112 : 239 : VERIFY_CONSTANT (cond);
10113 : 239 : if (integer_nonzerop (cond))
10114 : 67 : *jump_target = t;
10115 : : }
10116 : : break;
10117 : :
10118 : 21 : case GOTO_EXPR:
10119 : 21 : if (breaks (&TREE_OPERAND (t, 0))
10120 : 21 : || continues (&TREE_OPERAND (t, 0)))
10121 : 9 : *jump_target = TREE_OPERAND (t, 0);
10122 : : else
10123 : : {
10124 : 12 : if (!ctx->quiet)
10125 : 1 : error_at (loc, "%<goto%> is not a constant expression");
10126 : 12 : *non_constant_p = true;
10127 : : }
10128 : : break;
10129 : :
10130 : 984753 : case LOOP_EXPR:
10131 : 984753 : case DO_STMT:
10132 : 984753 : case WHILE_STMT:
10133 : 984753 : case FOR_STMT:
10134 : 984753 : cxx_eval_loop_expr (ctx, t,
10135 : : non_constant_p, overflow_p, jump_target);
10136 : 984753 : break;
10137 : :
10138 : 37275 : case SWITCH_EXPR:
10139 : 37275 : case SWITCH_STMT:
10140 : 37275 : cxx_eval_switch_expr (ctx, t,
10141 : : non_constant_p, overflow_p, jump_target);
10142 : 37275 : break;
10143 : :
10144 : 118 : case REQUIRES_EXPR:
10145 : : /* It's possible to get a requires-expression in a constant
10146 : : expression. For example:
10147 : :
10148 : : template<typename T> concept bool C() {
10149 : : return requires (T t) { t; };
10150 : : }
10151 : :
10152 : : template<typename T> requires !C<T>() void f(T);
10153 : :
10154 : : Normalization leaves f with the associated constraint
10155 : : '!requires (T t) { ... }' which is not transformed into
10156 : : a constraint. */
10157 : 118 : if (!processing_template_decl)
10158 : 118 : return evaluate_requires_expr (t);
10159 : : else
10160 : 0 : *non_constant_p = true;
10161 : 0 : return t;
10162 : :
10163 : 8502 : case ANNOTATE_EXPR:
10164 : 8502 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10165 : : lval,
10166 : : non_constant_p, overflow_p,
10167 : : jump_target);
10168 : 8502 : break;
10169 : :
10170 : 19887 : case USING_STMT:
10171 : 19887 : r = void_node;
10172 : 19887 : break;
10173 : :
10174 : 103 : case ASSERTION_STMT:
10175 : 103 : case PRECONDITION_STMT:
10176 : 103 : case POSTCONDITION_STMT:
10177 : 103 : {
10178 : 103 : if (contract_ignored_p (t))
10179 : : break;
10180 : :
10181 : 87 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
10182 : : G_("contract predicate is false in "
10183 : : "constant expression"),
10184 : 87 : EXPR_LOCATION (t), contract_evaluated_p (t),
10185 : : non_constant_p, overflow_p))
10186 : 38 : *non_constant_p = true;
10187 : 87 : r = void_node;
10188 : : }
10189 : 87 : break;
10190 : :
10191 : 1389521 : case TEMPLATE_ID_EXPR:
10192 : 1389521 : {
10193 : : /* We can evaluate template-id that refers to a concept only if
10194 : : the template arguments are non-dependent. */
10195 : 1389521 : gcc_assert (concept_check_p (t));
10196 : :
10197 : 1389521 : if (!value_dependent_expression_p (t)
10198 : 1389521 : && !uid_sensitive_constexpr_evaluation_p ())
10199 : 1389475 : r = evaluate_concept_check (t);
10200 : : else
10201 : 46 : *non_constant_p = true;
10202 : :
10203 : : break;
10204 : : }
10205 : :
10206 : 48 : case ASM_EXPR:
10207 : 48 : if (!ctx->quiet)
10208 : 28 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10209 : 48 : *non_constant_p = true;
10210 : 48 : return t;
10211 : :
10212 : 1031 : case BIT_CAST_EXPR:
10213 : 1031 : if (lval)
10214 : : {
10215 : 0 : if (!ctx->quiet)
10216 : 0 : error_at (EXPR_LOCATION (t),
10217 : : "address of a call to %qs is not a constant expression",
10218 : : "__builtin_bit_cast");
10219 : 0 : *non_constant_p = true;
10220 : 0 : return t;
10221 : : }
10222 : 1031 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10223 : 1031 : break;
10224 : :
10225 : 0 : case OMP_PARALLEL:
10226 : 0 : case OMP_TASK:
10227 : 0 : case OMP_FOR:
10228 : 0 : case OMP_SIMD:
10229 : 0 : case OMP_DISTRIBUTE:
10230 : 0 : case OMP_TASKLOOP:
10231 : 0 : case OMP_LOOP:
10232 : 0 : case OMP_TEAMS:
10233 : 0 : case OMP_TARGET_DATA:
10234 : 0 : case OMP_TARGET:
10235 : 0 : case OMP_SECTIONS:
10236 : 0 : case OMP_ORDERED:
10237 : 0 : case OMP_CRITICAL:
10238 : 0 : case OMP_SINGLE:
10239 : 0 : case OMP_SCAN:
10240 : 0 : case OMP_SCOPE:
10241 : 0 : case OMP_SECTION:
10242 : 0 : case OMP_STRUCTURED_BLOCK:
10243 : 0 : case OMP_MASTER:
10244 : 0 : case OMP_MASKED:
10245 : 0 : case OMP_TASKGROUP:
10246 : 0 : case OMP_TARGET_UPDATE:
10247 : 0 : case OMP_TARGET_ENTER_DATA:
10248 : 0 : case OMP_TARGET_EXIT_DATA:
10249 : 0 : case OMP_ATOMIC:
10250 : 0 : case OMP_ATOMIC_READ:
10251 : 0 : case OMP_ATOMIC_CAPTURE_OLD:
10252 : 0 : case OMP_ATOMIC_CAPTURE_NEW:
10253 : 0 : case OMP_DEPOBJ:
10254 : 0 : case OACC_PARALLEL:
10255 : 0 : case OACC_KERNELS:
10256 : 0 : case OACC_SERIAL:
10257 : 0 : case OACC_DATA:
10258 : 0 : case OACC_HOST_DATA:
10259 : 0 : case OACC_LOOP:
10260 : 0 : case OACC_CACHE:
10261 : 0 : case OACC_DECLARE:
10262 : 0 : case OACC_ENTER_DATA:
10263 : 0 : case OACC_EXIT_DATA:
10264 : 0 : case OACC_UPDATE:
10265 : 0 : if (!ctx->quiet)
10266 : 0 : error_at (EXPR_LOCATION (t),
10267 : : "statement is not a constant expression");
10268 : 0 : *non_constant_p = true;
10269 : 0 : break;
10270 : :
10271 : 0 : default:
10272 : 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10273 : : {
10274 : : /* This function doesn't know how to deal with pre-genericize
10275 : : statements; this can only happen with statement-expressions,
10276 : : so for now just fail. */
10277 : 0 : if (!ctx->quiet)
10278 : 0 : error_at (EXPR_LOCATION (t),
10279 : : "statement is not a constant expression");
10280 : : }
10281 : 0 : else if (flag_checking)
10282 : 0 : internal_error ("unexpected expression %qE of kind %s", t,
10283 : : get_tree_code_name (TREE_CODE (t)));
10284 : 0 : *non_constant_p = true;
10285 : 0 : break;
10286 : : }
10287 : :
10288 : 777137542 : if (r == error_mark_node)
10289 : 9908496 : *non_constant_p = true;
10290 : :
10291 : 39052824 : if (r == void_node && lval != vc_discard && !*jump_target
10292 : 777148173 : && !VOID_TYPE_P (TREE_TYPE (t)))
10293 : : {
10294 : : /* For diagnostic quality we should have handled this sooner, where we
10295 : : can be more specific about the out-of-lifetime object. But here we
10296 : : can still be correct. */
10297 : 1 : gcc_checking_assert (false);
10298 : : if (!ctx->quiet)
10299 : : error_at (EXPR_LOCATION (t),
10300 : : "%qE accesses an object outside its lifetime", t);
10301 : : *non_constant_p = true;
10302 : : }
10303 : :
10304 : 777137541 : if (*non_constant_p)
10305 : 243314139 : return t;
10306 : : else
10307 : : return r;
10308 : : }
10309 : :
10310 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
10311 : : function that is named by an expression ([basic.def.odr]) that is
10312 : : potentially constant evaluated.
10313 : :
10314 : : So we need to instantiate any constexpr functions mentioned by the
10315 : : expression even if the definition isn't needed for evaluating the
10316 : : expression. */
10317 : :
10318 : : static tree
10319 : 410517631 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10320 : : {
10321 : 410517631 : if (TREE_CODE (*tp) == FUNCTION_DECL
10322 : 10948346 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10323 : 10844377 : && !DECL_INITIAL (*tp)
10324 : 1934104 : && !trivial_fn_p (*tp)
10325 : 1934101 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10326 : 410517631 : && !uid_sensitive_constexpr_evaluation_p ())
10327 : : {
10328 : 1934041 : ++function_depth;
10329 : 1934041 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10330 : 1934036 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10331 : : else
10332 : 5 : synthesize_method (*tp);
10333 : 1934041 : --function_depth;
10334 : : }
10335 : 408583590 : else if (TREE_CODE (*tp) == CALL_EXPR
10336 : 397759675 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10337 : : {
10338 : 11000728 : if (EXPR_HAS_LOCATION (*tp))
10339 : 11000589 : input_location = EXPR_LOCATION (*tp);
10340 : : }
10341 : :
10342 : 410517631 : if (!EXPR_P (*tp))
10343 : 240708640 : *walk_subtrees = 0;
10344 : :
10345 : 410517631 : return NULL_TREE;
10346 : : }
10347 : :
10348 : : static void
10349 : 206589720 : instantiate_constexpr_fns (tree t)
10350 : : {
10351 : 206589720 : location_t loc = input_location;
10352 : 206589720 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10353 : 206589720 : input_location = loc;
10354 : 206589720 : }
10355 : :
10356 : : /* Look for heap variables in the expression *TP. */
10357 : :
10358 : : static tree
10359 : 155109 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10360 : : {
10361 : 155109 : if (VAR_P (*tp)
10362 : 155109 : && (DECL_NAME (*tp) == heap_uninit_identifier
10363 : 5240 : || DECL_NAME (*tp) == heap_identifier
10364 : 1878 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10365 : 1427 : || DECL_NAME (*tp) == heap_vec_identifier
10366 : 529 : || DECL_NAME (*tp) == heap_deleted_identifier))
10367 : : return *tp;
10368 : :
10369 : 111418 : if (TYPE_P (*tp))
10370 : 0 : *walk_subtrees = 0;
10371 : : return NULL_TREE;
10372 : : }
10373 : :
10374 : : /* Find immediate function decls in *TP if any. */
10375 : :
10376 : : static tree
10377 : 206032082 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
10378 : : {
10379 : 207498090 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10380 : : return *tp;
10381 : 206029181 : if (TREE_CODE (*tp) == PTRMEM_CST
10382 : 8018 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10383 : 206036620 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10384 : : return PTRMEM_CST_MEMBER (*tp);
10385 : : return NULL_TREE;
10386 : : }
10387 : :
10388 : : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10389 : : expression. Return a version of T that has TREE_CONSTANT cleared. */
10390 : :
10391 : : static tree
10392 : 137439 : mark_non_constant (tree t)
10393 : : {
10394 : 137439 : gcc_checking_assert (TREE_CONSTANT (t));
10395 : :
10396 : : /* This isn't actually constant, so unset TREE_CONSTANT.
10397 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10398 : : it to be set if it is invariant address, even when it is not
10399 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
10400 : : instead. */
10401 : 137439 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10402 : 136619 : t = copy_node (t);
10403 : 820 : else if (TREE_CODE (t) == CONSTRUCTOR)
10404 : 236 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10405 : : else
10406 : 584 : t = build_nop (TREE_TYPE (t), t);
10407 : 137439 : TREE_CONSTANT (t) = false;
10408 : 137439 : return t;
10409 : : }
10410 : :
10411 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10412 : : STRICT has the same sense as for constant_value_1: true if we only allow
10413 : : conforming C++ constant expressions, or false if we want a constant value
10414 : : even if it doesn't conform.
10415 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10416 : : per P0595 even when ALLOW_NON_CONSTANT is true.
10417 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10418 : : OBJECT must be non-NULL in that case. */
10419 : :
10420 : : static tree
10421 : 385893052 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10422 : : bool strict = true,
10423 : : mce_value manifestly_const_eval = mce_unknown,
10424 : : bool constexpr_dtor = false,
10425 : : tree object = NULL_TREE)
10426 : : {
10427 : 385893052 : auto_timevar time (TV_CONSTEXPR);
10428 : :
10429 : 385893052 : bool non_constant_p = false;
10430 : 385893052 : bool overflow_p = false;
10431 : :
10432 : 385893052 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10433 : : {
10434 : 0 : gcc_checking_assert (allow_non_constant);
10435 : : return t;
10436 : : }
10437 : :
10438 : 385893052 : constexpr_global_ctx global_ctx;
10439 : 385893052 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10440 : : allow_non_constant, strict,
10441 : 385893052 : !allow_non_constant ? mce_true : manifestly_const_eval };
10442 : :
10443 : : /* Turn off -frounding-math for manifestly constant evaluation. */
10444 : 385893052 : warning_sentinel rm (flag_rounding_math,
10445 : 385893052 : ctx.manifestly_const_eval == mce_true);
10446 : 385893052 : tree type = (object
10447 : 385893052 : ? cv_unqualified (TREE_TYPE (object))
10448 : 361690116 : : initialized_type (t));
10449 : 385893052 : tree r = t;
10450 : 385893052 : bool is_consteval = false;
10451 : 385893052 : if (VOID_TYPE_P (type))
10452 : : {
10453 : 3715816 : if (!constexpr_dtor)
10454 : : {
10455 : 3715816 : if (cxx_dialect < cxx20)
10456 : : return t;
10457 : : /* We could have a COMPOUND_EXPR here coming from
10458 : : keep_unused_object_arg. */
10459 : 3379808 : tree x = extract_call_expr (t);
10460 : 3379808 : if (x == NULL_TREE || x == error_mark_node)
10461 : : return t;
10462 : : /* Calls to immediate functions returning void need to be
10463 : : evaluated. */
10464 : 3379797 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10465 : 6759594 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10466 : : return t;
10467 : : else
10468 : : is_consteval = true;
10469 : : }
10470 : : }
10471 : 382177236 : else if (cxx_dialect >= cxx20
10472 : 169490493 : && (TREE_CODE (t) == CALL_EXPR
10473 : 143716484 : || TREE_CODE (t) == AGGR_INIT_EXPR
10474 : 142187537 : || TREE_CODE (t) == TARGET_EXPR))
10475 : : {
10476 : 29065542 : tree x = t;
10477 : 29065542 : if (TREE_CODE (x) == TARGET_EXPR)
10478 : 1762586 : x = TARGET_EXPR_INITIAL (x);
10479 : 29065542 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10480 : 57260996 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10481 : : is_consteval = true;
10482 : : /* Don't try to evaluate a std::vector constructor taking an integer, it
10483 : : will fail in the 'if (heap_var)' block below after doing all the work
10484 : : (c++/113835). This will need adjustment if P3554 is accepted. Note
10485 : : that evaluation of e.g. the vector default constructor can succeed, so
10486 : : we don't shortcut all vector constructors. */
10487 : 56390908 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10488 : 4069028 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10489 : 29070533 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10490 : : return t;
10491 : : }
10492 : 382176950 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10493 : : {
10494 : : /* In C++14 an NSDMI can participate in aggregate initialization,
10495 : : and can refer to the address of the object being initialized, so
10496 : : we need to pass in the relevant VAR_DECL if we want to do the
10497 : : evaluation in a single pass. The evaluation will dynamically
10498 : : update ctx.values for the VAR_DECL. We use the same strategy
10499 : : for C++11 constexpr constructors that refer to the object being
10500 : : initialized. */
10501 : 24464561 : if (constexpr_dtor)
10502 : : {
10503 : 147 : gcc_assert (object && VAR_P (object));
10504 : 147 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10505 : 147 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10506 : 147 : if (error_operand_p (DECL_INITIAL (object)))
10507 : : return t;
10508 : 136 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10509 : 136 : TREE_READONLY (ctx.ctor) = false;
10510 : : /* Temporarily force decl_really_constant_value to return false
10511 : : for it, we want to use ctx.ctor for the current value instead. */
10512 : 136 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10513 : : }
10514 : : else
10515 : : {
10516 : 24464414 : ctx.ctor = build_constructor (type, NULL);
10517 : 24464414 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10518 : : }
10519 : 24464550 : if (!object)
10520 : : {
10521 : 13561362 : if (TREE_CODE (t) == CALL_EXPR)
10522 : : {
10523 : : /* If T is calling a constructor to initialize an object, reframe
10524 : : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10525 : : from outside the constant evaluation, which will fail even if
10526 : : the value is actually constant (is_constant_evaluated3.C). */
10527 : 5269011 : tree fn = cp_get_callee_fndecl_nofold (t);
10528 : 10538014 : if (fn && DECL_CONSTRUCTOR_P (fn))
10529 : : {
10530 : 2385965 : object = CALL_EXPR_ARG (t, 0);
10531 : 2385965 : object = build_fold_indirect_ref (object);
10532 : 2385965 : r = build_aggr_init_expr (type, r);
10533 : : }
10534 : : }
10535 : 8292351 : else if (TREE_CODE (t) == TARGET_EXPR)
10536 : 3150496 : object = TARGET_EXPR_SLOT (t);
10537 : 5141855 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10538 : 444201 : object = AGGR_INIT_EXPR_SLOT (t);
10539 : : }
10540 : 24464550 : ctx.object = object;
10541 : 24464550 : if (object)
10542 : 16883850 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10543 : : (type, TREE_TYPE (object)));
10544 : 16883850 : if (object && DECL_P (object))
10545 : 14281851 : global_ctx.put_value (object, ctx.ctor);
10546 : 24464550 : if (TREE_CODE (r) == TARGET_EXPR)
10547 : : /* Avoid creating another CONSTRUCTOR when we expand the
10548 : : TARGET_EXPR. */
10549 : 3211204 : r = TARGET_EXPR_INITIAL (r);
10550 : : }
10551 : :
10552 : 382176939 : auto_vec<tree, 16> cleanups;
10553 : 382176939 : global_ctx.cleanups = &cleanups;
10554 : :
10555 : 382176939 : if (manifestly_const_eval == mce_true)
10556 : 206589720 : instantiate_constexpr_fns (r);
10557 : 382176939 : tree jmp_target = NULL_TREE;
10558 : 382176939 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
10559 : : &non_constant_p, &overflow_p,
10560 : : &jmp_target);
10561 : 382174523 : if (throws (&jmp_target) && !non_constant_p)
10562 : : {
10563 : 282 : if (!ctx.quiet)
10564 : 76 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10565 : 282 : non_constant_p = true;
10566 : 282 : jmp_target = NULL_TREE;
10567 : 282 : r = t;
10568 : : }
10569 : 382173959 : else if (!non_constant_p && jmp_target)
10570 : : {
10571 : 24 : non_constant_p = true;
10572 : 24 : if (!ctx.quiet)
10573 : : {
10574 : 3 : if (breaks (&jmp_target))
10575 : 3 : error ("%<break%> outside of a loop or %<switch%>");
10576 : 0 : else if (continues (&jmp_target))
10577 : 0 : error ("%<continue%> outside of a loop");
10578 : 0 : else if (returns (&jmp_target))
10579 : 0 : error ("%<return%> in a statement expression");
10580 : : else
10581 : 0 : gcc_unreachable ();
10582 : : }
10583 : 24 : r = t;
10584 : : }
10585 : :
10586 : : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
10587 : : of statements, and the result ought to be stored in ctx.ctor. */
10588 : 382174241 : if (r == void_node && !constexpr_dtor && ctx.ctor)
10589 : 0 : r = ctx.ctor;
10590 : :
10591 : 382174241 : unsigned int i;
10592 : 382174241 : tree cleanup;
10593 : 382174241 : jmp_target = NULL_TREE;
10594 : : /* Evaluate the cleanups. */
10595 : 764369466 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
10596 : 20984 : if (cleanup == NULL_TREE)
10597 : : /* NULL_TREE cleanup is a marker that before it is
10598 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
10599 : 23 : --i;
10600 : : else
10601 : 20961 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
10602 : : &non_constant_p, &overflow_p,
10603 : : &jmp_target);
10604 : 382174241 : if (throws (&jmp_target) && !non_constant_p)
10605 : : {
10606 : 0 : if (!ctx.quiet)
10607 : 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10608 : 0 : non_constant_p = true;
10609 : 0 : r = t;
10610 : : }
10611 : :
10612 : : /* Mutable logic is a bit tricky: we want to allow initialization of
10613 : : constexpr variables with mutable members, but we can't copy those
10614 : : members to another constexpr variable. */
10615 : 382174241 : if (!non_constant_p
10616 : 288904591 : && TREE_CODE (r) == CONSTRUCTOR
10617 : 392248146 : && CONSTRUCTOR_MUTABLE_POISON (r))
10618 : : {
10619 : 93 : if (!allow_non_constant)
10620 : 6 : error ("%qE is not a constant expression because it refers to "
10621 : : "mutable subobjects of %qT", t, type);
10622 : 93 : non_constant_p = true;
10623 : : }
10624 : :
10625 : 288904498 : if (!non_constant_p && cxx_dialect >= cxx20
10626 : 671078739 : && !global_ctx.heap_vars.is_empty ())
10627 : : {
10628 : 44228 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
10629 : : NULL);
10630 : 44228 : unsigned int i;
10631 : 44228 : if (heap_var)
10632 : : {
10633 : 43691 : if (!allow_non_constant && !non_constant_p)
10634 : : {
10635 : 11 : if (DECL_LANG_SPECIFIC (heap_var))
10636 : 2 : error ("%qE is not a constant expression because it refers to "
10637 : : "exception object allocated with "
10638 : : "%<__cxa_allocate_exception%>", t);
10639 : : else
10640 : 9 : error ("%qE is not a constant expression because it refers to "
10641 : : "a result of %<operator new%>", t);
10642 : 11 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10643 : : }
10644 : 43691 : r = t;
10645 : 43691 : non_constant_p = true;
10646 : : }
10647 : 92641 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
10648 : : {
10649 : 48413 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
10650 : : {
10651 : 43750 : if (!allow_non_constant && !non_constant_p)
10652 : : {
10653 : 16 : error ("%qE is not a constant expression because allocated "
10654 : : "storage has not been deallocated", t);
10655 : 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10656 : : }
10657 : 43750 : r = t;
10658 : 43750 : non_constant_p = true;
10659 : : }
10660 : : }
10661 : : }
10662 : :
10663 : : /* Check that immediate invocation does not return an expression referencing
10664 : : any immediate function decls. */
10665 : 382174241 : if (!non_constant_p && cxx_dialect >= cxx20)
10666 : 249450410 : if (tree immediate_fndecl
10667 : 124725205 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
10668 : : NULL))
10669 : : {
10670 : 3006 : if (!allow_non_constant && !non_constant_p)
10671 : : {
10672 : 72 : if (is_consteval)
10673 : 36 : error_at (cp_expr_loc_or_input_loc (t),
10674 : : "immediate evaluation returns address of immediate "
10675 : : "function %qD", immediate_fndecl);
10676 : : else
10677 : 54 : error_at (cp_expr_loc_or_input_loc (t),
10678 : : "constant evaluation returns address of immediate "
10679 : : "function %qD", immediate_fndecl);
10680 : : }
10681 : 3006 : r = t;
10682 : 3006 : non_constant_p = true;
10683 : : }
10684 : :
10685 : 382174241 : if (!non_constant_p && !constexpr_dtor)
10686 : 288857606 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
10687 : :
10688 : : /* After verify_constant because reduced_constant_expression_p can unset
10689 : : CONSTRUCTOR_NO_CLEARING. */
10690 : 382174241 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
10691 : : {
10692 : 83798 : if (!allow_non_constant)
10693 : 40 : error ("%qE is not a constant expression because it refers to "
10694 : : "an incompletely initialized variable", t);
10695 : 83798 : TREE_CONSTANT (r) = false;
10696 : 83798 : non_constant_p = true;
10697 : : }
10698 : :
10699 : 382174241 : if (non_constant_p)
10700 : : /* If we saw something bad, go back to our argument. The wrapping below is
10701 : : only for the cases of TREE_CONSTANT argument or overflow. */
10702 : 95394220 : r = t;
10703 : :
10704 : 382174241 : if (!non_constant_p && overflow_p)
10705 : 215 : non_constant_p = true;
10706 : :
10707 : : /* Unshare the result. */
10708 : 382174241 : bool should_unshare = true;
10709 : 382174241 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
10710 : 1009586 : && TARGET_EXPR_INITIAL (t) == r))
10711 : : should_unshare = false;
10712 : :
10713 : 382174241 : if (non_constant_p && !allow_non_constant)
10714 : 2640 : return error_mark_node;
10715 : 382171601 : else if (non_constant_p && TREE_CONSTANT (r))
10716 : 133114 : r = mark_non_constant (r);
10717 : 382038487 : else if (non_constant_p)
10718 : : return t;
10719 : :
10720 : 286912920 : if (constexpr_dtor)
10721 : : {
10722 : 118 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
10723 : 118 : return r;
10724 : : }
10725 : :
10726 : : /* Check we are not trying to return the wrong type. */
10727 : 286912802 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
10728 : : {
10729 : : /* If so, this is not a constant expression. */
10730 : 134 : if (!allow_non_constant)
10731 : 0 : error ("%qE is not a constant expression because it initializes "
10732 : 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
10733 : 134 : return t;
10734 : : }
10735 : :
10736 : 286912668 : if (should_unshare)
10737 : 149364063 : r = unshare_expr (r);
10738 : :
10739 : 286912668 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
10740 : : {
10741 : 9390540 : r = adjust_temp_type (type, r);
10742 : 9390540 : if (TREE_CODE (t) == TARGET_EXPR
10743 : 9390540 : && TARGET_EXPR_INITIAL (t) == r)
10744 : : return t;
10745 : 8606090 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
10746 : 1148171 : || TREE_CODE (t) == AGGR_INIT_EXPR)
10747 : : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
10748 : 245327 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
10749 : 1290 : r = get_target_expr (r);
10750 : : else
10751 : : {
10752 : 244037 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
10753 : 244037 : TREE_CONSTANT (r) = true;
10754 : : }
10755 : : }
10756 : :
10757 : 286128218 : if (TREE_CODE (t) == TARGET_EXPR
10758 : 225136 : && TREE_CODE (r) == TARGET_EXPR)
10759 : : {
10760 : : /* Preserve this flag for potential_constant_expression, and the others
10761 : : for good measure. */
10762 : 225042 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
10763 : 225042 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
10764 : 225042 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
10765 : 225042 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
10766 : : }
10767 : :
10768 : : /* Remember the original location if that wouldn't need a wrapper. */
10769 : 286128218 : if (location_t loc = EXPR_LOCATION (t))
10770 : 117687981 : protected_set_expr_location (r, loc);
10771 : :
10772 : 286128218 : return r;
10773 : 385890354 : }
10774 : :
10775 : : /* If T represents a constant expression returns its reduced value.
10776 : : Otherwise return error_mark_node. */
10777 : :
10778 : : tree
10779 : 125483883 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
10780 : : tsubst_flags_t complain /* = tf_error */)
10781 : : {
10782 : 125483883 : bool sfinae = !(complain & tf_error);
10783 : 125483883 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
10784 : 125483883 : if (sfinae && !TREE_CONSTANT (r))
10785 : 884 : r = error_mark_node;
10786 : 125483883 : return r;
10787 : : }
10788 : :
10789 : : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
10790 : : of constexpr variables. The actual initializer of DECL is not modified. */
10791 : :
10792 : : void
10793 : 147 : cxx_constant_dtor (tree t, tree decl)
10794 : : {
10795 : 147 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
10796 : 147 : }
10797 : :
10798 : : /* Helper routine for fold_simple function. Either return simplified
10799 : : expression T, otherwise NULL_TREE.
10800 : : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
10801 : : even if we are within template-declaration. So be careful on call, as in
10802 : : such case types can be undefined. */
10803 : :
10804 : : static tree
10805 : 109344331 : fold_simple_1 (tree t)
10806 : : {
10807 : 109344331 : tree op1;
10808 : 109344331 : enum tree_code code = TREE_CODE (t);
10809 : :
10810 : 109344331 : switch (code)
10811 : : {
10812 : : case INTEGER_CST:
10813 : : case REAL_CST:
10814 : : case VECTOR_CST:
10815 : : case FIXED_CST:
10816 : : case COMPLEX_CST:
10817 : : return t;
10818 : :
10819 : 983202 : case SIZEOF_EXPR:
10820 : 983202 : return fold_sizeof_expr (t);
10821 : :
10822 : 30772446 : case ABS_EXPR:
10823 : 30772446 : case ABSU_EXPR:
10824 : 30772446 : case CONJ_EXPR:
10825 : 30772446 : case REALPART_EXPR:
10826 : 30772446 : case IMAGPART_EXPR:
10827 : 30772446 : case NEGATE_EXPR:
10828 : 30772446 : case BIT_NOT_EXPR:
10829 : 30772446 : case TRUTH_NOT_EXPR:
10830 : 30772446 : case VIEW_CONVERT_EXPR:
10831 : 30772446 : CASE_CONVERT:
10832 : 30772446 : case FLOAT_EXPR:
10833 : 30772446 : case FIX_TRUNC_EXPR:
10834 : 30772446 : case FIXED_CONVERT_EXPR:
10835 : 30772446 : case ADDR_SPACE_CONVERT_EXPR:
10836 : :
10837 : 30772446 : op1 = TREE_OPERAND (t, 0);
10838 : :
10839 : 30772446 : t = const_unop (code, TREE_TYPE (t), op1);
10840 : 30772446 : if (!t)
10841 : : return NULL_TREE;
10842 : :
10843 : 1815436 : if (CONVERT_EXPR_CODE_P (code)
10844 : 1815436 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
10845 : 0 : TREE_OVERFLOW (t) = false;
10846 : : return t;
10847 : :
10848 : : default:
10849 : : return NULL_TREE;
10850 : : }
10851 : : }
10852 : :
10853 : : /* If T is a simple constant expression, returns its simplified value.
10854 : : Otherwise returns T. In contrast to maybe_constant_value we
10855 : : simplify only few operations on constant-expressions, and we don't
10856 : : try to simplify constexpressions. */
10857 : :
10858 : : tree
10859 : 109866707 : fold_simple (tree t)
10860 : : {
10861 : 109866707 : if (processing_template_decl)
10862 : : return t;
10863 : :
10864 : 109344331 : tree r = fold_simple_1 (t);
10865 : 109344331 : if (r)
10866 : : return r;
10867 : :
10868 : : return t;
10869 : : }
10870 : :
10871 : : /* Try folding the expression T to a simple constant.
10872 : : Returns that constant, otherwise returns T. */
10873 : :
10874 : : tree
10875 : 1012004 : fold_to_constant (tree t)
10876 : : {
10877 : 1012004 : if (processing_template_decl)
10878 : : return t;
10879 : :
10880 : 914158 : tree r = fold (t);
10881 : 914158 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
10882 : : return r;
10883 : : else
10884 : : return t;
10885 : : }
10886 : :
10887 : : /* If T is a constant expression, returns its reduced value.
10888 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
10889 : : Otherwise, returns a version of T without TREE_CONSTANT.
10890 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
10891 : : as per P0595. */
10892 : :
10893 : : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
10894 : :
10895 : : tree
10896 : 561548104 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
10897 : : mce_value manifestly_const_eval /* = mce_unknown */)
10898 : : {
10899 : 561548104 : tree orig_t = t;
10900 : 561548104 : tree r;
10901 : :
10902 : 561548104 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
10903 : : {
10904 : : /* Look up each operand in the cv_cache first to see if we've already
10905 : : reduced it, and reuse that result to avoid quadratic behavior if
10906 : : we're called when building up a large expression. */
10907 : 201342382 : int n = cp_tree_operand_length (t);
10908 : 201342382 : tree *ops = XALLOCAVEC (tree, n);
10909 : 201342382 : bool rebuild = false;
10910 : 572504556 : for (int i = 0; i < n; ++i)
10911 : : {
10912 : 371162174 : ops[i] = TREE_OPERAND (t, i);
10913 : 1112631820 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
10914 : 26992225 : if (*cached != ops[i])
10915 : : {
10916 : 7871012 : ops[i] = *cached;
10917 : 7871012 : rebuild = true;
10918 : : }
10919 : : }
10920 : 201342382 : if (rebuild)
10921 : : {
10922 : 6823951 : t = copy_node (t);
10923 : 22251229 : for (int i = 0; i < n; ++i)
10924 : 15427278 : TREE_OPERAND (t, i) = ops[i];
10925 : : }
10926 : : }
10927 : :
10928 : 561548104 : if (!is_nondependent_constant_expression (t))
10929 : : {
10930 : 0 : if (TREE_OVERFLOW_P (t)
10931 : 119843610 : || (!processing_template_decl && TREE_CONSTANT (t)))
10932 : 4325 : t = mark_non_constant (t);
10933 : 119843610 : return t;
10934 : : }
10935 : 441704494 : else if (CONSTANT_CLASS_P (t))
10936 : : /* No caching or evaluation needed. */
10937 : : return t;
10938 : :
10939 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
10940 : : but at least try folding it to a simple constant. */
10941 : 227591801 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
10942 : 454451 : return fold_to_constant (t);
10943 : :
10944 : 221191836 : if (manifestly_const_eval != mce_unknown)
10945 : : /* TODO: Extend the cache to be mce_value aware. And if we have a
10946 : : previously cached mce_unknown result that's TREE_CONSTANT, it means
10947 : : the reduced value is independent of mce_value and so we should
10948 : : be able to reuse it in the mce_true/false case. */
10949 : 106124992 : return cxx_eval_outermost_constant_expr (t, true, true,
10950 : 106122294 : manifestly_const_eval, false, decl);
10951 : :
10952 : 121012358 : if (cv_cache == NULL)
10953 : 140334 : cv_cache = hash_map<tree, tree>::create_ggc (101);
10954 : 121012358 : if (tree *cached = cv_cache->get (t))
10955 : : {
10956 : 9388370 : r = *cached;
10957 : 9388370 : if (r != t)
10958 : : {
10959 : : /* Clear processing_template_decl for sake of break_out_target_exprs;
10960 : : entries in the cv_cache are non-templated. */
10961 : 2792234 : processing_template_decl_sentinel ptds;
10962 : :
10963 : 2792234 : r = break_out_target_exprs (r, /*clear_loc*/true);
10964 : 2792234 : protected_set_expr_location (r, EXPR_LOCATION (t));
10965 : 2792234 : }
10966 : 9388370 : return r;
10967 : : }
10968 : :
10969 : 111623988 : uid_sensitive_constexpr_evaluation_checker c;
10970 : 111623988 : r = cxx_eval_outermost_constant_expr (t, true, true,
10971 : : manifestly_const_eval, false, decl);
10972 : 111623988 : gcc_checking_assert (r == t
10973 : : || CONVERT_EXPR_P (t)
10974 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
10975 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10976 : : || !cp_tree_equal (r, t));
10977 : 111623988 : if (!c.evaluation_restricted_p ())
10978 : 111442683 : cv_cache->put (orig_t, r);
10979 : : return r;
10980 : : }
10981 : :
10982 : : /* Dispose of the whole CV_CACHE. */
10983 : :
10984 : : static void
10985 : 26760597 : clear_cv_cache (void)
10986 : : {
10987 : 26760597 : if (cv_cache != NULL)
10988 : 26529998 : cv_cache->empty ();
10989 : 26760597 : }
10990 : :
10991 : : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
10992 : :
10993 : : void
10994 : 26760597 : clear_cv_and_fold_caches ()
10995 : : {
10996 : 26760597 : clear_cv_cache ();
10997 : 26760597 : clear_fold_cache ();
10998 : 26760597 : }
10999 : :
11000 : : /* Internal function handling expressions in templates for
11001 : : fold_non_dependent_expr and fold_non_dependent_init.
11002 : :
11003 : : If we're in a template, but T isn't value dependent, simplify
11004 : : it. We're supposed to treat:
11005 : :
11006 : : template <typename T> void f(T[1 + 1]);
11007 : : template <typename T> void f(T[2]);
11008 : :
11009 : : as two declarations of the same function, for example. */
11010 : :
11011 : : static tree
11012 : 25819186 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11013 : : bool manifestly_const_eval,
11014 : : tree object)
11015 : : {
11016 : 25819186 : gcc_assert (processing_template_decl);
11017 : :
11018 : 25819186 : if (is_nondependent_constant_expression (t))
11019 : : {
11020 : 14119465 : processing_template_decl_sentinel s;
11021 : 14119465 : t = instantiate_non_dependent_expr_internal (t, complain);
11022 : :
11023 : 14119465 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11024 : : {
11025 : 0 : if (TREE_OVERFLOW_P (t))
11026 : : {
11027 : 0 : t = build_nop (TREE_TYPE (t), t);
11028 : 0 : TREE_CONSTANT (t) = false;
11029 : : }
11030 : 0 : return t;
11031 : : }
11032 : 14119465 : else if (CONSTANT_CLASS_P (t))
11033 : : /* No evaluation needed. */
11034 : : return t;
11035 : :
11036 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11037 : : but at least try folding it to a simple constant. */
11038 : 3671934 : if (cp_unevaluated_operand && !manifestly_const_eval)
11039 : 89 : return fold_to_constant (t);
11040 : :
11041 : 3671845 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11042 : : mce_value (manifestly_const_eval),
11043 : : false, object);
11044 : : /* cp_tree_equal looks through NOPs, so allow them. */
11045 : 3671845 : gcc_checking_assert (r == t
11046 : : || CONVERT_EXPR_P (t)
11047 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11048 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11049 : : || !cp_tree_equal (r, t));
11050 : 3671845 : return r;
11051 : 14119465 : }
11052 : 11699721 : else if (TREE_OVERFLOW_P (t))
11053 : : {
11054 : 0 : t = build_nop (TREE_TYPE (t), t);
11055 : 0 : TREE_CONSTANT (t) = false;
11056 : : }
11057 : :
11058 : : return t;
11059 : : }
11060 : :
11061 : : /* Like maybe_constant_value but first fully instantiate the argument.
11062 : :
11063 : : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11064 : : followed by maybe_constant_value but is more efficient,
11065 : : because it calls instantiation_dependent_expression_p and
11066 : : potential_constant_expression at most once.
11067 : : The manifestly_const_eval argument is passed to maybe_constant_value.
11068 : :
11069 : : Callers should generally pass their active complain, or if they are in a
11070 : : non-template, diagnosing context, they can use the default of
11071 : : tf_warning_or_error. Callers that might be within a template context, don't
11072 : : have a complain parameter, and aren't going to remember the result for long
11073 : : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11074 : : appropriately. */
11075 : :
11076 : : tree
11077 : 125670089 : fold_non_dependent_expr (tree t,
11078 : : tsubst_flags_t complain /* = tf_warning_or_error */,
11079 : : bool manifestly_const_eval /* = false */,
11080 : : tree object /* = NULL_TREE */)
11081 : : {
11082 : 125670089 : if (t == NULL_TREE)
11083 : : return NULL_TREE;
11084 : :
11085 : 125142213 : if (processing_template_decl)
11086 : 24807806 : return fold_non_dependent_expr_template (t, complain,
11087 : 24807806 : manifestly_const_eval, object);
11088 : :
11089 : 100334407 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11090 : : }
11091 : :
11092 : : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11093 : : return the original expression. */
11094 : :
11095 : : tree
11096 : 2047158 : maybe_fold_non_dependent_expr (tree expr,
11097 : : tsubst_flags_t complain/*=tf_warning_or_error*/)
11098 : : {
11099 : 2047158 : tree t = fold_non_dependent_expr (expr, complain);
11100 : 2047158 : if (t && TREE_CONSTANT (t))
11101 : 1022923 : return t;
11102 : :
11103 : : return expr;
11104 : : }
11105 : :
11106 : : /* Like maybe_constant_init but first fully instantiate the argument. */
11107 : :
11108 : : tree
11109 : 28085139 : fold_non_dependent_init (tree t,
11110 : : tsubst_flags_t complain /*=tf_warning_or_error*/,
11111 : : bool manifestly_const_eval /*=false*/,
11112 : : tree object /* = NULL_TREE */)
11113 : : {
11114 : 28085139 : if (t == NULL_TREE)
11115 : : return NULL_TREE;
11116 : :
11117 : 28085139 : if (processing_template_decl)
11118 : : {
11119 : 1011380 : t = fold_non_dependent_expr_template (t, complain,
11120 : : manifestly_const_eval, object);
11121 : : /* maybe_constant_init does this stripping, so do it here too. */
11122 : 1011380 : if (TREE_CODE (t) == TARGET_EXPR)
11123 : : {
11124 : 61 : tree init = TARGET_EXPR_INITIAL (t);
11125 : 61 : if (TREE_CODE (init) == CONSTRUCTOR)
11126 : 1011380 : t = init;
11127 : : }
11128 : 1011380 : return t;
11129 : : }
11130 : :
11131 : 27073759 : return maybe_constant_init (t, object, manifestly_const_eval);
11132 : : }
11133 : :
11134 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11135 : : than wrapped in a TARGET_EXPR.
11136 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11137 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11138 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
11139 : :
11140 : : static tree
11141 : 64896332 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11142 : : mce_value manifestly_const_eval)
11143 : : {
11144 : 64896332 : if (!t)
11145 : : return t;
11146 : 64896332 : if (TREE_CODE (t) == EXPR_STMT)
11147 : 23855 : t = TREE_OPERAND (t, 0);
11148 : 64896332 : if (TREE_CODE (t) == CONVERT_EXPR
11149 : 64896332 : && VOID_TYPE_P (TREE_TYPE (t)))
11150 : 34546 : t = TREE_OPERAND (t, 0);
11151 : : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11152 : : DECL and losing that information would cause mischief later. */
11153 : 64896332 : if (TREE_CODE (t) == INIT_EXPR
11154 : 64896332 : && (!decl
11155 : 10702 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11156 : 10702 : TREE_TYPE (t))))
11157 : 24814 : t = TREE_OPERAND (t, 1);
11158 : 64896332 : if (TREE_CODE (t) == TARGET_EXPR)
11159 : 441798 : t = TARGET_EXPR_INITIAL (t);
11160 : 64896332 : if (!is_nondependent_static_init_expression (t))
11161 : : /* Don't try to evaluate it. */;
11162 : 56170623 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11163 : : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11164 : : else
11165 : : {
11166 : : /* [basic.start.static] allows constant-initialization of variables with
11167 : : static or thread storage duration even if it isn't required, but we
11168 : : shouldn't bend the rules the same way for automatic variables.
11169 : :
11170 : : But still enforce the requirements of constexpr/constinit.
11171 : : [dcl.constinit] "If a variable declared with the constinit specifier
11172 : : has dynamic initialization, the program is ill-formed, even if the
11173 : : implementation would perform that initialization as a static
11174 : : initialization." */
11175 : 11673803 : bool is_static = (decl && DECL_P (decl)
11176 : 38020074 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11177 : 1712607 : bool strict = (!is_static
11178 : : || (decl && DECL_P (decl)
11179 : 1712607 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11180 : 907519 : || DECL_DECLARED_CONSTINIT_P (decl))));
11181 : : if (is_static)
11182 : : manifestly_const_eval = mce_true;
11183 : :
11184 : 26776955 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11185 : 35911 : return fold_to_constant (t);
11186 : :
11187 : 26741044 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11188 : : manifestly_const_eval,
11189 : : false, decl);
11190 : : }
11191 : 64860421 : if (TREE_CODE (t) == TARGET_EXPR)
11192 : : {
11193 : 19823 : tree init = TARGET_EXPR_INITIAL (t);
11194 : 19823 : if (TREE_CODE (init) == CONSTRUCTOR)
11195 : 64896332 : t = init;
11196 : : }
11197 : : return t;
11198 : : }
11199 : :
11200 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11201 : :
11202 : : tree
11203 : 49242942 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11204 : : {
11205 : 49242942 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11206 : : }
11207 : :
11208 : : tree
11209 : 15651920 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11210 : : {
11211 : 15651920 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11212 : : }
11213 : :
11214 : : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11215 : :
11216 : : tree
11217 : 1470 : cxx_constant_init (tree t, tree decl)
11218 : : {
11219 : 1470 : return maybe_constant_init_1 (t, decl, false, mce_true);
11220 : : }
11221 : :
11222 : : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11223 : :
11224 : : static bool
11225 : 136683954 : callee_might_throw (tree t)
11226 : : {
11227 : 136683954 : if (cxx_dialect < cxx26 || !flag_exceptions)
11228 : : return false;
11229 : 38628185 : tree callee = cp_get_callee (t);
11230 : 38628185 : if (callee == NULL_TREE)
11231 : : return false;
11232 : 38575259 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11233 : 38575259 : return (!flag_enforce_eh_specs
11234 : 38575259 : || type_dependent_expression_p (callee)
11235 : 34559330 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11236 : 70323526 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11237 : 12381428 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11238 : : }
11239 : :
11240 : : #if 0
11241 : : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11242 : : /* Return true if the object referred to by REF has automatic or thread
11243 : : local storage. */
11244 : :
11245 : : enum { ck_ok, ck_bad, ck_unknown };
11246 : : static int
11247 : : check_automatic_or_tls (tree ref)
11248 : : {
11249 : : machine_mode mode;
11250 : : poly_int64 bitsize, bitpos;
11251 : : tree offset;
11252 : : int volatilep = 0, unsignedp = 0;
11253 : : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11254 : : &mode, &unsignedp, &volatilep, false);
11255 : : duration_kind dk;
11256 : :
11257 : : /* If there isn't a decl in the middle, we don't know the linkage here,
11258 : : and this isn't a constant expression anyway. */
11259 : : if (!DECL_P (decl))
11260 : : return ck_unknown;
11261 : : dk = decl_storage_duration (decl);
11262 : : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11263 : : }
11264 : : #endif
11265 : :
11266 : : /* Data structure for passing data from potential_constant_expression_1
11267 : : to check_for_return_continue via cp_walk_tree. */
11268 : : struct check_for_return_continue_data {
11269 : : hash_set<tree> *pset;
11270 : : tree continue_stmt;
11271 : : tree break_stmt;
11272 : : bool could_throw;
11273 : : };
11274 : :
11275 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11276 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11277 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11278 : : For C++26 also note presence of possibly throwing calls. */
11279 : : static tree
11280 : 23300394 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11281 : : {
11282 : 23300394 : tree t = *tp, s, b;
11283 : 23300394 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11284 : 23300394 : switch (TREE_CODE (t))
11285 : : {
11286 : : case RETURN_EXPR:
11287 : : return t;
11288 : :
11289 : 30 : case CONTINUE_STMT:
11290 : 30 : if (d->continue_stmt == NULL_TREE)
11291 : 30 : d->continue_stmt = t;
11292 : : break;
11293 : :
11294 : 5237 : case BREAK_STMT:
11295 : 5237 : if (d->break_stmt == NULL_TREE)
11296 : 3110 : d->break_stmt = t;
11297 : : break;
11298 : :
11299 : : #define RECUR(x) \
11300 : : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11301 : : d->pset)) \
11302 : : return r
11303 : :
11304 : : /* For loops, walk subtrees manually, so that continue stmts found
11305 : : inside of the bodies of the loops are ignored. */
11306 : 13368 : case DO_STMT:
11307 : 13368 : *walk_subtrees = 0;
11308 : 13368 : RECUR (DO_COND (t));
11309 : 13368 : s = d->continue_stmt;
11310 : 13368 : b = d->break_stmt;
11311 : 13368 : RECUR (DO_BODY (t));
11312 : 13368 : d->continue_stmt = s;
11313 : 13368 : d->break_stmt = b;
11314 : 13368 : break;
11315 : :
11316 : 101 : case WHILE_STMT:
11317 : 101 : *walk_subtrees = 0;
11318 : 101 : RECUR (WHILE_COND_PREP (t));
11319 : 101 : RECUR (WHILE_COND (t));
11320 : 101 : s = d->continue_stmt;
11321 : 101 : b = d->break_stmt;
11322 : 101 : RECUR (WHILE_BODY (t));
11323 : 95 : d->continue_stmt = s;
11324 : 95 : d->break_stmt = b;
11325 : 95 : break;
11326 : :
11327 : 140 : case FOR_STMT:
11328 : 140 : *walk_subtrees = 0;
11329 : 140 : RECUR (FOR_INIT_STMT (t));
11330 : 140 : RECUR (FOR_COND_PREP (t));
11331 : 140 : RECUR (FOR_COND (t));
11332 : 140 : RECUR (FOR_EXPR (t));
11333 : 140 : s = d->continue_stmt;
11334 : 140 : b = d->break_stmt;
11335 : 140 : RECUR (FOR_BODY (t));
11336 : 94 : d->continue_stmt = s;
11337 : 94 : d->break_stmt = b;
11338 : 94 : break;
11339 : :
11340 : 0 : case RANGE_FOR_STMT:
11341 : 0 : *walk_subtrees = 0;
11342 : 0 : RECUR (RANGE_FOR_EXPR (t));
11343 : 0 : s = d->continue_stmt;
11344 : 0 : b = d->break_stmt;
11345 : 0 : RECUR (RANGE_FOR_BODY (t));
11346 : 0 : d->continue_stmt = s;
11347 : 0 : d->break_stmt = b;
11348 : 0 : break;
11349 : :
11350 : 207 : case SWITCH_STMT:
11351 : 207 : *walk_subtrees = 0;
11352 : 207 : RECUR (SWITCH_STMT_COND (t));
11353 : 207 : b = d->break_stmt;
11354 : 207 : RECUR (SWITCH_STMT_BODY (t));
11355 : 187 : d->break_stmt = b;
11356 : 187 : break;
11357 : : #undef RECUR
11358 : :
11359 : : case STATEMENT_LIST:
11360 : : case CONSTRUCTOR:
11361 : : break;
11362 : :
11363 : 1173833 : case AGGR_INIT_EXPR:
11364 : 1173833 : case CALL_EXPR:
11365 : : /* In C++26 a function could throw. */
11366 : 1173833 : if (callee_might_throw (t))
11367 : 101101 : d->could_throw = true;
11368 : : break;
11369 : :
11370 : 21377203 : default:
11371 : 21377203 : if (!EXPR_P (t))
11372 : 6684332 : *walk_subtrees = 0;
11373 : : break;
11374 : : }
11375 : :
11376 : : return NULL_TREE;
11377 : : }
11378 : :
11379 : : /* Return true if T denotes a potentially constant expression. Issue
11380 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11381 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11382 : : consider the expression in the current context, independent of constexpr
11383 : : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11384 : : and hard errors should not be reported by constexpr_error.
11385 : :
11386 : : C++0x [expr.const] used to say
11387 : :
11388 : : 6 An expression is a potential constant expression if it is
11389 : : a constant expression where all occurrences of function
11390 : : parameters are replaced by arbitrary constant expressions
11391 : : of the appropriate type.
11392 : :
11393 : : 2 A conditional expression is a constant expression unless it
11394 : : involves one of the following as a potentially evaluated
11395 : : subexpression (3.2), but subexpressions of logical AND (5.14),
11396 : : logical OR (5.15), and conditional (5.16) operations that are
11397 : : not evaluated are not considered. */
11398 : :
11399 : : static bool
11400 : 3180539464 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11401 : : bool fundef_p, tsubst_flags_t flags,
11402 : : tree *jump_target)
11403 : : {
11404 : : #define RECUR(T,RV) \
11405 : : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11406 : : jump_target)
11407 : :
11408 : 3180539464 : enum { any = false, rval = true };
11409 : 3180539464 : int i;
11410 : 3180539464 : tree tmp;
11411 : :
11412 : 3180539464 : if (t == error_mark_node)
11413 : : return false;
11414 : 3180528440 : if (t == NULL_TREE)
11415 : : return true;
11416 : 3174871707 : location_t loc = cp_expr_loc_or_input_loc (t);
11417 : :
11418 : 3174871707 : if (*jump_target)
11419 : : /* If we are jumping, ignore everything. This is simpler than the
11420 : : cxx_eval_constant_expression handling because we only need to be
11421 : : conservatively correct, and we don't necessarily have a constant value
11422 : : available, so we don't bother with switch tracking. */
11423 : : return true;
11424 : :
11425 : 825570 : if (TREE_THIS_VOLATILE (t) && want_rval
11426 : 284359 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11427 : 3169178230 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11428 : : {
11429 : 77613 : if (TREE_CLOBBER_P (t))
11430 : : {
11431 : : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
11432 : 0 : gcc_checking_assert (false);
11433 : : return true;
11434 : : }
11435 : :
11436 : 77613 : if (flags & tf_error)
11437 : 21 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11438 : : "a volatile lvalue %qE with type %qT", t,
11439 : 21 : TREE_TYPE (t));
11440 : 77613 : return false;
11441 : : }
11442 : 3169022978 : if (CONSTANT_CLASS_P (t))
11443 : : return true;
11444 : 2401562277 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11445 : 2401562277 : && TREE_TYPE (t) == error_mark_node)
11446 : : return false;
11447 : :
11448 : 2401562252 : switch (TREE_CODE (t))
11449 : : {
11450 : : case FUNCTION_DECL:
11451 : : case BASELINK:
11452 : : case TEMPLATE_DECL:
11453 : : case OVERLOAD:
11454 : : case TEMPLATE_ID_EXPR:
11455 : : case LABEL_DECL:
11456 : : case CASE_LABEL_EXPR:
11457 : : case PREDICT_EXPR:
11458 : : case CONST_DECL:
11459 : : case SIZEOF_EXPR:
11460 : : case ALIGNOF_EXPR:
11461 : : case OFFSETOF_EXPR:
11462 : : case NOEXCEPT_EXPR:
11463 : : case TEMPLATE_PARM_INDEX:
11464 : : case TRAIT_EXPR:
11465 : : case IDENTIFIER_NODE:
11466 : : case USERDEF_LITERAL:
11467 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11468 : : case FIELD_DECL:
11469 : : case RESULT_DECL:
11470 : : case USING_DECL:
11471 : : case USING_STMT:
11472 : : case PLACEHOLDER_EXPR:
11473 : : case REQUIRES_EXPR:
11474 : : case STATIC_ASSERT:
11475 : : case DEBUG_BEGIN_STMT:
11476 : : return true;
11477 : :
11478 : 15473723 : case RETURN_EXPR:
11479 : 15473723 : if (!RECUR (TREE_OPERAND (t, 0), any))
11480 : : return false;
11481 : : /* FALLTHROUGH */
11482 : :
11483 : 15338070 : case BREAK_STMT:
11484 : 15338070 : case CONTINUE_STMT:
11485 : 15338070 : *jump_target = t;
11486 : 15338070 : return true;
11487 : :
11488 : 242803537 : case PARM_DECL:
11489 : 242803537 : if (now && want_rval)
11490 : : {
11491 : 72767915 : tree type = TREE_TYPE (t);
11492 : 72767915 : if (dependent_type_p (type)
11493 : 48370922 : || !COMPLETE_TYPE_P (processing_template_decl
11494 : : ? type : complete_type (type))
11495 : 121138835 : || is_really_empty_class (type, /*ignore_vptr*/false))
11496 : : /* An empty class has no data to read. */
11497 : 24397332 : return true;
11498 : 48370583 : if (flags & tf_error)
11499 : 16 : constexpr_error (input_location, fundef_p,
11500 : : "%qE is not a constant expression", t);
11501 : 48370583 : return false;
11502 : : }
11503 : : return true;
11504 : :
11505 : 183926987 : case AGGR_INIT_EXPR:
11506 : 183926987 : case CALL_EXPR:
11507 : : /* -- an invocation of a function other than a constexpr function
11508 : : or a constexpr constructor. */
11509 : 183926987 : {
11510 : 183926987 : tree fun = get_function_named_in_call (t);
11511 : 183926987 : const int nargs = call_expr_nargs (t);
11512 : 183926987 : i = 0;
11513 : :
11514 : 183926987 : if (fun == NULL_TREE)
11515 : : {
11516 : : /* Reset to allow the function to continue past the end
11517 : : of the block below. Otherwise return early. */
11518 : 100714 : bool bail = true;
11519 : :
11520 : 100714 : if (TREE_CODE (t) == CALL_EXPR
11521 : 100714 : && CALL_EXPR_FN (t) == NULL_TREE)
11522 : 100714 : switch (CALL_EXPR_IFN (t))
11523 : : {
11524 : : /* These should be ignored, they are optimized away from
11525 : : constexpr functions. */
11526 : : case IFN_UBSAN_NULL:
11527 : : case IFN_UBSAN_BOUNDS:
11528 : : case IFN_UBSAN_VPTR:
11529 : : case IFN_FALLTHROUGH:
11530 : : case IFN_ASSUME:
11531 : : return true;
11532 : :
11533 : : case IFN_ADD_OVERFLOW:
11534 : : case IFN_SUB_OVERFLOW:
11535 : : case IFN_MUL_OVERFLOW:
11536 : : case IFN_LAUNDER:
11537 : : case IFN_VEC_CONVERT:
11538 : : bail = false;
11539 : : break;
11540 : :
11541 : : default:
11542 : : break;
11543 : : }
11544 : :
11545 : : if (bail)
11546 : : {
11547 : : /* fold_call_expr can't do anything with IFN calls. */
11548 : 44 : if (flags & tf_error)
11549 : 0 : constexpr_error (loc, fundef_p,
11550 : : "call to internal function %qE", t);
11551 : 44 : return false;
11552 : : }
11553 : : }
11554 : :
11555 : 183826273 : if (fun && is_overloaded_fn (fun))
11556 : : {
11557 : 167494311 : if (!RECUR (fun, true))
11558 : : return false;
11559 : 167080148 : fun = get_fns (fun);
11560 : :
11561 : 167080148 : if (TREE_CODE (fun) == FUNCTION_DECL)
11562 : : {
11563 : 146420984 : if (builtin_valid_in_constant_expr_p (fun))
11564 : : return true;
11565 : 145576869 : if (!maybe_constexpr_fn (fun)
11566 : : /* Allow any built-in function; if the expansion
11567 : : isn't constant, we'll deal with that then. */
11568 : 43099324 : && !fndecl_built_in_p (fun)
11569 : : /* In C++20, replaceable global allocation functions
11570 : : are constant expressions. */
11571 : 30766347 : && (!cxx_replaceable_global_alloc_fn (fun)
11572 : 181364 : || TREE_CODE (t) != CALL_EXPR
11573 : 181364 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
11574 : 70647 : && (current_function_decl == NULL_TREE
11575 : 70647 : || !is_std_allocator_allocate
11576 : 70647 : (current_function_decl))))
11577 : : /* Allow placement new in std::construct_at. */
11578 : 30589942 : && (!cxx_placement_new_fn (fun)
11579 : 50972 : || TREE_CODE (t) != CALL_EXPR
11580 : 50972 : || current_function_decl == NULL_TREE
11581 : 50966 : || !is_std_construct_at (current_function_decl))
11582 : 30556458 : && !cxx_dynamic_cast_fn_p (fun)
11583 : 176130713 : && !cxx_cxa_builtin_fn_p (fun))
11584 : : {
11585 : : /* In C++26 evaluation of the function arguments might
11586 : : throw and in that case it is irrelevant whether
11587 : : fun is constexpr or not. */
11588 : 30520823 : if (cxx_dialect >= cxx26)
11589 : 10973770 : for (; i < nargs; ++i)
11590 : : {
11591 : 6609699 : tree x = get_nth_callarg (t, i);
11592 : 6609699 : bool rv = processing_template_decl ? any : rval;
11593 : 6609699 : bool sub_now = false;
11594 : 6609699 : if (!potential_constant_expression_1 (x, rv, strict,
11595 : : sub_now,
11596 : : fundef_p,
11597 : : flags,
11598 : : jump_target))
11599 : : return false;
11600 : 6054495 : if (throws (jump_target))
11601 : : return true;
11602 : : }
11603 : 29870625 : if ((flags & tf_error)
11604 : 29870625 : && constexpr_error (loc, fundef_p,
11605 : : "call to non-%<constexpr%> "
11606 : : "function %qD", fun))
11607 : 263 : explain_invalid_constexpr_fn (fun);
11608 : 29870625 : return false;
11609 : : }
11610 : : }
11611 : :
11612 : 135715210 : fun = OVL_FIRST (fun);
11613 : : /* Skip initial arguments to base constructors. */
11614 : 135715210 : if (DECL_BASE_CONSTRUCTOR_P (fun))
11615 : 2403175 : i = num_artificial_parms_for (fun);
11616 : : }
11617 : 16331962 : else if (fun)
11618 : : {
11619 : 16331962 : if (TREE_TYPE (fun)
11620 : 16331962 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
11621 : : want_rval = rval;
11622 : : else
11623 : : want_rval = any;
11624 : 16331962 : if (RECUR (fun, want_rval))
11625 : : /* Might end up being a constant function pointer. But it
11626 : : could also be a function object with constexpr op(), so
11627 : : we pass 'any' so that the underlying VAR_DECL is deemed
11628 : : as potentially-constant even though it wasn't declared
11629 : : constexpr. */;
11630 : : else
11631 : : return false;
11632 : : }
11633 : 300760719 : for (; i < nargs; ++i)
11634 : : {
11635 : 165173283 : tree x = get_nth_callarg (t, i);
11636 : : /* In a template, reference arguments haven't been converted to
11637 : : REFERENCE_TYPE and we might not even know if the parameter
11638 : : is a reference, so accept lvalue constants too. */
11639 : 165173283 : bool rv = processing_template_decl ? any : rval;
11640 : : /* Don't require an immediately constant value, as constexpr
11641 : : substitution might not use the value of the argument. */
11642 : 165173283 : bool sub_now = false;
11643 : 165173283 : if (!potential_constant_expression_1 (x, rv, strict,
11644 : : sub_now, fundef_p, flags,
11645 : : jump_target))
11646 : : return false;
11647 : 1959643527 : if (throws (jump_target))
11648 : : return true;
11649 : : }
11650 : : /* In C++26 a function could throw. */
11651 : 135587436 : if (*jump_target == NULL_TREE && callee_might_throw (t))
11652 : 12260995 : *jump_target = void_node;
11653 : : return true;
11654 : : }
11655 : :
11656 : 133587636 : case NON_LVALUE_EXPR:
11657 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
11658 : : -- an lvalue of integral type that refers to a non-volatile
11659 : : const variable or static data member initialized with
11660 : : constant expressions, or
11661 : :
11662 : : -- an lvalue of literal type that refers to non-volatile
11663 : : object defined with constexpr, or that refers to a
11664 : : sub-object of such an object; */
11665 : 133587636 : return RECUR (TREE_OPERAND (t, 0), rval);
11666 : :
11667 : 34717 : case EXCESS_PRECISION_EXPR:
11668 : 34717 : return RECUR (TREE_OPERAND (t, 0), rval);
11669 : :
11670 : 240780062 : case VAR_DECL:
11671 : 240780062 : if (DECL_HAS_VALUE_EXPR_P (t))
11672 : : {
11673 : 4044109 : if (now && is_normal_capture_proxy (t))
11674 : : {
11675 : : /* -- in a lambda-expression, a reference to this or to a
11676 : : variable with automatic storage duration defined outside that
11677 : : lambda-expression, where the reference would be an
11678 : : odr-use. */
11679 : :
11680 : 443026 : if (want_rval)
11681 : : /* Since we're doing an lvalue-rvalue conversion, this might
11682 : : not be an odr-use, so evaluate the variable directly. */
11683 : 442346 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
11684 : :
11685 : 680 : if (flags & tf_error)
11686 : : {
11687 : 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
11688 : 3 : auto_diagnostic_group d;
11689 : 3 : if (constexpr_error (input_location, fundef_p,
11690 : : "lambda capture of %qE is not a "
11691 : : "constant expression", cap)
11692 : 3 : && decl_constant_var_p (cap))
11693 : 3 : inform (input_location, "because it is used as a glvalue");
11694 : 3 : }
11695 : 680 : return false;
11696 : : }
11697 : 3601083 : tree ve = DECL_VALUE_EXPR (t);
11698 : : /* Treat __PRETTY_FUNCTION__ inside a template function as
11699 : : potentially-constant. */
11700 : 3601083 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
11701 : : return true;
11702 : 3601078 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
11703 : 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
11704 : 3598342 : return RECUR (ve, rval);
11705 : : }
11706 : 236735953 : if (want_rval
11707 : 163066662 : && (now || !var_in_maybe_constexpr_fn (t))
11708 : 153784707 : && !type_dependent_expression_p (t)
11709 : 122328112 : && !decl_maybe_constant_var_p (t)
11710 : 51017244 : && !is_local_temp (t)
11711 : 50545277 : && (strict
11712 : 1318406 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
11713 : 417205 : || (DECL_INITIAL (t)
11714 : 410128 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
11715 : 50537431 : && COMPLETE_TYPE_P (TREE_TYPE (t))
11716 : 287258549 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
11717 : : {
11718 : 50517740 : if (flags & tf_error)
11719 : 160 : non_const_var_error (loc, t, fundef_p);
11720 : 50517740 : return false;
11721 : : }
11722 : : return true;
11723 : :
11724 : 257045129 : case NOP_EXPR:
11725 : 257045129 : if (REINTERPRET_CAST_P (t))
11726 : : {
11727 : 133298 : if (flags & tf_error)
11728 : 47 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
11729 : : "constant expression");
11730 : 133298 : return false;
11731 : : }
11732 : : /* FALLTHRU */
11733 : 590560883 : case CONVERT_EXPR:
11734 : 590560883 : case VIEW_CONVERT_EXPR:
11735 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
11736 : : may change to something more specific to type-punning (DR 1312). */
11737 : 590560883 : {
11738 : 590560883 : tree from = TREE_OPERAND (t, 0);
11739 : 590560883 : if (location_wrapper_p (t))
11740 : : {
11741 : 292713212 : iloc_sentinel ils = loc;
11742 : 292713212 : return (RECUR (from, want_rval));
11743 : 292713212 : }
11744 : 297847671 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
11745 : : {
11746 : 153091220 : STRIP_ANY_LOCATION_WRAPPER (from);
11747 : 153091220 : if (TREE_CODE (from) == INTEGER_CST
11748 : 153091220 : && !integer_zerop (from))
11749 : : {
11750 : 1778 : if (flags & tf_error)
11751 : 22 : constexpr_error (loc, fundef_p,
11752 : : "%<reinterpret_cast%> from integer to "
11753 : : "pointer");
11754 : 1778 : return false;
11755 : : }
11756 : : }
11757 : 297845893 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
11758 : : }
11759 : :
11760 : 11854 : case ADDRESSOF_EXPR:
11761 : : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
11762 : 11854 : t = TREE_OPERAND (t, 0);
11763 : 11854 : goto handle_addr_expr;
11764 : :
11765 : 58178618 : case ADDR_EXPR:
11766 : : /* -- a unary operator & that is applied to an lvalue that
11767 : : designates an object with thread or automatic storage
11768 : : duration; */
11769 : 58178618 : t = TREE_OPERAND (t, 0);
11770 : :
11771 : 58178618 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
11772 : : /* A pointer-to-member constant. */
11773 : : return true;
11774 : :
11775 : 58190234 : handle_addr_expr:
11776 : : #if 0
11777 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
11778 : : any checking here, as we might dereference the pointer later. If
11779 : : we remove this code, also remove check_automatic_or_tls. */
11780 : : i = check_automatic_or_tls (t);
11781 : : if (i == ck_ok)
11782 : : return true;
11783 : : if (i == ck_bad)
11784 : : {
11785 : : if (flags & tf_error)
11786 : : error ("address-of an object %qE with thread local or "
11787 : : "automatic storage is not a constant expression", t);
11788 : : return false;
11789 : : }
11790 : : #endif
11791 : 58190234 : return RECUR (t, any);
11792 : :
11793 : 74278594 : case COMPONENT_REF:
11794 : 74278594 : case ARROW_EXPR:
11795 : 74278594 : case OFFSET_REF:
11796 : : /* -- a class member access unless its postfix-expression is
11797 : : of literal type or of pointer to literal type. */
11798 : : /* This test would be redundant, as it follows from the
11799 : : postfix-expression being a potential constant expression. */
11800 : 74278594 : if (type_unknown_p (t))
11801 : : return true;
11802 : 67749618 : if (is_overloaded_fn (t))
11803 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
11804 : : which uses ob as an lvalue. */
11805 : 69330481 : want_rval = false;
11806 : 69330481 : gcc_fallthrough ();
11807 : :
11808 : 69330481 : case REALPART_EXPR:
11809 : 69330481 : case IMAGPART_EXPR:
11810 : 69330481 : case BIT_FIELD_REF:
11811 : 69330481 : return RECUR (TREE_OPERAND (t, 0), want_rval);
11812 : :
11813 : 218904 : case EXPR_PACK_EXPANSION:
11814 : 218904 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
11815 : :
11816 : : case PACK_INDEX_EXPR:
11817 : : return true;
11818 : :
11819 : 72108727 : case INDIRECT_REF:
11820 : 72108727 : return RECUR (TREE_OPERAND (t, 0), rval);
11821 : :
11822 : 14424038 : case STATEMENT_LIST:
11823 : 52501324 : for (tree stmt : tsi_range (t))
11824 : 38490908 : if (!RECUR (stmt, any))
11825 : 252666672 : return false;
11826 : : return true;
11827 : :
11828 : 2190122 : case MODIFY_EXPR:
11829 : 2190122 : if (cxx_dialect < cxx14)
11830 : 1957 : goto fail;
11831 : 2188165 : if (!RECUR (TREE_OPERAND (t, 0), any))
11832 : : return false;
11833 : : /* Just ignore clobbers. */
11834 : 1759281 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
11835 : : return true;
11836 : 1619501 : if (!RECUR (TREE_OPERAND (t, 1), rval))
11837 : : return false;
11838 : : return true;
11839 : :
11840 : 323374 : case MODOP_EXPR:
11841 : 323374 : if (cxx_dialect < cxx14)
11842 : 96 : goto fail;
11843 : 323278 : if (!RECUR (TREE_OPERAND (t, 0), rval))
11844 : : return false;
11845 : 311846 : if (!RECUR (TREE_OPERAND (t, 2), rval))
11846 : : return false;
11847 : : return true;
11848 : :
11849 : 292911 : case DO_STMT:
11850 : 292911 : if (!RECUR (DO_COND (t), rval))
11851 : : return false;
11852 : 292911 : if (!RECUR (DO_BODY (t), any))
11853 : : return false;
11854 : 292866 : if (breaks (jump_target) || continues (jump_target))
11855 : 2 : *jump_target = NULL_TREE;
11856 : : return true;
11857 : :
11858 : 226429 : case FOR_STMT:
11859 : 226429 : if (!RECUR (FOR_INIT_STMT (t), any))
11860 : : return false;
11861 : 226429 : if (!RECUR (FOR_COND_PREP (t), any))
11862 : : return false;
11863 : 226429 : tmp = FOR_COND (t);
11864 : 226429 : if (!RECUR (tmp, rval))
11865 : : return false;
11866 : 226359 : if (tmp)
11867 : : {
11868 : 181583 : if (!processing_template_decl)
11869 : 176743 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11870 : : /* If we couldn't evaluate the condition, it might not ever be
11871 : : true. */
11872 : 181583 : if (!integer_onep (tmp))
11873 : : {
11874 : : /* Before returning true, check if the for body can contain
11875 : : a return. */
11876 : 181583 : hash_set<tree> pset;
11877 : 181583 : check_for_return_continue_data data = { &pset, NULL_TREE,
11878 : 181583 : NULL_TREE, false };
11879 : 181583 : if (tree ret_expr
11880 : 181583 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
11881 : : &data, &pset))
11882 : 105084 : *jump_target = ret_expr;
11883 : 181583 : if (data.could_throw)
11884 : 14922 : *jump_target = void_node;
11885 : 181583 : return true;
11886 : 181583 : }
11887 : : }
11888 : 44776 : if (!RECUR (FOR_EXPR (t), any))
11889 : : return false;
11890 : 44776 : if (!RECUR (FOR_BODY (t), any))
11891 : : return false;
11892 : 44776 : if (!RECUR (FOR_COND_CLEANUP (t), any))
11893 : : return false;
11894 : 44776 : if (breaks (jump_target) || continues (jump_target))
11895 : 12 : *jump_target = NULL_TREE;
11896 : : return true;
11897 : :
11898 : 429 : case RANGE_FOR_STMT:
11899 : 429 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
11900 : : return false;
11901 : 429 : if (!RECUR (RANGE_FOR_EXPR (t), any))
11902 : : return false;
11903 : 429 : if (!RECUR (RANGE_FOR_BODY (t), any))
11904 : : return false;
11905 : 425 : if (breaks (jump_target) || continues (jump_target))
11906 : 0 : *jump_target = NULL_TREE;
11907 : : return true;
11908 : :
11909 : 94050 : case WHILE_STMT:
11910 : 94050 : if (!RECUR (WHILE_COND_PREP (t), any))
11911 : : return false;
11912 : 94050 : tmp = WHILE_COND (t);
11913 : 94050 : if (!RECUR (tmp, rval))
11914 : : return false;
11915 : 93989 : if (!processing_template_decl)
11916 : 93973 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11917 : : /* If we couldn't evaluate the condition, it might not ever be true. */
11918 : 93989 : if (!integer_onep (tmp))
11919 : : {
11920 : : /* Before returning true, check if the while body can contain
11921 : : a return. */
11922 : 91122 : hash_set<tree> pset;
11923 : 91122 : check_for_return_continue_data data = { &pset, NULL_TREE,
11924 : 91122 : NULL_TREE, false };
11925 : 91122 : if (tree ret_expr
11926 : 91122 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
11927 : : &data, &pset))
11928 : 235 : *jump_target = ret_expr;
11929 : 91122 : if (data.could_throw)
11930 : 6633 : *jump_target = void_node;
11931 : 91122 : return true;
11932 : 91122 : }
11933 : 2867 : if (!RECUR (WHILE_BODY (t), any))
11934 : : return false;
11935 : 2866 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
11936 : : return false;
11937 : 2866 : if (breaks (jump_target) || continues (jump_target))
11938 : 16 : *jump_target = NULL_TREE;
11939 : : return true;
11940 : :
11941 : 21514 : case SWITCH_STMT:
11942 : 21514 : if (!RECUR (SWITCH_STMT_COND (t), rval))
11943 : : return false;
11944 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
11945 : : unreachable labels would be checked and it is enough if there is
11946 : : a single switch cond value for which it is a valid constant
11947 : : expression. We need to check if there are any RETURN_EXPRs
11948 : : or CONTINUE_STMTs inside of the body though, as in that case
11949 : : we need to set *jump_target. */
11950 : : else
11951 : : {
11952 : 21508 : hash_set<tree> pset;
11953 : 21508 : check_for_return_continue_data data = { &pset, NULL_TREE,
11954 : 21508 : NULL_TREE, false };
11955 : 21508 : if (tree ret_expr
11956 : 21508 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
11957 : : &data, &pset))
11958 : : /* The switch might return. */
11959 : 20991 : *jump_target = ret_expr;
11960 : 517 : else if (data.continue_stmt)
11961 : : /* The switch can't return, but might continue. */
11962 : 3 : *jump_target = data.continue_stmt;
11963 : 21508 : if (data.could_throw)
11964 : 34 : *jump_target = void_node;
11965 : 21508 : }
11966 : 21508 : return true;
11967 : :
11968 : 44 : case STMT_EXPR:
11969 : 44 : return RECUR (STMT_EXPR_STMT (t), rval);
11970 : :
11971 : 275084 : case LAMBDA_EXPR:
11972 : 275084 : if (cxx_dialect >= cxx17)
11973 : : /* In C++17 lambdas can be constexpr, don't give up yet. */
11974 : : return true;
11975 : 413 : else if (flags & tf_error)
11976 : 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
11977 : : "constant expression before C++17");
11978 : : return false;
11979 : :
11980 : 108280 : case NEW_EXPR:
11981 : 108280 : case VEC_NEW_EXPR:
11982 : 108280 : case DELETE_EXPR:
11983 : 108280 : case VEC_DELETE_EXPR:
11984 : 108280 : if (cxx_dialect >= cxx20)
11985 : : /* In C++20, new-expressions are potentially constant. */
11986 : : return true;
11987 : 79934 : else if (flags & tf_error)
11988 : 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
11989 : : "constant expression before C++20");
11990 : : return false;
11991 : :
11992 : 66449 : case DYNAMIC_CAST_EXPR:
11993 : 66449 : case PSEUDO_DTOR_EXPR:
11994 : 66449 : case OMP_PARALLEL:
11995 : 66449 : case OMP_TASK:
11996 : 66449 : case OMP_FOR:
11997 : 66449 : case OMP_SIMD:
11998 : 66449 : case OMP_DISTRIBUTE:
11999 : 66449 : case OMP_TASKLOOP:
12000 : 66449 : case OMP_LOOP:
12001 : 66449 : case OMP_TEAMS:
12002 : 66449 : case OMP_TARGET_DATA:
12003 : 66449 : case OMP_TARGET:
12004 : 66449 : case OMP_SECTIONS:
12005 : 66449 : case OMP_ORDERED:
12006 : 66449 : case OMP_CRITICAL:
12007 : 66449 : case OMP_SINGLE:
12008 : 66449 : case OMP_SCAN:
12009 : 66449 : case OMP_SCOPE:
12010 : 66449 : case OMP_SECTION:
12011 : 66449 : case OMP_MASTER:
12012 : 66449 : case OMP_MASKED:
12013 : 66449 : case OMP_TASKGROUP:
12014 : 66449 : case OMP_TARGET_UPDATE:
12015 : 66449 : case OMP_TARGET_ENTER_DATA:
12016 : 66449 : case OMP_TARGET_EXIT_DATA:
12017 : 66449 : case OMP_ATOMIC:
12018 : 66449 : case OMP_ATOMIC_READ:
12019 : 66449 : case OMP_ATOMIC_CAPTURE_OLD:
12020 : 66449 : case OMP_ATOMIC_CAPTURE_NEW:
12021 : 66449 : case OMP_DEPOBJ:
12022 : 66449 : case OACC_PARALLEL:
12023 : 66449 : case OACC_KERNELS:
12024 : 66449 : case OACC_SERIAL:
12025 : 66449 : case OACC_DATA:
12026 : 66449 : case OACC_HOST_DATA:
12027 : 66449 : case OACC_LOOP:
12028 : 66449 : case OACC_CACHE:
12029 : 66449 : case OACC_DECLARE:
12030 : 66449 : case OACC_ENTER_DATA:
12031 : 66449 : case OACC_EXIT_DATA:
12032 : 66449 : case OACC_UPDATE:
12033 : 66449 : case OMP_ARRAY_SECTION:
12034 : : /* GCC internal stuff. */
12035 : 66449 : case VA_ARG_EXPR:
12036 : 66449 : case TRANSACTION_EXPR:
12037 : 66449 : case AT_ENCODE_EXPR:
12038 : 66449 : fail:
12039 : 66449 : if (flags & tf_error)
12040 : 25 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12041 : : "expression", t);
12042 : : return false;
12043 : :
12044 : : case OMP_DECLARE_MAPPER:
12045 : : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12046 : : constant. */
12047 : : return true;
12048 : :
12049 : 465 : case THROW_EXPR:
12050 : 465 : if (cxx_dialect < cxx26)
12051 : 285 : goto fail;
12052 : 180 : return RECUR (TREE_OPERAND (t, 0), rval);
12053 : :
12054 : 497 : case ASM_EXPR:
12055 : 497 : if (flags & tf_error)
12056 : 3 : inline_asm_in_constexpr_error (loc, fundef_p);
12057 : : return false;
12058 : :
12059 : 370687 : case OBJ_TYPE_REF:
12060 : 370687 : if (cxx_dialect >= cxx20)
12061 : : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12062 : : return true;
12063 : 227327 : else if (flags & tf_error)
12064 : 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12065 : : "%<constexpr%> before C++20");
12066 : : return false;
12067 : :
12068 : 6698 : case TYPEID_EXPR:
12069 : : /* In C++20, a typeid expression whose operand is of polymorphic
12070 : : class type can be constexpr. */
12071 : 6698 : {
12072 : 6698 : tree e = TREE_OPERAND (t, 0);
12073 : 6698 : if (cxx_dialect < cxx20
12074 : 513 : && strict
12075 : 513 : && !TYPE_P (e)
12076 : 273 : && !type_dependent_expression_p (e)
12077 : 6704 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12078 : : {
12079 : 4 : if (flags & tf_error)
12080 : 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12081 : : "constant expression because %qE is "
12082 : : "of polymorphic type", e);
12083 : 4 : return false;
12084 : : }
12085 : : return true;
12086 : : }
12087 : :
12088 : 25658722 : case POINTER_DIFF_EXPR:
12089 : 25658722 : case MINUS_EXPR:
12090 : 25658722 : want_rval = true;
12091 : 25658722 : goto binary;
12092 : :
12093 : 61875368 : case LT_EXPR:
12094 : 61875368 : case LE_EXPR:
12095 : 61875368 : case GT_EXPR:
12096 : 61875368 : case GE_EXPR:
12097 : 61875368 : case EQ_EXPR:
12098 : 61875368 : case NE_EXPR:
12099 : 61875368 : case SPACESHIP_EXPR:
12100 : 61875368 : want_rval = true;
12101 : 61875368 : goto binary;
12102 : :
12103 : 1358666 : case PREINCREMENT_EXPR:
12104 : 1358666 : case POSTINCREMENT_EXPR:
12105 : 1358666 : case PREDECREMENT_EXPR:
12106 : 1358666 : case POSTDECREMENT_EXPR:
12107 : 1358666 : if (cxx_dialect < cxx14)
12108 : 8203 : goto fail;
12109 : 1350463 : goto unary;
12110 : :
12111 : 1043225 : case BIT_NOT_EXPR:
12112 : : /* A destructor. */
12113 : 1043225 : if (TYPE_P (TREE_OPERAND (t, 0)))
12114 : : return true;
12115 : : /* fall through. */
12116 : :
12117 : 43976247 : case CONJ_EXPR:
12118 : 43976247 : case SAVE_EXPR:
12119 : 43976247 : case FIX_TRUNC_EXPR:
12120 : 43976247 : case FLOAT_EXPR:
12121 : 43976247 : case NEGATE_EXPR:
12122 : 43976247 : case ABS_EXPR:
12123 : 43976247 : case ABSU_EXPR:
12124 : 43976247 : case TRUTH_NOT_EXPR:
12125 : 43976247 : case FIXED_CONVERT_EXPR:
12126 : 43976247 : case UNARY_PLUS_EXPR:
12127 : 43976247 : case UNARY_LEFT_FOLD_EXPR:
12128 : 43976247 : case UNARY_RIGHT_FOLD_EXPR:
12129 : 43976247 : case VEC_DUPLICATE_EXPR:
12130 : 1043225 : unary:
12131 : 43976247 : return RECUR (TREE_OPERAND (t, 0), rval);
12132 : :
12133 : 28178755 : case CAST_EXPR:
12134 : 28178755 : case CONST_CAST_EXPR:
12135 : 28178755 : case STATIC_CAST_EXPR:
12136 : 28178755 : case REINTERPRET_CAST_EXPR:
12137 : 28178755 : case IMPLICIT_CONV_EXPR:
12138 : 28178755 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12139 : : /* In C++98, a conversion to non-integral type can't be part of a
12140 : : constant expression. */
12141 : : {
12142 : 295 : if (flags & tf_error)
12143 : 0 : constexpr_error (loc, fundef_p,
12144 : : "cast to non-integral type %qT in a constant "
12145 : 0 : "expression", TREE_TYPE (t));
12146 : 295 : return false;
12147 : : }
12148 : : /* This might be a conversion from a class to a (potentially) literal
12149 : : type. Let's consider it potentially constant since the conversion
12150 : : might be a constexpr user-defined conversion. */
12151 : 28178460 : else if (cxx_dialect >= cxx11
12152 : 28159084 : && (dependent_type_p (TREE_TYPE (t))
12153 : 8055803 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12154 : 8050810 : || literal_type_p (TREE_TYPE (t)))
12155 : 28133796 : && TREE_OPERAND (t, 0)
12156 : 56089641 : && (TREE_CODE (t) != CAST_EXPR
12157 : 22394131 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12158 : : {
12159 : 27890508 : tree from = TREE_OPERAND (t, 0);
12160 : 27890508 : if (TREE_CODE (t) == CAST_EXPR)
12161 : 22373458 : from = TREE_VALUE (from);
12162 : 27890508 : tree type = TREE_TYPE (from);
12163 : : /* If this is a dependent type, it could end up being a class
12164 : : with conversions. */
12165 : 27890508 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12166 : : return true;
12167 : : /* Or a non-dependent class which has conversions. */
12168 : 353588 : else if (CLASS_TYPE_P (type)
12169 : 353588 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12170 : 98835 : return true;
12171 : : }
12172 : :
12173 : 23762434 : return (RECUR (TREE_OPERAND (t, 0),
12174 : 23762434 : !TYPE_REF_P (TREE_TYPE (t))));
12175 : :
12176 : 8357811 : case BIND_EXPR:
12177 : 8357811 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12178 : :
12179 : 36530532 : case CLEANUP_POINT_EXPR:
12180 : 36530532 : case MUST_NOT_THROW_EXPR:
12181 : 36530532 : case TRY_CATCH_EXPR:
12182 : : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12183 : : body could throw, even with catch (...) among handlers we'd need
12184 : : to analyze them in detail if they couldn't rethrow it. More
12185 : : importantly though, throws (jump_target) is just conservative,
12186 : : and there could be e.g.
12187 : : try
12188 : : {
12189 : : possibly_throwing_fn (args);
12190 : : break;
12191 : : }
12192 : : catch (...)
12193 : : {
12194 : : }
12195 : : or continue or return instead of break. So, clearing *jump_target
12196 : : because we see catch (...) handler might mean we missed break
12197 : : etc. */
12198 : 36530532 : case TRY_BLOCK:
12199 : 36530532 : case EH_SPEC_BLOCK:
12200 : 36530532 : case EXPR_STMT:
12201 : 36530532 : case PAREN_EXPR:
12202 : : /* For convenience. */
12203 : 36530532 : case LOOP_EXPR:
12204 : 36530532 : case EXIT_EXPR:
12205 : 36530532 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12206 : :
12207 : 5819128 : case DECL_EXPR:
12208 : 5819128 : tmp = DECL_EXPR_DECL (t);
12209 : 4667700 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12210 : 8766227 : && (processing_template_decl
12211 : 2570146 : ? !decl_maybe_constant_var_p (tmp)
12212 : 2193193 : : !decl_constant_var_p (tmp)))
12213 : : {
12214 : 2249233 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12215 : : {
12216 : 5 : if (flags & tf_error)
12217 : 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12218 : : "%qD defined %<thread_local%> in "
12219 : : "%<constexpr%> context", tmp);
12220 : 5 : return false;
12221 : : }
12222 : 2249228 : else if (TREE_STATIC (tmp))
12223 : : {
12224 : 81 : if (flags & tf_error)
12225 : 15 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12226 : : "%qD defined %<static%> in %<constexpr%> "
12227 : : "context", tmp);
12228 : 81 : return false;
12229 : : }
12230 : 2249147 : else if (!check_for_uninitialized_const_var
12231 : 2249147 : (tmp, /*constexpr_context_p=*/true, flags))
12232 : : return false;
12233 : : }
12234 : 5815261 : if (VAR_P (tmp))
12235 : 4663833 : return RECUR (DECL_INITIAL (tmp), want_rval);
12236 : : return true;
12237 : :
12238 : 26164 : case TRY_FINALLY_EXPR:
12239 : 26164 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12240 : 26164 : && RECUR (TREE_OPERAND (t, 1), any));
12241 : :
12242 : 20363270 : case SCOPE_REF:
12243 : 20363270 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12244 : :
12245 : 23117625 : case TARGET_EXPR:
12246 : 23117625 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12247 : 23057887 : && !TARGET_EXPR_ELIDING_P (t)
12248 : 40810180 : && !literal_type_p (TREE_TYPE (t)))
12249 : : {
12250 : 1342642 : if (flags & tf_error)
12251 : : {
12252 : 21 : auto_diagnostic_group d;
12253 : 21 : if (constexpr_error (loc, fundef_p,
12254 : : "temporary of non-literal type %qT in a "
12255 : 21 : "constant expression", TREE_TYPE (t)))
12256 : 21 : explain_non_literal_class (TREE_TYPE (t));
12257 : 21 : }
12258 : 1342642 : return false;
12259 : : }
12260 : : /* FALLTHRU */
12261 : 39075133 : case INIT_EXPR:
12262 : 39075133 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12263 : : return true;
12264 : 39058601 : return RECUR (TREE_OPERAND (t, 1), rval);
12265 : :
12266 : 28095303 : case CONSTRUCTOR:
12267 : 28095303 : {
12268 : 28095303 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12269 : 28095303 : constructor_elt *ce;
12270 : 118534187 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12271 : 91295380 : if (!RECUR (ce->value, want_rval))
12272 : : return false;
12273 : : return true;
12274 : : }
12275 : :
12276 : 20709044 : case TREE_LIST:
12277 : 20709044 : {
12278 : 20709044 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12279 : : || DECL_P (TREE_PURPOSE (t)));
12280 : 20709044 : if (!RECUR (TREE_VALUE (t), want_rval))
12281 : : return false;
12282 : 18490026 : if (TREE_CHAIN (t) == NULL_TREE)
12283 : : return true;
12284 : 43857 : return RECUR (TREE_CHAIN (t), want_rval);
12285 : : }
12286 : :
12287 : 10061190 : case TRUNC_DIV_EXPR:
12288 : 10061190 : case CEIL_DIV_EXPR:
12289 : 10061190 : case FLOOR_DIV_EXPR:
12290 : 10061190 : case ROUND_DIV_EXPR:
12291 : 10061190 : case TRUNC_MOD_EXPR:
12292 : 10061190 : case CEIL_MOD_EXPR:
12293 : 10061190 : case ROUND_MOD_EXPR:
12294 : 10061190 : {
12295 : 10061190 : tree denom = TREE_OPERAND (t, 1);
12296 : 10061190 : if (!RECUR (denom, rval))
12297 : : return false;
12298 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
12299 : : that hasn't been through instantiate_non_dependent_expr yet. */
12300 : 9573546 : if (!processing_template_decl)
12301 : 4036077 : denom = cxx_eval_outermost_constant_expr (denom, true);
12302 : 9573546 : if (integer_zerop (denom))
12303 : : {
12304 : 335 : if (flags & tf_error)
12305 : 35 : constexpr_error (input_location, fundef_p,
12306 : : "division by zero is not a constant expression");
12307 : 335 : return false;
12308 : : }
12309 : : else
12310 : : {
12311 : 9573211 : want_rval = true;
12312 : 9573211 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12313 : : }
12314 : : }
12315 : :
12316 : 4586442 : case COMPOUND_EXPR:
12317 : 4586442 : {
12318 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12319 : : COMPOUND_EXPR; don't get confused. */
12320 : 4586442 : tree op0 = TREE_OPERAND (t, 0);
12321 : 4586442 : tree op1 = TREE_OPERAND (t, 1);
12322 : 4586442 : STRIP_NOPS (op1);
12323 : 4586442 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12324 : 553648 : return RECUR (op0, want_rval);
12325 : : else
12326 : 4032794 : goto binary;
12327 : : }
12328 : :
12329 : : /* If the first operand is the non-short-circuit constant, look at
12330 : : the second operand; otherwise we only care about the first one for
12331 : : potentiality. */
12332 : 15664938 : case TRUTH_AND_EXPR:
12333 : 15664938 : case TRUTH_ANDIF_EXPR:
12334 : 15664938 : tmp = boolean_true_node;
12335 : 15664938 : goto truth;
12336 : 6254193 : case TRUTH_OR_EXPR:
12337 : 6254193 : case TRUTH_ORIF_EXPR:
12338 : 6254193 : tmp = boolean_false_node;
12339 : 21919131 : truth:
12340 : 21919131 : {
12341 : 21919131 : tree op0 = TREE_OPERAND (t, 0);
12342 : 21919131 : tree op1 = TREE_OPERAND (t, 1);
12343 : 21919131 : if (!RECUR (op0, rval))
12344 : : return false;
12345 : 14204046 : if (!(flags & tf_error) && RECUR (op1, rval))
12346 : : /* When quiet, try to avoid expensive trial evaluation by first
12347 : : checking potentiality of the second operand. */
12348 : : return true;
12349 : 1310884 : if (!processing_template_decl)
12350 : 938648 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12351 : 1310884 : if (tree_int_cst_equal (op0, tmp))
12352 : 5349 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12353 : : else
12354 : : return true;
12355 : : }
12356 : :
12357 : : case PLUS_EXPR:
12358 : : case MULT_EXPR:
12359 : : case POINTER_PLUS_EXPR:
12360 : : case RDIV_EXPR:
12361 : : case EXACT_DIV_EXPR:
12362 : : case MIN_EXPR:
12363 : : case MAX_EXPR:
12364 : : case LSHIFT_EXPR:
12365 : : case RSHIFT_EXPR:
12366 : : case LROTATE_EXPR:
12367 : : case RROTATE_EXPR:
12368 : : case BIT_IOR_EXPR:
12369 : : case BIT_XOR_EXPR:
12370 : : case BIT_AND_EXPR:
12371 : : case TRUTH_XOR_EXPR:
12372 : : case UNORDERED_EXPR:
12373 : : case ORDERED_EXPR:
12374 : : case UNLT_EXPR:
12375 : : case UNLE_EXPR:
12376 : : case UNGT_EXPR:
12377 : : case UNGE_EXPR:
12378 : : case UNEQ_EXPR:
12379 : : case LTGT_EXPR:
12380 : : case RANGE_EXPR:
12381 : : case COMPLEX_EXPR:
12382 : 192540316 : want_rval = true;
12383 : : /* Fall through. */
12384 : 192540316 : case ARRAY_REF:
12385 : 192540316 : case ARRAY_RANGE_REF:
12386 : 192540316 : case MEMBER_REF:
12387 : 192540316 : case DOTSTAR_EXPR:
12388 : 192540316 : case MEM_REF:
12389 : 192540316 : case BINARY_LEFT_FOLD_EXPR:
12390 : 192540316 : case BINARY_RIGHT_FOLD_EXPR:
12391 : 192540316 : binary:
12392 : 405066247 : for (i = 0; i < 2; ++i)
12393 : 304926385 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12394 : : return false;
12395 : : return true;
12396 : :
12397 : : case VEC_PERM_EXPR:
12398 : 103887 : for (i = 0; i < 3; ++i)
12399 : 103853 : if (!RECUR (TREE_OPERAND (t, i), true))
12400 : : return false;
12401 : : return true;
12402 : :
12403 : 4663892 : case COND_EXPR:
12404 : 4663892 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12405 : : {
12406 : 8 : if (flags & tf_error)
12407 : 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12408 : : "constant expression");
12409 : 8 : return false;
12410 : : }
12411 : : /* Fall through. */
12412 : 9444369 : case IF_STMT:
12413 : 9444369 : case VEC_COND_EXPR:
12414 : : /* If the condition is a known constant, we know which of the legs we
12415 : : care about; otherwise we only require that the condition and
12416 : : either of the legs be potentially constant. */
12417 : 9444369 : tmp = TREE_OPERAND (t, 0);
12418 : 9444369 : if (!RECUR (tmp, rval))
12419 : : return false;
12420 : 8140240 : if (!processing_template_decl)
12421 : 7001712 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12422 : : /* potential_constant_expression* isn't told if it is called for
12423 : : manifestly_const_eval or not, so for consteval if always
12424 : : process both branches as if the condition is not a known
12425 : : constant. */
12426 : 8140240 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12427 : : {
12428 : 8123063 : if (integer_zerop (tmp))
12429 : 2213921 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12430 : 5909142 : else if (TREE_CODE (tmp) == INTEGER_CST)
12431 : 2055319 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12432 : : }
12433 : 3871000 : tmp = *jump_target;
12434 : 4512082 : for (i = 1; i < 3; ++i)
12435 : : {
12436 : 4406666 : tree this_jump_target = tmp;
12437 : 4406666 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12438 : : want_rval, strict, now, fundef_p,
12439 : : tf_none, &this_jump_target))
12440 : : {
12441 : 3973229 : if (returns (&this_jump_target) || throws (&this_jump_target))
12442 : 1136538 : *jump_target = this_jump_target;
12443 : 2629046 : else if (!returns (jump_target) && !throws (jump_target))
12444 : : {
12445 : 2629046 : if (breaks (&this_jump_target)
12446 : 2629046 : || continues (&this_jump_target))
12447 : 42 : *jump_target = this_jump_target;
12448 : 2629046 : if (i == 1)
12449 : : {
12450 : : /* If the then branch is potentially constant, but
12451 : : does not return, check if the else branch
12452 : : couldn't return, break or continue. */
12453 : 2203492 : hash_set<tree> pset;
12454 : 2203492 : check_for_return_continue_data data = { &pset, NULL_TREE,
12455 : : NULL_TREE,
12456 : 2203492 : false };
12457 : 4406984 : if (tree ret_expr
12458 : 2203492 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12459 : : check_for_return_continue, &data,
12460 : : &pset))
12461 : 18428 : *jump_target = ret_expr;
12462 : 2185064 : else if (*jump_target == NULL_TREE)
12463 : : {
12464 : 2185025 : if (data.continue_stmt)
12465 : 0 : *jump_target = data.continue_stmt;
12466 : 2185025 : else if (data.break_stmt)
12467 : 5 : *jump_target = data.break_stmt;
12468 : : }
12469 : 2203492 : if (data.could_throw)
12470 : 40336 : *jump_target = void_node;
12471 : 2203492 : }
12472 : : }
12473 : 3765584 : return true;
12474 : : }
12475 : : }
12476 : 105416 : if (flags & tf_error)
12477 : : {
12478 : 3 : if (TREE_CODE (t) == IF_STMT)
12479 : 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
12480 : : "constant expression");
12481 : : else
12482 : 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
12483 : : "constant expression", t);
12484 : : }
12485 : : return false;
12486 : :
12487 : 988 : case VEC_INIT_EXPR:
12488 : 988 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
12489 : : return true;
12490 : 411 : if (flags & tf_error)
12491 : : {
12492 : 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
12493 : : "initialization"))
12494 : 3 : diagnose_non_constexpr_vec_init (t);
12495 : : }
12496 : : return false;
12497 : :
12498 : : case TYPE_DECL:
12499 : : case TAG_DEFN:
12500 : : /* We can see these in statement-expressions. */
12501 : : return true;
12502 : :
12503 : 494585 : case CLEANUP_STMT:
12504 : 494585 : if (!RECUR (CLEANUP_BODY (t), any))
12505 : : return false;
12506 : 494305 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
12507 : : return false;
12508 : : return true;
12509 : :
12510 : : case EMPTY_CLASS_EXPR:
12511 : : return true;
12512 : :
12513 : 22 : case GOTO_EXPR:
12514 : 22 : {
12515 : 22 : tree *target = &TREE_OPERAND (t, 0);
12516 : : /* Gotos representing break, continue and cdtor return are OK. */
12517 : 22 : if (breaks (target) || continues (target) || returns (target))
12518 : : {
12519 : 9 : *jump_target = *target;
12520 : 9 : return true;
12521 : : }
12522 : 13 : if (DECL_ARTIFICIAL (*target))
12523 : : /* The user didn't write this goto, this isn't the problem. */
12524 : : return true;
12525 : 11 : if (flags & tf_error)
12526 : 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
12527 : : "expression");
12528 : : return false;
12529 : : }
12530 : :
12531 : 48 : case ASSERTION_STMT:
12532 : 48 : case PRECONDITION_STMT:
12533 : 48 : case POSTCONDITION_STMT:
12534 : 48 : if (!checked_contract_p (get_contract_semantic (t)))
12535 : : return true;
12536 : 33 : return RECUR (CONTRACT_CONDITION (t), rval);
12537 : :
12538 : 224 : case LABEL_EXPR:
12539 : 224 : t = LABEL_EXPR_LABEL (t);
12540 : 224 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
12541 : : return true;
12542 : 29 : else if (flags & tf_error)
12543 : 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
12544 : : "function only available with %<-std=c++23%> or "
12545 : : "%<-std=gnu++23%>");
12546 : : return false;
12547 : :
12548 : 2764 : case ANNOTATE_EXPR:
12549 : 2764 : return RECUR (TREE_OPERAND (t, 0), rval);
12550 : :
12551 : 22272 : case BIT_CAST_EXPR:
12552 : 22272 : return RECUR (TREE_OPERAND (t, 0), rval);
12553 : :
12554 : : /* Coroutine await, yield and return expressions are not. */
12555 : 682 : case CO_AWAIT_EXPR:
12556 : 682 : case CO_YIELD_EXPR:
12557 : 682 : case CO_RETURN_EXPR:
12558 : 682 : case TEMPLATE_FOR_STMT:
12559 : 682 : if (flags & tf_error)
12560 : 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
12561 : : "%qE is not a constant expression", t);
12562 : : return false;
12563 : :
12564 : : /* Assume a TU-local entity is not constant, we'll error later when
12565 : : instantiating. */
12566 : : case TU_LOCAL_ENTITY:
12567 : : return false;
12568 : :
12569 : 30 : case NONTYPE_ARGUMENT_PACK:
12570 : 30 : {
12571 : 30 : tree args = ARGUMENT_PACK_ARGS (t);
12572 : 30 : int len = TREE_VEC_LENGTH (args);
12573 : 90 : for (int i = 0; i < len; ++i)
12574 : 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
12575 : : return false;
12576 : : return true;
12577 : : }
12578 : :
12579 : 0 : default:
12580 : 0 : if (objc_non_constant_expr_p (t))
12581 : : return false;
12582 : :
12583 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
12584 : 0 : gcc_unreachable ();
12585 : : return false;
12586 : : }
12587 : : #undef RECUR
12588 : : }
12589 : :
12590 : : bool
12591 : 1173654826 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
12592 : : bool fundef_p, tsubst_flags_t flags)
12593 : : {
12594 : 1173654826 : if (flags & tf_error)
12595 : : {
12596 : : /* Check potentiality quietly first, as that could be performed more
12597 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
12598 : : that fails, replay the check noisily to give errors. */
12599 : 5937252 : flags &= ~tf_error;
12600 : 5937252 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12601 : : flags))
12602 : : return true;
12603 : 893 : flags |= tf_error;
12604 : : }
12605 : :
12606 : 1167718467 : tree target = NULL_TREE;
12607 : 1167718467 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12608 : 1167718467 : flags, &target);
12609 : : }
12610 : :
12611 : : /* The main entry point to the above. */
12612 : :
12613 : : bool
12614 : 362351413 : potential_constant_expression (tree t)
12615 : : {
12616 : 362351413 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12617 : : /*now*/false, /*fundef_p*/false,
12618 : 362351413 : tf_none);
12619 : : }
12620 : :
12621 : : /* As above, but require a constant rvalue. */
12622 : :
12623 : : bool
12624 : 21326433 : potential_rvalue_constant_expression (tree t)
12625 : : {
12626 : 21326433 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12627 : : /*now*/false, /*fundef_p*/false,
12628 : 21326433 : tf_none);
12629 : : }
12630 : :
12631 : : /* Like above, but complain about non-constant expressions. */
12632 : :
12633 : : bool
12634 : 73 : require_potential_constant_expression (tree t)
12635 : : {
12636 : 73 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12637 : : /*now*/false, /*fundef_p*/false,
12638 : 73 : tf_warning_or_error);
12639 : : }
12640 : :
12641 : : /* Cross product of the above. */
12642 : :
12643 : : bool
12644 : 77446 : require_potential_rvalue_constant_expression (tree t)
12645 : : {
12646 : 77446 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12647 : : /*now*/false, /*fundef_p*/false,
12648 : 77446 : tf_warning_or_error);
12649 : : }
12650 : :
12651 : : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
12652 : :
12653 : : bool
12654 : 211 : require_potential_rvalue_constant_expression_fncheck (tree t)
12655 : : {
12656 : 211 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12657 : : /*now*/false, /*fundef_p*/true,
12658 : 211 : tf_warning_or_error);
12659 : : }
12660 : :
12661 : : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
12662 : :
12663 : : bool
12664 : 520 : require_rvalue_constant_expression (tree t)
12665 : : {
12666 : 520 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12667 : : /*now*/true, /*fundef_p*/false,
12668 : 520 : tf_warning_or_error);
12669 : : }
12670 : :
12671 : : /* Like potential_constant_expression, but don't consider possible constexpr
12672 : : substitution of the current function. That is, PARM_DECL qualifies under
12673 : : potential_constant_expression, but not here.
12674 : :
12675 : : This is basically what you can check when any actual constant values might
12676 : : be value-dependent. */
12677 : :
12678 : : bool
12679 : 605582241 : is_constant_expression (tree t)
12680 : : {
12681 : 605582241 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12682 : : /*now*/true, /*fundef_p*/false,
12683 : 605582241 : tf_none);
12684 : : }
12685 : :
12686 : : /* As above, but expect an rvalue. */
12687 : :
12688 : : bool
12689 : 107623903 : is_rvalue_constant_expression (tree t)
12690 : : {
12691 : 107623903 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12692 : : /*now*/true, /*fundef_p*/false,
12693 : 107623903 : tf_none);
12694 : : }
12695 : :
12696 : : /* Like above, but complain about non-constant expressions. */
12697 : :
12698 : : bool
12699 : 5859002 : require_constant_expression (tree t)
12700 : : {
12701 : 5859002 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12702 : : /*now*/true, /*fundef_p*/false,
12703 : 5859002 : tf_warning_or_error);
12704 : : }
12705 : :
12706 : : /* Like is_constant_expression, but allow const variables that are not allowed
12707 : : under constexpr rules. */
12708 : :
12709 : : bool
12710 : 64896332 : is_static_init_expression (tree t)
12711 : : {
12712 : 64896332 : return potential_constant_expression_1 (t, /*want_rval*/false,
12713 : : /*strict*/false, /*now*/true,
12714 : 64896332 : /*fundef_p*/false, tf_none);
12715 : : }
12716 : :
12717 : : /* Returns true if T is a potential constant expression that is not
12718 : : instantiation-dependent, and therefore a candidate for constant folding even
12719 : : in a template. */
12720 : :
12721 : : bool
12722 : 587558251 : is_nondependent_constant_expression (tree t)
12723 : : {
12724 : 587558251 : return (!type_unknown_p (t)
12725 : 587558202 : && is_constant_expression (t)
12726 : 1070611783 : && !instantiation_dependent_expression_p (t));
12727 : : }
12728 : :
12729 : : /* Returns true if T is a potential static initializer expression that is not
12730 : : instantiation-dependent. */
12731 : :
12732 : : bool
12733 : 64896332 : is_nondependent_static_init_expression (tree t)
12734 : : {
12735 : 64896332 : return (!type_unknown_p (t)
12736 : 64896332 : && is_static_init_expression (t)
12737 : 121106965 : && !instantiation_dependent_expression_p (t));
12738 : : }
12739 : :
12740 : : /* True iff FN is an implicitly constexpr function. */
12741 : :
12742 : : bool
12743 : 160797 : decl_implicit_constexpr_p (tree fn)
12744 : : {
12745 : 160797 : if (!(flag_implicit_constexpr
12746 : 0 : && TREE_CODE (fn) == FUNCTION_DECL
12747 : 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
12748 : : return false;
12749 : :
12750 : 0 : if (DECL_CLONED_FUNCTION_P (fn))
12751 : 0 : fn = DECL_CLONED_FUNCTION (fn);
12752 : :
12753 : 0 : return (DECL_LANG_SPECIFIC (fn)
12754 : 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
12755 : : }
12756 : :
12757 : : /* Finalize constexpr processing after parsing. */
12758 : :
12759 : : void
12760 : 94979 : fini_constexpr (void)
12761 : : {
12762 : : /* The contexpr call and fundef copies tables are no longer needed. */
12763 : 94979 : constexpr_call_table = NULL;
12764 : 94979 : fundef_copies_table = NULL;
12765 : 94979 : }
12766 : :
12767 : : #include "gt-cp-constexpr.h"
|